DECISION CONTROL STATEMENTS
In decision control statements group of statements are executed when condition is true. If condition is false, then else part statements are executed. They are used to make one time decisions in c programming.
There are four types of decision control statements in c language:
1. Simple if statement
2. If else statement
3. Else if statement
4. Nested if Statement
Simple if statement
The if statement checks whether the text expression inside parenthesis ( ) is true or not. If the test expression is true, statements inside the body of if statement is executed but if test is false, statements inside body of if
is ignored.
1. Write a program to check the given numbers are equal.
/*************************************************************************************/
//************************************************************************************
#include < stdio.h >
#include < conio.h >
Void main ( )
{
Int m, n ;
Clrscr ( ) ;
Printf ( “ enter m, n values\n ” );
Scanf ( ”%d %d ”, &m, &n ) ;
If ( m == n )
{
Printf ( “ m, n are equal \n ” );
}
Getch ( ) ;
}
2. Write a program to check the given number is positive.
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void main ( )
{
int num ;
clrscr ( );
printf ( " enter number \n" ) ;
scanf ( " %d ", &num ) ;
If ( num > 0 )
{
Printf ( “ the given number is positive \n ” );
}
Getch ( ) ;
}
3. Write a ogram to print the given number is Zero.
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void main ( )
{
int num ;
clrscr ( );
printf ( " enter number \n" ) ;
scanf ( " %d ", &num ) ;
If ( num == 0 )
{
Printf ( “ the given number is zero \n ” );
}
Getch ( ) ;
}
6. Write a Program to Find the Largest Number among Three Numbers Entered by User.
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void main ( )
{
int a, b, c ;
clrscr ( );
printf ( " enter a,b,c values \n" ) ;
scanf("%d %d %d", &a, &b, &c);
if(a>b && a>c)
printf("Largest number = %d", a);
if(b>a && b>c)
printf("Largest number = %d", b);
if(c>a && c>b)
printf("Largest number = %d", c);
getch();
}
If else statement
In these type of statements, group of statements are executed when condition is true. If condition is false, then else part statements are executed.
1. Write a program to check the given numbers are equal or not.
/*****************************************************************/
//****************************************************************
#include < stdio.h >
#include < conio.h >
Void main ( )
{
Int m, n ;
Printf ( “ enter m, n values \n ” ) ;
Scanf ( ” %d %d ”, &m, &n) ;
Clrscr ( ) ;
If ( m == n )
Printf ( “ m, n are equal \n ” ) ;
Else
Printf ( “ m, n are not equal \n ” ) ;
Getch ( ) ;
}
2. Write a program to check whether a number entered by user is even or odd.
/*****************************************************************/
//****************************************************************
#include < stdio.h >
#include < conio.h >
Void main ( )
{
int num ;
clrscr ( ) ;
printf ( " enter number \n " ) ;
scanf ( " %d ", &num ) ;
if ( num % 2 == 0 )
printf ( " given number is even \n " ) ;
else
printf ( " given number is odd \n " ) ;
getch ( ) ;
}
3. Write a Program to Check Whether a Character is Vowel or consonant.
/*****************************************************************/
//****************************************************************
#include < stdio.h >
#include < conio.h >
Void main ( )
{
char c ;
clrscr ( ) ;
printf ( " enter character \n " ) ;
scanf ( " %c " , & c ) ;
if ( c == ' a ' | | c == ' e ' | | c == ' i ' | | c == ' o ' | | c == ' u ' )
printf ( " given character is vowel " ) ;
else
printf ( " given character is consonant " ) ;
getch ( ) ;
}
4. Write a Program to check the given number is Zero or not.
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void main ( )
{
int num ;
clrscr ( ) ;
printf ( " enter number \n" ) ;
scanf ( " %d ", &num ) ;
If ( num == 0 )
Printf ( “ the given number is zero \n ” ) ;
else
printf ( " given number is not zero " ) ;
Getch ( ) ;
}
5. Accept any two numbers then check which is the biggest.
/*****************************************************************/
//****************************************************************
#include < stdio.h >
#include < conio.h >
Void main ( )
{
Int a, b ;
Printf ( “ enter a , b values \n ” ) ;
Scanf ( ” %d %d ”, &a, &b ) ;
Clrscr ( ) ;
If ( a > b )
Printf ( “ a is greater than b ” ) ;
Else
Printf ( “ b is greater than a ” ) ;
Getch ( ) ;
}
6. Accept a number then check it is Positive or Negative.
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void main ( )
{
int num ;
clrscr ( );
printf ( " enter number \n" ) ;
scanf ( " %d ", &num ) ;
If ( num > 0 )
Printf ( “ the given number is positive \n ” ) ;
else
printf ( " the given number is negative \n " ) ;
Getch ( ) ;
}
7. C Program to Check Whether the Entered Year is Leap Year or not.
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void main ( )
{
int year ;
clrscr ( );
printf ( " enter year \n" ) ;
scanf ( " %d ", & year ) ;
If ( year % 4 == 0 )
Printf ( “ the given yearr is leap year \n ” ) ;
else
printf ( " the given year is not leap year \n " ) ;
Getch ( ) ;
}
8. C Program to Checker Whether a Character is an Alphabet or not.
/*****************************************************************/
//****************************************************************
#include < stdio.h >
#include < conio.h >
Void main ( )
{
char c ;
clrscr ( ) ;
printf ( " enter character \n " ) ;
scanf ( " %c " , & c ) ;
if ( ( c > = ' a ' && c < = ' z ' ) | | ( c > = ' A ' && c < = ' Z ' ) )
printf ( " given character is an alphabet " ) ;
else
printf ( " given character is not an alphabet " ) ;
getch ( ) ;
}
9. Accept age then check eligible to vote or not.
/*****************************************************************/
//****************************************************************
#include < stdio.h >
#include < conio.h >
Void main ( )
{
Int age ;
Printf ( “ enter age \n ” ) ;
Scanf ( ” %d ”, & age ) ;
Clrscr ( ) ;
If ( age > = 18 )
Printf ( “ eligible to vote ” ) ;
Else
Printf ( “ not eligible to vote ” ) ;
Getch ( ) ;
}
Else if statement
If condition 1 is false, then condition 2 is checked and statements are executed if it is true. If condition 2 also gets failure, then else part is executed.
Example program
/****************************************************************************************/
//***************************************************************************************
#include < stdio.h >
#include < conio.h >
Void main ( )
{
Int m, n ;
Printf ( “enter m, n values \n ” ) ;
Scanf ( ” %d %d ”, &m, &n ) ;
Clrscr ( ) ;
If ( m > n )
Printf ( “ m is greater than n. \n ” ) ;
Else if ( m < n )
Printf ( “ m is less than n. \n ” ) ;
Else
Printf ( “ m, n are equal \n ” ) ;
Getch ( ) ;
}
2. C Program to Check Whether a Number is Positive or Negative or Zero.
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void main ( )
{
int num ;
clrscr ( );
printf ( " enter number \n" ) ;
scanf ( " %d ", &num ) ;
If ( num > 0 )
Printf ( “ the given number is positive \n ” ) ;
else if ( num < 0)
printf ( " the given number is negative \n " ) ;
else
printf ( " the given number is zero \n " ) ;
Getch ( ) ;
}
3. Accept any three numbers then check which is the biggest.
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void main ( )
{
int a, b, c ;
clrscr ( );
printf ( " enter a, b, c values \ n " ) ;
scanf ( " %d %d %d ", & a, & b, & c ) ;
If ( a > b && a > c )
Printf ( “ a is greater than b,c \ n ” ) ;
else if ( b > a && b >c )
printf ( " b is greater than a, c \n " ) ;
else
printf ( " c is greater than a, b " ) ;
Getch ( ) ;
}
4. Accept a character then check it is vowel or consonant with case.
/*****************************************************************/
//****************************************************************
#include < stdio.h >
#include < conio.h >
Void main ( )
{
char c ;
clrscr ( ) ;
printf ( " enter character \n " ) ;
scanf ( " %c" , & c ) ;
if ( c == ' a ' | | c == ' e ' | | c == ' i ' | | c == ' o ' | | c == ' u ' )
printf ( " given character is vowel " ) ;
else if ( c == ' A ' | | c == ' E ' | | c == ' I ' | | c == ' O ' | | c == ' U ' )
printf ( " given character is vowel with case" ) ;
else
printf ( " given character is consonant " ) ;
getch ( ) ;
}
5. Accept age then check it is teenage or below teenage or above teenage.
/*****************************************************************/
//****************************************************************
#include < stdio.h >
#include < conio.h >
Void main ( )
{
Int age ;
Printf ( “ enter age \n ” ) ;
Scanf ( ” %d ”, & age ) ;
Clrscr ( ) ;
if ( age > = 13 && age < = 19 )
printf ( " teenage " ) ;
else if ( age < 13 )
printf ( " below teenage " ) ;
else
printf ( " above teenage " ) ;
getch ( ) ;
}
Nested if statement
We can use one if statement inside another if statements. This type of statements are called nested if statements. The nested if statement is used when program requires more than one test expression.
Example Program
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void main ( )
{
Int m , n ;
Clrscr ( ) ;
Printf ( " enter m, n values \n ” ) ;
Scanf ( ” %d %d ”, &m, &n ) ;
If ( m = = 100 )
{
/* if condition is true then check the following */
If ( n = = 200 )
{
/* if condition is true then print the following */
Printf ( " Value of m is 100 and n is 200 \n ” ) ;
}
}
Printf ( “ exact value of m is %d \n ”, m) ;
Printf ( “ exact value of n is %d \n ”, n) ;
Getch ( ) ;
}
2. Write a C program to relate two integers entered by user using = or > or < sign.
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void main ( )
{
Int m, n ;
Printf ( “enter m, n values \n ” ) ;
Scanf ( ” %d %d ”, &m, &n ) ;
Clrscr ( ) ;
If ( m = = n )
Printf ( “ Result %d = %d \n ”, m, n ) ;
Else
if ( m > n )
Printf ( “ Result %d > %d \n ”, m, n ) ;
Else
Printf ( “ Result %d < %d \n ”, m, n ) ;
Getch ( ) ;
}
3. Accept student number, name and c, c++ and java marks then find total, average and grade.
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void main ( )
{
Int sno, c, cpp, java, total ;
float avg ;
char sname[20], grade ;
Clrscr ( ) ;
Printf ( “enter student no, name \n ” ) ;
Scanf ( ” %d %s ”, &sno, &sname ) ;
Printf ( “enter 3 subject marks \n ” ) ;
Scanf ( ” %d %d %d ”, &c, &cpp, &java ) ;
total = c + cpp + java ;
average = total / 3.0 ;
if ( c > = 35 && cpp > = 35 && java > = 35 )
{
if ( average > = 60 )
grade = ' A ' ;
else if ( average > = 50 )
grade = ' B ' ;
else if ( average > = 40 )
grade = ' C ' ;
else
grade = ' P ' ;
}
else
grade = ' F ' ;
clrscr ( ) ;
printf ( " student name is : % s \n ", sname ) ;
printf ( " student total is : % d \n ", total ) ;
printf ( " student average is : % .2f \n ", average ) ;
printf ( " student grade is : % c \n ", grade ) ;
getch ( ) ;
}
4. Write a Program to Find the Largest Number among Three Numbers Entered by User.
/*******************************************************************/
//******************************************************************
# include < stdio.h >
# include < conio.h >
void main ( )
{
int a, b, c ;
clrscr ( );
printf ( " enter a, b, c values \ n " ) ;
scanf ( " %d %d %d ", & a, & b, & c ) ;
If ( a > b )
{
If ( a > c )
Printf ( “ a is greater than b,c \ n ” ) ;
else
Printf ( " c is greater than a, b " ) ;
}
else
{
if ( b > c )
Printf ( " b is greater than a, c \n " ) ;
else
printf ( " c is greater than a, b " ) ;
}
Getch ( ) ;
}