Tuesday 13 January 2015

Cpp Program to find the biggest ans smallest number in 3 numbers


This is Cpp Program which finds the biggest and smallest number of the given three numbers.

We use 2 functions biggest() and smallest() to calculate the biggest and smallest number of the 3 numbers.

for biggest number.
check first number(a) is greater than second number(b) and third number(c)
if it is true , a is greater than the 3 numbers else
check second number(b) is greater than first number(a) and third number (c)
if it is true , b is  greater than the 3 numbers else
obviously c is greater than the 2 numbers

for smallest number,
check first number(a) is lesser than second number(b) and third number(c)
if it is true , a is lesser than the 3 numbers else
check second number(b) is lesser than first number(a) and third number (c)
if it is true , b is  lesser than the 3 numbers else
obviously c is lesser than the 3 numbers

PROGRAM :
#include <iostream>
using namespace std;

int biggestNumber(int num1, int num2,int num3);
int smallestNumber(int num1, int num2,int num3);

int main()
{
    int num1,num2,num3;
    cout << "Enter 1st number :\t" ;
    cin >> num1;
    cout << " \n Enter 2nd number :\t" ;
    cin >> num2;
    cout << " \nEnter 3rd number :\t" ;
    cin >> num3;
    cout << endl;

    biggestNumber(num1,num2,num3);
    smallestNumber(num1,num2,num3);

    return 0;
}

int biggestNumber(int num1, int num2,int num3){

    if(num1 > num2 && num1 > num3)
        cout << num1 << " is the largest of the 3 numbers" << endl;
    else if (num2>num1 && num2>num3)
        cout << num2 << " is the largest of the 3 numbers" << endl;
    else
        cout << num3 << " is the largest of the 3 numbers" << endl;
}
int smallestNumber(int num1, int num2,int num3){

    if(num1 < num2 && num1 < num3)
        cout << num1 << " is the smallest of the 3 numbers" << endl;
    else if (num2<num1 && num2<num3)
        cout << num2 << " is the smallest of the 3 numbers" << endl;
    else
        cout << num3 << " is the smallest of the 3 numbers" << endl;
}

OUTPUT : 
Cpp - Biggest and Smallest number of 3 numbers

No comments:

Post a Comment