If Else & Switch Case
// Simple if statement
#include
int main()
{
int x;
printf( ” Enter a number ::: ” );
scanf( “%d”, &x );
if( x > 100 )
{
printf( “\n Number is greater than 100.” );
}
return 0;
}
Output:
Enter a number ::: 500
Number is greater than 100.
// Find leap year
#include
int main()
{
int year;
printf(“Enter a year : \n”);
scanf(“%d”, &year);
if ( year%400 == 0)
{
printf(“%d is a leap year.\n”, year);
}
else if ( year%100 == 0)
{
printf(“%d is not a leap year.\n”, year);
}
else if ( year%4 == 0 )
{
printf(“%d is a leap year.\n”, year);
}
else
{
printf(“%d is not a leap year.\n”, year);
}
return 0;
}
// find perfect squre root
#include
#include
void main()
{
int n,a;
printf(“Enter the value of N:\n”);
scanf(“%d”,&n);
a=sqrt(n);
if(n-(a*a)==0)
printf(“\n\n\nIt is perfect squre”);
else
printf(“\n\n\nIt is not perfect squre”);
}
Output :
Enter the value of N:
25
It is petfect squre
//Find maximum number between two
#include
int main(){
int a,b,max;
printf(“Enter two numbers : “);
scanf(“%d %d”,&a,&b);
if(a > b)
max = a;
else
max = b;
printf(“Max is %d”,max));
return 0;
}
Output:
Enter two numbers : 12 34
Max is 34
// Find number is even or odd
#include
void main()
{
int a;
printf(“Enter Any Number : \n”);
scanf(“%d”,&a);
printf(“———————-\n”);
if(a%2==0)
{
printf(“\nNumber is Even.”);
}
else
{
printf(“\nNumber is Odd.”);
}
}
Output:
Enter Any Number :
125
———————-
Number is odd.
/*
* C program to check whether a given integer is positive
* or negative */
#include
void main()
{
int number;
printf(“Enter a number “);
scanf(“%d”, &number);
if (number >= 0)
{
printf(“%d is a positive number “, number);
}
else {
printf(“%d is a negative number “, number);
}
}
Output:
Enter a number
-1
-1 is a negative number
Enter a number
14
14 is a positive number
// Neste if else Example
#include
void main()
{
int a,b,c;
printf(“Entar Value A : \n”);
scanf(“%d”,&a);
printf(“Enter Value B : \n”);
scanf(“%d”,&b);
printf(“Enter Value C : \n”);
scanf(“%d”,&c);
if(a>b)
{
if(a>c)
{
printf(“\nA is Big Value.”);
}
else
{
printf(“\nC is Big Value.”);
}
}
else
{
if(b>c)
{
printf(“\nB is Big Value.”);
}
else
{
printf(“\nC is Big Value.”);
}
}
}
Output:
Enter value A:
10
Enter value B:
45
Enter value C:
5
B is Big Value.
// Find perfect triangle
#include
#include
void main()
{
float a,b,c,x,y,z;
printf(“Enter First side:\n”);
scanf(“%f”,&a);
printf(“Enter Second side:\n”);
scanf(“%f”,&b);
printf(“Enter Third side:\n”);
scanf(“%f”,&c);
x=sqrt((a*a)+(b*b));
y=sqrt((b*b)+(c*c));
z=sqrt((c*c)+(a*a));
if(c==x||a==y||b==z)
{
printf(“\nRight Triangle”);
}
else
{
printf(“\nNot Right Triangle”);
}
}
Output:
Enter First side:
3
Enter Second side:
4
Enter Third side:
5
Right Triangle
// check whether the given character is Vowel or not
#include
int main()
{
char ch;
printf( ” Enter any Character : ” );
scanf( “%c”, &ch );
if( ( ch == ‘a’ ) || ( ch == ‘e’ ) || ( ch == ‘i’ ) ||
( ch == ‘o’ ) || ( ch == ‘u’ ) || ( ch == ‘A’ ) ||
( ch == ‘E’ ) || ( ch == ‘I’ ) || ( ch == ‘O’ ) ||
( ch == ‘U’ ) )
{
printf( “\n Character is a Vowel.” );
}
else
{
printf( “\n Character is not a Vowel.” );
}
return 0;
}
Output:
Enter any Character : N
Character is not a Vowel.
// check whether the given character is Alphanumeric or not
#include
#include
int main()
{
int ch;
printf( “\n Enter a character ::: ” );
ch = getchar();
if( isalnum( ch ) )
{
printf( “\n It is alpha-numeric character.\n” );
}
else
{
printf( “\n This is not an alpha-numeric character.\n” );
}
return 0;
}
Output:
Enter a character ::: N
It is alpha-numeric character.
// Find a Any type of character
#include &it >
void main()
{
char c;
printf(“Enter any character:>>\n”);
scanf(“%c”,&c);
if(isupper(c))
printf(“It is UPPERCASE char…..!”);
else if(islower(c))
printf(“It is Lowercase char…..!”);
else if(isdigit(c))
printf(“It is DigiCase char…..!”);
else
printf(“It is Special char…..!”);
}
Output:
Enter any character:>>
D
It is UPPERCASE char…..!
//Find input is number,character (Lower or upper) or other symbol
#include
void main()
{
char n;
printf(“Enter a Character: “);
scanf(“%c”,&n);
if(n>=48&&n=65&&n=97&&n<=122)//ASCII value of a-z is 97-122
{
printf(“Lower case”);
}
else
{
printf(“Other symbol”);
}
}
Output:
Enter a Character: N
Upper case
// Calculator example
#include
void main()
{
char op;
float a,b,result;
printf(“Enter no of A:\n”);
scanf(“%f”,&a);
printf(“Enter no of B:\n”);
scanf(“%f”,&b);
printf(“Operator[+,-,*,/]::\n”);
fflush(stdin);
scanf(“%c”,&op);
if(op==’+’)
{
result=a+b;
printf(“Result:>>%.2f”,result);
}
else if(op==’*’)
{
result=a*b;
printf(“Result:>>%.2f”,result);
}
else if(op==’-‘)
{
result=a-b;
printf(“Result:>>%.2f”,result);
}
else
{
printf(“Invalid Choice:”);
}
}
Output:
Enter no of A:
10
Enter no of B:
20
Operator [+,-,*,/]::
*
Result:>>200.00
// Simple marksheet example
#include
void main()
{
int rono;
char name[20];
float math,sci,eng,total,per;
printf(“Enter the roll no:\n”);
scanf(“%d”,&rono);
printf(“Enter the name:\n”);
scanf(“%s”,name);
printf(“Enter the maths marks:\n”);
scanf(“%f”,&math);
printf(“Enter the sci marks:\n”);
scanf(“%f”,&sci);
printf(“Enter the eng marks:\n”);
scanf(“%f”,&eng);
total=math+sci+eng;
printf(” total Is=%f\n”,total);
per=total/3;
printf(” Percantage Is=%f”,per);
if(per<40)
{
printf(“\n Fail”);
}
else if(per40)
{
printf(“\nGrade C”);
}
else if(per50)
{
printf(“\nGrade B”);
}
else if(per60)
{
printf(“\nGrade A”);
}
else
{
printf(“\nDist”);
}
if(math<35||sci<35||eng<35)
{
printf(“\nFail”);
}
else
{
printf(“\nPass”);
}
}
Output:
Enter the roll no:
102
Enter the name:
Anshi
Enter the maths marks:
60
Enter the sci marks:
80
Enter the eng marks:
85
Total is:225
Percantage is:75
Grade A
Pass
//calculate roots of a quadratic equation
#include
#include
int main(){
float a,b,c;
float d,root1,root2;
printf(“Enter a, b and c of quadratic equation: “);
scanf(“%f%f%f”,&a,&b,&c);
d = b * b – 4 * a * c;
if(d < 0){
printf(“Roots are complex number.\n”);
printf(“Roots of quadratic equation are: “);
printf(“%.3f%+.3fi”,-b/(2*a),sqrt(-d)/(2*a));
printf(“, %.3f%+.3fi”,-b/(2*a),-sqrt(-d)/(2*a));
return 0;
}
else if(d==0){
printf(“Both roots are equal.\n”);
root1 = -b /(2* a);
printf(“Root of quadratic equation is: %.3f “,root1);
return 0;
}
else{
printf(“Roots are real numbers.\n”);
root1 = ( -b + sqrt(d)) / (2* a);
root2 = ( -b – sqrt(d)) / (2* a);
printf(“Roots of quadratic equation are: %.3f , %.3f”,root1,root2);
}
return 0;
}
Output:
Enter a, b and c of quadratic equation: 1 2 1
Roots are equal
Roots of quadratic equation are: -1.000
//Find maximum among three using conditional operator
#include
void main()
{
int a,b,c;
printf(“Enter three numbers : “);
scanf(“%d%d%d”,&a,&b,&c);
printf(“Max is: %d”,a>b?a>c?a:c:b>c?b:c);
}
Output:
Enter three numbers : 10 23 30
Max is: 23
Switch Case Statement
// Find day using switch case
#include
void main()
{
int day;
printf(“Enter day number:>>\n”);
scanf(“%d”,&day);
switch(day)
{
case 1:
{
printf(“Monday”);
break;
}
case 2:
{
printf(“Tuesday”);
break;
}
case 3:
{
printf(“Wednesday”);
break;
}
case 4:
{
printf(“Thursday”);
break;
}
case 5:
{
printf(“Friday”);
break;
}
case 6:
{
printf(“Saturday”);
break;
}
case 7:
{
printf(“Sunday”);
break;
}
default:
{
printf(“Invalid day number”);
}
printf(“\nHave a nice day”);
}
Output:
Enter day number:>>5
Friday
// Operator as case in switch case
#include
void main()
{
char op;
float num1,num2,res;
printf(“Enter first number:>>\n”);
scanf(“%f”,&num1);
printf(“Enter second number:>>\n”);
scanf(“%f”,&num2);
printf(“Operation(+,-,*,/):>>\n”);
fflush(stdin);
scanf(“%c”,&op);
switch(op)
{
case ‘+’:
{
res=num1+num2;
break;
}
case ‘-‘:
{
res=num1-num2;
break;
}
case ‘*’:
{
res=num1*num2;
break;
}
case ‘/’:
{
res=num1/num2;
break;
}
default:
{
printf(“Invalid choice…”);
}
}
printf(“Result::%f”,res);
}
Output:
Enter first number:>>
56
Enter second number:>>
10
Operation(+,-,*,/):>>
*
Result::560
// Multiple choise in Switch case
#include
void main()
{
float p,r,n,i,basic,hra,da,pf,netsal,a,b,c;
int choice;
printf(“\n1.Interest \n2.netsal”);
printf(“\nEnter the value of choice:>>\n”);
scanf(“%d”,&choice);
switch(choice)
{
case 1:
{
printf(“Enter the value P:\n”);
scanf(“%f”,&p);
printf(“Enter the value R:\n”);
scanf(“%f”,&r);
printf(“Enter the value N:\n”);
scanf(“%f”,&n);
i=p*r*n/100;
printf(“Interest::%f”,i);
break;
}
case 2:
{
printf(“Enter the value of basic:\n”);
scanf(“%f”,&basic);
printf(“Enter the value of A:\n”);
scanf(“%f”,&a);
printf(“Enter the value of B:\n”);
scanf(“%f”,&b);
printf(“Enter the value of C:\n”);
scanf(“%f”,&c);
hra=basic*a;
da=basic*b;
pf=basic*c;
netsal=basic+hra+da+pf;
printf(“Net Salary:%f”,netsal);
break;
}
default:
{
printf(“Invalid Choice”);
}
}
}
Output:
1.Intrest
2.netsal
Entre the value of choice:>>
1
Enter the value of p:
1200
Enter the value of r:
1.2
Enter the value of n:
2
Intrest::28.8
// Nested switch case example
#include
#include
void main()
{
int choice,ch1,ch2;
printf(“\n1.Pen \n2.Fruits”);
printf(“\nEnter value of choice:>>”);
scanf(“%d”,&choice);
switch(choice)
{
case 1:
{
printf(“\n1.Micro \n2.Renold \n3.Cello:”);
printf(“\nEnter the value of Ch1:>>\n”);
scanf(“%d”,&ch1);
switch(ch1)
{
case 1:
{
printf(“\nMicro”);
break;
}
case 2:
{
printf(“\nRenold”);
break;
}
case 3:
{
printf(“\nCello”);
break;
}
default:
{
printf(“Invalid case”);
}
}
break;
}
case 2:
{
printf(“\n1.Mango \n2.Orange \n3.Pineple”);
printf(“Enter the value of Ch2:>>\n”);
scanf(“%d”,&ch2);
switch(ch2)
{
case 1:
{
printf(“\nMango”);
break;
}
case 2:
{
printf(“\nOrange”);
break;
}
case 3:
{
printf(“\nPineple”);
break;
}
default:
{
printf(“Invalid case”);
}
}
break;
}
default:
{
printf(“Invalid Choice”);
}
}
}
Output:
1.Pen
2.Fruits
Enter value of choice:>>
1
1.Micro
2.Renold
3.Cello
Enter the value of Ch1:>>
2
Renold
// check whether the given character is Vowel or not using switch case
#include
int main()
{
char ch;
printf( ” Enter any Character : ” );
scanf( “%c”, &ch );
switch( ch )
{
case ‘a’:
case ‘A’:
case ‘e’:
case ‘E’:
case ‘i’:
case ‘I’:
case ‘o’:
case ‘O’:
case ‘u’:
case ‘U’:
printf( “\n %c is Vowel.”, ch );
break;
default :
printf( “\n %c is not a Vowel.”, ch );
break;
}
return 0;
}
Output:
Enter any Character : u
u is Vowel.
// sizeof() operator
#include
int main()
{
int a = 100, b;
b = sizeof(a);
printf( “\n The size of a : %d”, b );
b = sizeof(double);
printf( “\n The size of double : %d”, b );
b = sizeof(123L);
printf( “\n The size of 123L : %d”, b );
b = sizeof(123.45);
printf( “\n The size of 123.45 : %d”, b );
b = sizeof(123.45f);
printf( “\n The size of 123.45f : %d”, b );
getch();
return 0;
}
The size of a :
The size of double :
The size of 123L :
The size of 123.45 :
The size of 123.45f :
// sizeof operator
#include
int main()
{
printf( “The size of an int is:\t\t” );
printf( “%d bytes.\n”, sizeof(int) );
printf( “The size of a short int is:\t” );
printf( “%d bytes.\n”, sizeof(short) );
printf( “The size of a long int is:\t” );;
printf( “%d bytes.\n”, sizeof(long) );
printf( “The size of a char is:\t\t” );
printf( “%d bytes.\n”, sizeof(char) );
printf( “The size of a float is:\t\t” );
printf( “%d bytes.\n”, sizeof(float) );
printf( “The size of a double is:\t” );
printf( “%d bytes.\n”, sizeof(double) );
return 0;
}
Output:
The size of an int is: 4 bytes.
The size of a short int is: 2 bytes.
The size of a long int is: 4 bytes.
The size of a char is: 1 bytes.
The size of a float is: 4 bytes.
The size of a double is: 8 bytes.
// Use of Comma
#include
int main()
{
int a, b, c;
c = ( a = 20, b = 30, a + b );
printf( “\n a : %d”, a );
printf( “\n b : %d”, b );
printf( “\n c : %d”, c );
return 0;
}
Output:
a : 20
b : 30
c : 50
// Use of Comma
#include
int main()
{
int num, sq, cube;
num = 2;
sq = (num * num), cube = (num * num * num);
printf( “\n The square of %d is : %d”, num, sq );
printf( “\n The cube of %d is : %d”, num, cube );
return 0;
}
Output:
The square of 2 is : 4
The cube of 2 is : 8
// Conditional OR Ternary Operator ( ? : )
#include
int main()
{
int a, b, max;
printf( ” Enter Two Numbers ::: ” );
scanf( “%d %d”, &a, &b );
max = ( a > b ) ? a : b;
printf( “\n Maximum Value is ::: %d”, max );
return 0;
}
Output:
Enter Two Numbers ::: 23 14
Maximum Value is ::: 23