Wednesday, 25 May 2016

USING IF-ELSE-IF Ladder STATEMENT

//THIS PROGRAM ILLUSTRATES THE USE OF IF STATEMENTS IN C++(USEFUL IN HSC)

NOW IF STATEMENT IS A CONDITIONAL STATEMENT IN C++ i.e WHERE YOU WANT TO MAKE ANY CHOICE IN RUN TIME(i.e DURING EXECUTION OF PROGRAM).

LETS SEE AN EASY EXAMPLE TO COMPARE THREE NUMBER ENTERED BY THE USER. FINDING OUT WHICH ONE IS LARGEST AMONG THEM


#include<iostream.h>

#include<conio.h>

 void main()

{

clrscr();

int a,b,c;                      \\this is variable declartion

cout<<"Enter three numbers";     \\Print the content as it is on output screen

cin>>a;

cin>>b;

cin>>c;              \\you can also take input in one step as cin>>a>>b>>c;



if(a>b && a>c)       \\if statement... Now here we have 2 condition to check i.e

                       \\ a>b and a>c. In c++ and is written as two emphasand symbol.

{

cout<<a<<"Is the largest number"; \\be clever and think like a programmer

}

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

{

cout<<b<<"Is the largest number";

}

else

{

cout<<c<<"Is the largest number";

}

getch();

}

No comments:

Post a Comment