Smallest among three numbers ( else if )
So here is the code given below how to use else if conditions in C++. So here we go to the code. Just don't copy the code just go for its explanation given below the code. It may guide/help your for your learning and remembrance of the code.
Smallest among three numbers
#include<iostream.h>
#include<conio.h>
void main(){
clrscr();
int a, b, c;
cout<<"\n\t Enter number : ";
cin>>a;
cout<<"\n\t Enter number : ";
cin>>b;
cout<<"\n\t Enter number : ";
cin>>c;
if( a < b && a < c){
cout<<"\n\t Smallest number is : "<<a;
}
else if( b < c && b < a ){
cout<<"\n\t Smallest number is : "<<b;
} else {
cout<<"\n\t Smallest number is : "<<c;
}
getch();
}
Explanation of Code
- The header tags.
#include<iostream.h>
#include<conio.h>
- Then the main() function is declared.
void main()
- clrscr(); is a function use to clear the output console screen.
- int is use to initialize variables a, b & c.
int a,b;
- cout is use to display/output the entered characters in it.
cout<<"\n\t Enter number : ";
cout<<"\n\t Enter number : ";
cout<<"\n\t Enter number : ";
- cin is use to connect it with for an input value in it
cin>>a;
cin>>b;
cin>>c;
- if condition is use to compare the two values which are to be inserted. If if condition goes true it will display the out which is mentioned in it with the input value.
if ( a < b && a < c ){
cout<<"\n\t Smallest number is : "<<a;
}
- else if condition works when if condition doesn't goes true with the input values in the condition and displays the output in the else condition i.e.
else if ( b < c && b < a ){
cout<<"\n\t Smallest number is : "<<b;
}
- else condition works when if condition and else if condition doesn't goes true with the input values in the condition and displays the output in the else condition i.e.
else {
cout<<"\n\t Smallest number is : "<<c;
}
- \n it displays the output in each new line accordingly.
cout<<"\n";
- \t is a tab use to give 3 white space in an output of program accordingly.
cout<<"\t";
- getch(); is the function use to get the characters directly from the keyboard.
getch();
Screenshots :
Code