Wednesday, September 15, 2010

Using Nested If Else in C++

This program will teach you how to use nested if else case in c++ programming.

Following is a program to find smallest of three numbers using if else case


#include <iostream.h>
#include <conio.h>

void main()
{
int a,b,c;

cout << "You have to enter three unequal numbers and we will find the smallest for you." << endl;

cout << "\nEnter 1st number : "; // "\n" is used to go to the next line
cin >> a;

cout << "\nEnter 2nd number : ";
cin >> b;

cout << "\nEnter 3rd number : ";
cin >> c;

if(a < b)
{
if(a < c)
cout << "The smallest number is " << a;
}
else //since a is not least,only 2 options remain,either b < c or c < b
{
if(b < c)
cout << "The smallest number is " << b;
else
cout << "The smallest number is " << c;
}

getch()
}

0 comments:

Post a Comment