Adsense 728 * 15

Search

Custom Search

Find Greatest of three Numbers - C, C++ Code

Write a C, C++ program to find the greatest of three numbers. Given three input numbers, we have to write a code to find greatest of three numbers.


In this tutorial, we cover following topics.

  • Algorithm to find greatest of three numbers.
  • C Program to find greatest of three numbers.
  • C++ program to find greatest of three numbers.

Algorithm to Find Greatest of three Numbers

1. In first step, let's declare three variables to hold the values of three input numbers.

2. In next step, Compare a with b and c. If a is greater than b and c than a is greatest among three numbers.

3. Similarly, Compare b with a and c. if b is greater than a and c than b is greatest among three numbers.

 4. Compare c with a and b. If c is greater than a and b than c is greatest among three numbers.

C interview questions

Find Greatest of three Numbers in C++


We have discussed the algorithm to find greatest of three numbers. Let's write a C++ code to print the greatest number.

#include <iostream>
using namespace std;

int main() {

    int a,b,c;

    cout << "Enter three numbers";
    cin  >> a >> b >> c;

    /*check whether a is greater than b and c*/

    if(a > b  && a > c) {
  
       cout<<"Greatest number is"<<a;
 
    /* check whether b is greater than a and c */
    } else if( b > a && b > c) {

       cout << "Greatest number is "<<b;
   
    } else {
 
     cout << "Greatest number is "<<c;

  }

   return 0;
 
}


Find  Greatest of three Numbers in C

#include <stdio.h>

int main(){
    
    int a, b, c;
    
    printf("Enter three numbers ");
    scanf("%d %d %d", &a, &b, &c);
    
    if (a > b && a > c) {
        printf("%d is Greatest",a);
    }
    else if (b > a && b > c) {
        printf("%d is Greatest",b);
    }
    else {
        printf("%d is Greatest",c);
    }
    
    return 0;
}


Output :

Enter three numbers :  8  2   1

8 is Greatest

Program to find second largest element in an array

Find largest element in an array

Find sum of fibonacci series in c, c++

No comments:

Post a Comment