Home / C/C++ / Archive by category 'Control Structures'

Control Structures

Find bigger of 4 numbers when other 3 are equal in C

int main()
{ 
int a, b, c, d, K, M;
   printf(“Enter 4 numbers (A, B, C, D) : \n”) ;
   scanf(%d%d%d%d”, &a, &b,&c, &d) ;
   K=a+b;
   M=c+d;
   printf(“\nThe bigger number is “) ;
if(K>M)
       (a>b) ?printf(“A. \n”) : printf(“ B.\n”) ;
else
       (c>d) ?printf(“C. \n”) : printf(“ D.\n”) ;
   return 0;
}

Program to test a number for positive, negative or zero value in C

#include <stdio. h>
int main()
{ 
int x;
   printf("Enter the number to test: \n") ;
   scanf("%d",&x) ;
if(x)
  {
     printf(" \nThe input is a non-zero value. \n") ;
     if(x<0)
        printf("The number is negative. \n");
     else
  printf("The number is positive. \n") ;
   }
else
        printf("\nThe number is zero. \n") ;
return 0;
}

Program to find out of a number is odd or even using bitwise AND in C

#include <stdio. h>
int main()
{ 
int x;
   printf(“Enter the number: \n”) ;
   scanf(%d”,&x) ;
if(x&1)
       printf(“\nThe number is odd. \n”) ;
else
       printf(“\nThe number is even. \n”) ;
return 0;
}

Program to find out if a number is odd or even in C

#include <stdio. h>
int main()
{ 
int x;
   printf(“Enter the number: \n”) ;
   scanf(%d”,&x) ;
if(x%2)
   printf(“\nThe number is odd.\n”) ;
else
   printf(“\nThe number is even. \n”) ;
return 0;
}

Program to print the largest of two numbers using if-else construct in C

#include <stdio. h>
int main()
{ 
int x, y;
   printf(“Enter two numbers: \n”) ;
   scanf(%d%d”, &x,&y) ;
if(x>=y)
   printf(“\n%d is the greater number.\n”, x) ;
else
   printf(“\n%d is the greater number.\n”, y) ;
return 0;
}

Program to print the absolute value of a number in C

#include <stdio. h>
int main( )
{ 
int x;
printf( “Enter the number: \n”) ;
scanf(%d”,&x) ;
if( x<0)
	x=-x;
printf( “\nThe absolute value of the number is %d. \n”, x) ;
return 0;
}

Program to calculate grade based on marks in C

#include <stdio. h>
int main( )
{ 
int m1,m2, m3, Total;
  char Grade;
printf("Enter three marks: \n") ;
   scanf( "%d%d%d", &m1, &m2,&m3) ;
  Total=m1+m2+m3;
if( m1<=40 || m2<=40 || m3<=40)
   { 
Grade='F' ;
   }
if( m1<=60 && m2<=60 && m3<=60 && Total>120)
   { 
Grade='C' ;
   }
if( m1<=80 && m2<=80 && m3<=80 && Total>180)
   { 
Grade='B' ;
   }
if( Total > 240)
   {
  Grade='A' ;
   }
printf("\nm1=%d, m2=%d, m3=%d. \n", m1, m2,m3) ;
printf("\nTotal=%d,Grade is %c.\n", Total, Grade) ;
return 0;
}

Program to let the user guess a number between 1 and 10 in C

#include <stdio.h>
int main()
{ 
int Magic=6, Guess;
   printf("Guess a number between 1 and 10: \n") ;
   scanf("%d",&Guess) ;
if( Guess==Magic)
        printf( " \nYour guess is correct. \n") ;
if( Guess! =Magic)
        printf( " \nYou have made a wrong guess. \n") ;
return 0;
}

Program to print the largest of three numbers in C

#include <stdio.h>
int main()
{ 
int a, b, c;
   printf( "Enter three numbers: \n") ;
   scanf( "%d%d%d", &a, &b, &c) ;
if( a>=b && a>=c)
        printf( " \n%d is the largest number.\n",a) ;
if( b>=a && b>=c)
        printf( " \n%d is the largest number.\n",b) ;
if( c>=a && c>=b)
        printf( " \n%d is the largest number.\n",c) ;
return 0;
}

Program to print the largest of two numbers in C

#include <stdio.h>
int main()
{ 
int x,y;
   printf("Enter two numbers: \n") ;
   scanf("%d%d", &x,&y) ;
if( x>=y)
   printf("\n%d is the greater number.\n", x) ;
if( x<y)
   printf("\n%d is the greater number.\n", y) ;
return 0;
}

Program to illustrate if statement in C language

#include <stdio. h>
int main( )
{ 
int x, y;
  x=3;
  y=5;
  if( x<y)
   y=x++;
  printf(“\n x = %d, y = %d. \n”, x, y) ;
  return 0;
}