Operators are used to make the efficiency of code.
(+) and (-) operator
Everyone knows the + and - operators. But we can use these operators for swap two numbers without any variable.
Ex:
int a = 6;
int b = 5;
a=a+b;
b=a-b;
a=a-b;
printf("%d,%d",a,b);
As well as we can do multiplication and division by using + and - operators.
(/) operator
By using this operator, we can split the integer and get right side part.
just like substring of number.
Ex:
int a = 1234;
i wanna b as 12;
so b = a/100;
u can get b as 12;
like this u can get 123 by using /10
and u can get 1 by using /1000
(%) operator
This is one special operators of all other operators.
I) get substring from left side of interger
Ex:
int a = 1234;
i wanna b as 34
b = a % 100;
II) Circular array
you can form a circular array by using % operator.
Ex:
output :
vicky
ickyv
ckyvi
kyvic
yvick
vicky
char str[] = "vicky" ;
n=5;
for(i=0;i<=n;i++){
for(j=i;j<n+i;j++){
printf("%c",str[j%n]);
}
printf("\n");
}
iii) sum of digits (output single digits)
You can get sum of digit by using %9
Ex:
a = 123;
b = a % 9;
output is 6 (1+2+3)
if your n % 9 value is 0 then output is 9
ex:
a = 450;
b = a % 9
if ( b == 0)
printf("sum of digit is 9');
else
printf("sum of digit is %d',b);
suppose a is 0 then you need to add another one conditions.
IV) Odd Nubmer
You can find odd number by using %2 operator
No comments:
Post a Comment