Recent

recentposts

If-Else

 If Statements

Now suppose that user wants to check certain condition and if the conditions are fulfilled then certain statement should execute otherwise other statement should get executed. For this purpose, if-else statement were introduced.

The selection construct (if-else) mean that execution of statement depending upon a condition-test.

An if statement tests a particular condition, If the condition evaluates to true, a course-of-action is followed or executed , otherwise the other course-of-action gets executed.

Syntax:

if( condition )

{ statement1 ; }

else

{statement2 ; } 

 



 


Program to find whether the number input by user is positive or negative.

#include<iostream.h>

#include<conio.h>


void main(){


clrscr();                                


int numb ;


cout<<" Enter the number = " ;

cin>>numb ;


if( numb >=0 )

{   cout<<"The number is positive" ;    }

else

{  cout<<"The number is negative" ;   }


getch() ;

}

 

Program to find whether the number input by user is completely divisible by 3 or not .

#include<iostream.h>

#include<conio.h>


void main(){


clrscr() ;                                


int numb, remains ;


cout<<" Enter the number = " ;

cin>>numb ;


remains = numb%3 ;


if ( remains==0 )

{   cout<<" The number is completely divisible " ;   }

else

{   cout<<" The number is not completely divisible " ;   }


getch() ;

}

 

Program to find the average marks of 5 subjects and determine the grade if  70< average <80 grade C and 80< average <90 grade B and 90< average <100 grade A else grade D.

#include<iostream.h>

#include<conio.h>


void main(){


clrscr() ;                                      


int mark1, mark2, mark3, mark4, mark5 ;

float average ;                   

/* float is used for storing decimal value since average can have decimal part   */


cout<<" Enter the 1st mark = " ;

cin>>mark1 ;

cout<<" Enter the 2nd mark = " ;

cin>>mark2 ;

cout<<" Enter the 3rd mark = " ;

cin>>mark3 ;

cout<<" Enter the 4th mark = " ;

cin>>mark4 ;

cout<<" Enter the 5th mark = " ;

cin>>mark5 ;


average = ( mark1 + mark2 + mark3 + mark4 + mark5 ) / 5 ;

if( average>70 && average<80 )

{   cout<<"Grade = C " ;   }

else if ( average >80 && average <90 )

{   cout<<"Grade = B " ;   }

else if ( average >90 && average <100 )

{   cout<<"Grade = A " ;   }

else

{    cout<<"Grade = D " ;   }


getch() ;

}

 

Program to enter the age of a person and identify whether he/she can vote or  not.

#include<iostream.h>

#include<conio.h>


void main(){


int age ;


cout<<"Enter the age of the person  = " ;

cin>>age ;


if ( age >= 18 )

 {   cout<<"He/she can vote " ;   }

else

{   cout<<"He/she cannot vote " ;   }


getch() ;

}

 

Program to input all the angles of a triangle and then find whether they can form a triangle or not .

#include<iostream.h>

#include<conio.h>


void main(){


clrscr();


int ang1, ang2, ang3, total_angle ;


cout<<" Enter the 1st angle = " ;

cin>>ang1 ;

cout<<" Enter the 2nd angle = " ;

cin>>ang2 ;

cout<<" Enter the 3rd angle = " ;

cin>>ang3 ;


total_angle = ( ang1 + ang2 + ang3 ) ;


if ( total_angle == 180 )

 {   cout<<"The triangle can be formed " ;   }

else

{   cout<<" The triangle cannot be formed " ;   }


getch() ;

}

Program to input two numbers and if both are less than 0 , print ‘both negative’, if both are greater than 0 , print ‘both positive’, and if one less than 0 and other is greater than 0 then print ‘one is negative and other is positive’.

#include<iostream.h>

#include<conio.h>


void main(){


clrscr() ;

int numb1 , numb2 ;


cout<<" Enter 1st number = " ;

cin>>numb1 ;

cout<<" Enter 2nd number = " ;

cin>>numb2 ;


if ( numb1 < 0 && numb2 < 0 )

   {   cout<<" both negative  " ;   }

else if (numb1 > 0 && numb2 > 0)

   {   cout<<" both positive  " ;   }

else if((numb1 > 0 && numb2 < 0) || (numb1 < 0 && numb2 > 0))

   {   cout<<" one is negative and other is positive  " ;   }


getch() ;

}

_____________________________________________________________

SHARE

Milan Tomic

Hi. I’m Designer of Blog Magic. I’m CEO/Founder of ThemeXpose. I’m Creative Art Director, Web Designer, UI/UX Designer, Interaction Designer, Industrial Designer, Web Developer, Business Enthusiast, StartUp Enthusiast, Speaker, Writer and Photographer. Inspired to make things looks better.

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment

Please give your valuable suggestions