Operators are the symbols which performs specific task on the operands.
Arithmetic operators are used for numberic calculations. They are of two types-
Prefix Increment / Decrement
#include <stdio.h>
int main()
{
int x = 5, y, b = 10, c;
y = ++x;
c = --b;
printf("The value of y is = %d and x is = %d\n", y, x); // The value of y is = 6 and x is = 6
printf("The value of c is = %d and c is = %d\n", c, b); // The value of c is = 9 and c is = 9
return 0;
}
Postfix Increment / Decrement
#include <stdio.h>
int main()
{
int x = 5, y, b = 10, c;
y = x++;
c = b--;
printf("The value of y is = %d and x is = %d\n", y, x); // The value of y is = 5 and x is = 6
printf("The value of c is = %d and c is = %d\n", c, b); // The value of c is = 10 and c is = 9
return 0;
}
a < b ? printf("a is smaller") : printf("b is smaller");
sizeof(int); // 4
sizeof(float); // 4
int a,b,c;
Operator
Description
Precedence Level
Associativity
( )
Function call
1
left to right
+
Unary plus
2
left to right
*
multiplicaiton
3
left to right
+
addition
4
left to right
<<
left shift
5
left to right
<
less than
6
left to right
= =
equal to
7
left to right
&
Bitwise AND
8
left to right
^
Bitwise XOR
9
left to right
|
Bitwise OR
10
left to right
&&
logical AND
11
left to right
||
logical OR
12
left to right
? :
conditional operator
13
right to left
=
assignment operator
14
right to left
,
comma operator
15
left to right
Questions
Syntax ↓
if(condition)
statement1;
if(condition)
{
statement;
......
....
}
syntax 2↓
if(condition)
statement1;
else
statement2;
// Here if the condition is true then statement1 is executed and if it is false then statement2 is executed.
if(condition)
{
statement;
......
....
}
else
{
statement;
......
....
}
Syntax ↓
if(condition 1)
{
if(condition 2)
statementA1;
else
statementA2;
}
else
{
if(condition 3)
statementB1;
else
statementB2;
}
if(condition1)
statementA;
else
if(condition2)
statementB;
else
if(condition3)
statementC;
else
statementD;
while(conditon)
{
statement;
statement;
}
Program to print the numbers from 1 to 10 using while loop.
#include<stdio.h>
int main()
{
int i = 1;
while(i<=10)
{
printf("%d\t",i);
i++;
}
printf("\n");
}
Output ↓
1 2 3 4 5 6 7 8 9 10
Syntax ↓
do
{
statement;
statement;
.......
}while(condition);
Program to print the numbers from 1 to 10 using do...while loop
#include<stdio.h>
int main()
{
int i = 1;
do
{
printf("%d\t",i);
i++;
}while(i<=10);
printf("\n");
}
Output↓
1 2 3 4 5 6 7 8 9 10
Syntax ↓
for(expression1;expression2;expression3)
{
statement;
statement;
}
Program to understand nesting of for loop
#include<stdio.h>
int main()
{
int i,j;
for(i=1;i<=3;i++) // outer loop
{
printf("i = %d\n",i);
for(j=1;j<=4;j++)
printf("j = %d\t",j);
printf("\n");
}
return 0;
}
goto label;
.....
.....
label:
statement;
......
......
Program to print whether the number is even or odd.
#include<stdio.h>
int main()
{
int n;
printf("Enter the number : ");
scanf("%d",&n);
if(n%2==0)
goto even;
else
goto odd;
even :
printf("Number is even");
goto end;
odd :
printf("Number is odd");
goto end;
end :
printf("\n");
return 0;
}
switch(expression)
{
case constant1:
statement
......
break;
case constant2:
statement
......
break;
case constant3:
statement
......
break;
default:
statement
......
}
#include <stdio.h>
int main()
{
int n,fact=1;
printf("Enter the number whose factorial you want to calculate : ");
scanf("%d",&n);
for (int i = 1; i <= n; i++)
{
fact*=i;
}
printf("The factorial of %d is %d\n",n,fact);
return 0;
}
#include <stdio.h>
int main()
{
int n,fact=1;
printf("Enter the number whose factorial you want to calculate : ");
scanf("%d",&n);
int i = 1;
while(i<=n)
{
fact*=i;
i++;
}
printf("The factorial of %d is %d\n",n,fact);
return 0;
}
#include <stdio.h>
int main()
{
int a, b;
char op;
printf("Enter the value of 'a' and 'b' : ");
scanf("%d%d", &a, &b);
printf("Enter the operation from : '+', '-', '/', '*', '%%' : ");
scanf(" %c", &op);
if (op == '+')
printf("The sum of %d and %d is %d\n", a, b, a + b);
else if (op == '-')
printf("The subtraction of %d and %d is %d\n", a, b, a - b);
else if (op == '*')
printf("The multiplication of %d and %d is %d\n", a, b, a * b);
else if (op == '/')
printf("The division of %d and %d is %d\n", a, b, a / b);
else if (op == '%')
printf("The modulus of %d and %d is %d\n", a, b, a % b);
else
printf("You enter wrong operator\n");
return 0;
}
#include <stdio.h>
int main()
{
char op;
int a, b, c;
printf("Enter the value of a and b : ");
scanf("%d%d", &a, &b);
printf("Enter the operator : ");
scanf(" %c",&op);
switch (op)
{
case '+':
c = a + b;
printf("sum = %d\n", c);
break;
case '-':
c = a - b;
printf("subraction = %d\n", c);
break;
case '*':
c = a * b;
printf("multiplication = %d\n", c);
break;
case '/':
c = a / b;
printf("division = %d\n", c);
break;
case '%':
c = a % b;
printf("remainder = %d\n", c);
break;
default:
printf("Entered the wrong operator\n");
break;
}
return 0;
}
#include <stdio.h>
int main()
{
char ch;
printf("Enter alphabet : ");
scanf("%c", &ch);
switch (ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("The entered alphabet is a vowel\n");
break;
default:
printf("The entered alphabet is a consotant\n");
break;
}
return 0;
}
#include <stdio.h>
int main()
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
printf("* ");
}
printf("\n");
}
return 0;
}
#include
int main()
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
printf("%d ",j);
}
printf("\n");
}
return 0;
}
#include <stdio.h>
int main()
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
printf("%d ",i);
}
printf("\n");
}
return 0;
}
#include <stdio.h>
int main()
{
for(int i = 5;i>=1;i--)
{
for(int j=i;j>=1;j--)
{
printf("* ");
}
printf("\n");
}
return 0;
}
*
* *
* * *
* * * *
* * * * *
#include <stdio.h>
int main()
{
for(int i = 5;i>=1;i--)
{
for(int j=i;j>1;j--)
{
printf("_");
}
for(int k = 1;k<=5-i+1;k++)
{
printf("*");
}
printf("\n");
}
return 0;
}
#include <stdio.h>
int main() {
int num = 3354, rev = 0;
while (num > 0)
{
rev *= 10;
rev += (num % 10);
num /= 10;
}
printf("The reverse is : %d", rev);
return 0;
}