C Programming – If Statements – Examples

Examples of IF Statements

Click the following Links to go Solutions of Programs :

  • 1.1 To Check Eligible to Vote – I
  • 1.2 To Check Eligible to Vote – II
  • 1.3 To Check Eligible to Vote – III
  • 2.1 Biggest of Two numbers – I
  • 2.2 Biggest of Two numbers – II
  • 2.3 Biggest of Two numbers – III
  • 3.1 Biggest of Three numbers – I
  • 3.2 Biggest of Three numbers – II
  • 3.3 Biggest of Three numbers – II
  • 4.1 To check given value is positive,negative or zero value.
  • 5.1 To check given number is Even or Odd number
  • 6.1 To Calculate the absolute value of an integer
  • 7.1 Leap Year or Not
  • 8.1 To categorizes a single character that is entered at the keyboard.
  • 9.1 To Evaluate simple expression of the following form – I : Number operator Number For Ex : See Following Output
  • Type in your expression : 10+20
  • 30.00
  • Ex:9.2 To Evaluate simple expression of the following form -II : Number operator Number

Example : 1.1 To Check Eligible to Vote

#include<stdio.h>
#include<conio.h>
void main()
{
int age;
clrscr();
printf("\nEnter Your Age : ");
scanf("%d",&age);

if(age>=18)
   printf("\nAge is greater than or equal to 18");

getch();
}

Output : 1

Enter Your Age : 30
Age is greater than or equal to 18

Output : 2 (No Result)

Enter Your Age : 10


Example : 1.2 To Check Eligible to Vote

#include<stdio.h>
#include<conio.h>
void main()
{
int age;
clrscr();
printf("\nEnter Your Age : ");
scanf("%d",&age);

if(age>=18)
{
   printf("\nAge is greater than or equal to 18");
   printf("\nThis Age Eligible to Vote");
}

getch();
}

Output : 1

Enter Your Age : 30
Age is greater than or equal to 18
This Age Eligible to Vote

Output : 2 (No Result)

Enter Your Age : 10


Example : 1.3 To Check Eligible to Vote

#include<stdio.h>
#include<conio.h>
void main()
{
int age;
clrscr();
printf("\nEnter Your Age : ");
scanf("%d",&age);

if(age>=18)
   printf("\nEligible to Vote");
else
   printf("\nNot Eligible to Vote");

getch();
}

Output : 1

Enter Your Age : 30
Age is greater than or equal to 18
Eligible to Vote

Output : 2 (No Result)

Enter Your Age : 10
Not Eligible to vote


Example : 2.1 Biggest of Two Numbers

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("\nEnter Value of A ");
scanf("%d",&a);

printf("\nEnter Value of B ");
scanf("%d",&b);

if(a>b)
   printf("\n A is Big");
else
   printf("\n B is Big");

getch();
}

Output : 1

Enter value of A 10
Enter value of B 20
A is big

Output : 2 (Result is Wrong – To Fix This Problem – See Next Example 2.2)

Enter value of A 10
Enter value of B 10
B is big


Go Top

