EXAMPLES OF CONTROL STATEMENT

WAP to read a number .print whether it is odd or even without using % symbol.

No/2*2==no

8/2*2==8 True

7/2*2==7 False  

#include<stdio.h>

void main()

{

  int no;

  printf(“Enter a number :”);

  scanf(“%d”,&no);

  if( no/2*2==no )

  printf(“Even number”);

  else

  printf(“Odd number”);

}   

Output 1: Enter a number: 15 Odd number

Output2: Enter a number: 20 Even number

WAP to read a number. Print whether it is +ve number or –ve number.

#include<stdio.h>

void main()

{

  int no;

  printf(“Enter a number :”);

  scanf(“%d”,&no);

  if(no<0)

  printf(“-ve number”);

  else

  printf(“+ve number”);

Output 1: Enter a number: 10 +ve number

Output 2: Enter a number: -5 -ve number

WAP to read a number print whether it is +ve or –ve without comparing with 0.

#include<stdio.h>

void main()

{

  int no;

  printf(“enter a number :”);

  scanf(“%d”,&no);

  if( no==abs(no))

  printf(“+ve number”);

  else

  printf(“-ve number”);

}   

Output 1: enter a number : 10 +ve number

Output 2 : enter a number : -7 -ve number

Explanation:

No=10

abs(no)=10

Example 2:

No=-10

Abs(no) = 10

WAP to read purchase rate, sales rate. Print whether it is profit or loss.

#include<stdio.h>

void main()

{

  int pr,sr;

  printf(“Enter pr,sr :”);

  scanf(“%d%d”,&pr,&sr);

  if( sr > pr )

  printf(“Profit=%d”,sr-pr);

  else

  printf(“Loss=%d”,pr-sr);

}  

Output 1: Enter pr,sr: 150 80

Loss=70

Output 2: Enter pr,sr: 200 370

Profit=170

WAP to read a char. Print whether it lower case or not.

#include<stdio.h>

void main()

{

  char ch;

  printf(“Enter a char :”);

  scanf(“%c”,&ch);

  if( ch>='a' && ch<='z' )

  printf(“Lower case”);

  else

  printf(“Not a lower case”);

}  

Output 1: Enter a char: a lower case

output 2: Enter a char: A Not a lower case.

Output3: Enter a char: + Not a lower case

WAP to read a char. Print whether it is upper case or not.

#include<stdio.h>

void main()

{

  char ch;

  printf(“Enter a char :”);

  scanf(“ %c”,&ch);

  if( ch>='A' && ch<='Z' )

  printf(“Upper case”);

  else

  printf(“Not a Upper case”);

}   

Output 1: Enter a char: a Not a upper case

output 2: Enter a char: A Upper case.

Output3: Enter a char: + Not a upper case

WAP to read a char. Print whether it is digit or not.

#include<stdio.h>

void main()

{

  char ch;

  printf(“Enter a digit :”);

  scanf(“ %c”,&ch);

  if( ch>='0' && ch<='9' )

  printf(“Digit”);

  else

  printf(“Not a Digit”);

}   

Output 1: Enter a digit: 5 Digit

output 2: Enter a digit: A Not a digit.

WAP to read 2 numbers. Print whether they are equal or not.

#include<stdio.h>

void main()

{

  int a,b;

  printf(“Enter 2 numbers :”);

  scanf(“%d%d”,&a,&b);

  if( a==b )

  printf(“Both are equal”);

  else

  printf(“Both are not equal”);

}  

Output1: Enter 2 numbers: 10 20 Both are not equal

Output2: Enter 2 numbers : 20 20 Both are equal

WAP to read 2 numbers. Print whether they are equal or not without using == operator.

#include<stdio.h>

void main()

{

  int a,b;

  printf(“Enter 2 numbers :”);

  scanf(“%d%d”,&a,&b);

  if( a-b )

  printf(“Both are not equal”);

  else

  printf(“Both are equal”);

}   

Output1 : Enter 2 numbers : 10 20 Both are not equal

Explanation :

If( a-b)

If( 10-20)

If( -10 )

If( True )

Output2 : Enter 2 numbers : 20 20 Both are equal

Explanation :

If( a-b)

If( 20-20)

If( 0 )

If( False )

WAP to read marks in 3 subjects. Print whether the student is pass or fail.

#include<stdio.h>

void main()

{

  int m1,m2,m3;

  printf(“Enter marks in 3 subjects :”);

  scanf(“%d%d%d”,&m1,&m2,&m3);

  if( m1>=35 && m2>=35 && m3>=35 )

  printf(“Pass”);

  else

  printf(“Fail”);

}   

Output1: Enter marks in 3 subjects : 90 80 70 Pass.

Output2: Enter marks in 3 subjects : 40 50 20 Fail .

WAP to read a character. Print whether it is vowel or consonant.

#include<stdio.h>

void main()

{

  char ch;

  printf(“Enter a char :”);

  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(“Vowel”);

  else

  printf(“Consonant”);

}                               

Output1 : Enter a char : a Vowel

Output2 : Enter a char : b Consonant

WAP to read 3 sides of a triangle. Print whether we can draw the triangle or not.

#include<stdio.h>

void main()

{

  int a,b,c;

  printf(“Enter 3 sides of a triangle :”);

  scanf(“%d%d%d”,&a,&b,&c);

  if( a+b>c && b+c>a && a+c>b )

  printf(“We can draw the triangle”);

  else

  printf(“We can not draw the triangle”);

}  

Output1 : Enter 3 sides of a triangle : 4 5 6 We can draw the triangle

Output 2 : Enter 3 sides of a triangle : 1 2 3 We can not draw the triangle

WAP to read temp of water. Print its form.

#include<stdio.h>

void main()

{

  int temp;

  printf(“Enter temp of water :”);

  scanf(“%d”,&temp);

  if( temp <= 0)

  printf(“ICE”);

  else if( temp<100)

  printf(“water”);

  else

  printf(“steam”);

}  

 

WAP to read marks in 3 subjects. Print the grade.

#include<stdio.h>

void main()

{

  int m1,m2,m3,avg;

  printf(“Enter 3 subjects marks :”);

  scanf(“%d%d%d”,&m1,&m2,&m3);

  avg=(m1+m2+m3)/3;

  if( m1>=35 && m2>=35 && m3>=35 )

{

  if( avg>=60 )

  printf(“I Class”);

  else if( avg>=50 )

  printf(“II Class”);

  else

  printf(“III Class”);

}

  else

  printf(“Fail”);

}   

Output1 : Enter 3 subjects marks : 100 100 20 Fail

Output 2: Enter 3 subjects marks : 35 35 35 III Class