Example : 2.2 Biggest of Two Numbers (Change below Code in Previous Code ( Ex 2.1 )

if(a>b)
   printf("\nA is Big");
if(b>a)
   printf("\nB is Big");

(Or)

if(a>b)
   printf("\nA is Big");
else if(b>a)
   printf("\nB is Big");

Output : 1

Enter value of A 10
Enter value of b 20
B is big

Output : 2 (To print message A & B are equal – See the below Example 2.3

Enter value of A 10
Enter value of B 10


Example : 2.3 Biggest of Two Numbers (Change below Code in Previous Code (Ex 2.1 )

if(a>b)
   printf("\nA is Big");
if(b>a)
   printf("\nB is Big");
else if(a==b)
   printf("\nA & B are Equal");

Output : 1

Enter value of A 10
Enter value of b 20
B is big

Output : 2

Enter value of A 10
Enter value of B 10
A & B are Equal


Example : 3.1 Biggest of Three Numbers

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("\nEnter Value of A ");
scanf("%d",&a);
printf("\nEnter Value of B ");
scanf("%d",&b);
printf("\nEnter Value of C ");
scanf("%d",&c);

if(a>b && a>c)
   printf("\n%d is Big",a);
else if (b>a && b>c)
   printf("\n%d is Big",b);
else if (c>a && c>b)
   printf("\n%d is Big",c);

getch();
}

Output : 1

Enter Value of A 2
Enter Value of B 3
Enter Value of C 4
4 is Big

Output : 2 (A & C Values are less than B.See the Next Output )

Enter Value of A 3
Enter Value of B 4
Enter Value of C 3
4 is Big

Output : 3 (A & C Values are greater than B. To print message A & C is big – See [Ex 3.2])

Enter Value of A 3
Enter Value of B 2
Enter Value of C 3

Output : 4 (To print message 3 values are Equal – See [Ex 3.2])

Enter Value of A 3
Enter Value of B 3
Enter Value of C 3


Example : 3.2 Biggest of Three Numbers (Change below code in Previous Code[(Ex 3.1 ] )

if(a>b && a>c)
   printf("\n%d is Big",a);
else if (b>a && b>c)
   printf("\n%d is Big",b);
else if (c>a && c>b)
   printf("\n%d is Big",c);
else
   printf("\n3 Values are Equal");

[or]

if(a>b && a>c)
   printf("\n%d is Big",a);
else if (b>a && b>c)
   printf("\n%d is Big",b);
else if (c>a && c>b)
   printf("\n%d is Big",c);
else if(a==b && b==c)
   printf("\n3 Values are Equal");

Output : 1

Enter Value of A 3
Enter Value of B 3
Enter Value of C 3
3 Values are Equal

Output : 2 (A & C Values are greater than B. To print message A & C is big – See [Ex 3.3])

Enter Value of A 3
Enter Value of B 2
Enter Value of C 3


Example : 3.3 Biggest of Three Numbers (Finally)

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“\nEnter Value of A “);
scanf(“%d”,&a);
printf(“\nEnter Value of B “);
scanf(“%d”,&b);
printf(“\nEnter Value of C “);
scanf(“%d”,&c);

if(a==c && b<a)
    printf(“\nA & C Values are Big “);
else if(a==b && c<a)
   printf(“\nA & B Values are Big”);
else if(b==c && a<b)
   printf(“\nB & C Values are Big”);
else if(a==b && b==c)
   printf(“\n3 Values are Equal.”);
else if(a>b && a>c)
   printf(“\nA (%d) is Big”,a);
else if (b>a && b>c)
   printf(“\nB (%d) is Big”,b);
else if (c>a && c>b)
   printf(“\nC (%d) is Big”,c);

getch();
}

Output : 1

Enter Value of A 2
Enter Value of B 3
Enter Value of C 2
B (3) is Big

Output : 2

Enter Value of A 3
Enter Value of B 2
Enter Value of C 3
A & C values are Big


Example : 3.1 Biggest of Three Numbers

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“\nEnter Value of A “);
scanf(“%d”,&a);
printf(“\nEnter Value of B “);
scanf(“%d”,&b);
printf(“\nEnter Value of C “);
scanf(“%d”,&c);

if(a>b && a>c)
   printf(“\n%d is Big”,a);
else if (b>a && b>c)
   printf(“\n%d is Big”,b);
else if (c>a && c>b)
   printf(“\n%d is Big”,c);

getch();
}

Output : 1

Enter Value of A 2
Enter Value of B 3
Enter Value of C 4
4 is Big

Output : 2 (A & C Values are less than B.See the Next Output )

Enter Value of A 3
Enter Value of B 4
Enter Value of C 3
4 is Big

Output : 3 (A & C Values are greater than B. To print message A & C is big – See [Ex 3.2])

Enter Value of A 3
Enter Value of B 2
Enter Value of C 3

Output : 4 (To print message 3 values are Equal – See [Ex 3.2])

Enter Value of A 3
Enter Value of B 3
Enter Value of C 3


Example : 3.2 Biggest of Three Numbers (Change below code in Previous Code[(Ex 3.1 ] )

if(a>b && a>c)
   printf(“\n%d is Big”,a);
else if (b>a && b>c)
   printf(“\n%d is Big”,b);
else if (c>a && c>b)
   printf(“\n%d is Big”,c);
else
   printf(“\n3 Values are Equal”);

[or]

if(a>b && a>c)
   printf(“\n%d is Big”,a);
else if (b>a && b>c)
   printf(“\n%d is Big”,b);
else if (c>a && c>b)
   printf(“\n%d is Big”,c);
else if(a==b && b==c)
   printf(“\n3 Values are Equal”);

Output : 1

Enter Value of A 3
Enter Value of B 3
Enter Value of C 3
3 Values are Equal

Output : 2 (A & C Values are greater than B. To print message A & C is big – See [Ex 3.3])

Enter Value of A 3
Enter Value of B 2
Enter Value of C 3


5.1 To check given number is Even or Odd number

#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf(“\nEnter any Value “);
scanf(“%d”,&n);

if(n%2==0)
   printf(“\n%d is Even Number “,n);
else if (n%2==1)
   printf(“\n%d is Odd Number”,n);

getch();
}

Output : 1

Enter any Value 3
3 is Odd Number


6.1 To Calculate the absolute value of an integer

#include<stdio.h>
#include<conio.h>
void main()
{
int number;
clrscr();
printf (“Enter any Number “);
scanf (“%d”, &number);

if ( number < 0 )    number= – number;

printf (“\n The absolute value is %d”,number);
getch();
}

Output : 1

Enter any Number 2
The absolute value is 2

Output : 2

Enter any Number -4
The absolute value is 4


Ex:7.1 Leap Year or Not

#include<stdio.h>
#include<conio.h>
void main()
{
int year,reminder;
clrscr();
printf (“Enter the Year “);
scanf (“%d”,&year);

//if(year%4==0 && year%4!=0 || year % 400==0)
//if((year%4==0 && year%4!=0) || year % 400==0)
if(reminder==0)
   printf(“\nIt’s a Leap Year”);
else
   printf(“\nIt’s not a Leap Year”);

getch();
}

Output : 1

Enter The year 2010
It’s a Leap Year.


Ex:8.1 to Categorizes a Single Character that is entered at the keyboard.

#include<stdio.h>
#include<conio.h>
void main()
{
char c;
clrscr()
printf(“\nEnter a Single Character > “);
scanf(“%c”,&c);

if ( (c >= ‘a’ && c <= ‘z’) || (c >= ‘A’ && c <=’Z’) )    printf (“It’s an alphabetic character.\n”); else if (c >= ‘0’ && c <= ‘9’ )    printf (“It’s a digit.\n”); else    printf (“It’s a special character.\n”);

getch();
}

Output : 1

Enter a Single Character > a
It’s an alphabetic character.

Output : 2

Enter a Single Character > 2
It’s a digit.

Output : 3

Enter a Single Character > &
It’s a special character.


Ex:9.1 To Evaluate simple expression of the following form – I : Number operator Number

#include<stdio.h>
#include<conio.h>
void main()
{
float number1,number2;
char operator;
printf (“\nType in your expression : “);
scanf(“%f %c %f”,&number1,operator,&number2);

if ( operator == ‘+’ )
   printf (“\n%.2f “, number1+number2);
else if ( operator == ‘-‘ )
   printf (“\n%.2f “, number1-number2);
else if ( operator == ‘*’ )
   printf (“\n%.2f “, number1*number2);
else if ( operator == ‘/’ )
   printf (“\n%.2f “, number1/number2);

getch();
}

Output : 1

Type in your expression : 10+20
30.00

Output : 2

Type in your expression : 123.45*121
14937.45

Output : 3 (To Fix following Output See Next Example )

Type in your expression : 5/0
(Program terminated.Because Division by Zero Error)


Ex:9.2 To Evaluate simple expression of the following form -II : Number operator Number

#include<stdio.h>
#include<conio.h>
void main()
{
float number1,number2;
char operator;
printf (“\nType in your expression : “);
scanf(“%f %c %f”,&number1,operator,&number2);

if ( operator == ‘+’ )
   printf (“\n%.2f “, number1+number2);
else if ( operator == ‘-‘ )
   printf (“\n%.2f “, number1-number2);
else if ( operator == ‘*’ )
   printf (“\n%.2f “, number1*number2);
else if ( operator == ‘/’ )
else if ( operator == ‘/’ )
{
  if(number2==0)
     printf(“\nCannot divided by zero “);
  else
     printf (“\n%.2f “, number1/number2);
}
else
   printf(“\nUnknown Operator.);

getch();
}

Output : 1

Type in your expression : 5/0
Cannot divided by zero

Output : 2

Type in your expression : 5$0
Unknown Operator


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *