Google Add

Search

Program to Find Largest of Three Numbers - C, C++ Code


  • Write a program to find largest of three numbers.
  • C program to find largest of three numbers.
  • C++ program to find largest of three numbers.

In this program, We'll take three input numbers and print largest among three numbers. To solve this problem, first let's write an algorithm to find largest number among three numbers.

Program to Find Largest of Three Numbers


Algorithm to Find Largest of Three Numbers



 1. Declare three variable a ,b, c.

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

 3. 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.

Program to find second largest element in an array

Find largest element in an array


C Program to Find Largest of  Three Numbers


We have discussed the algorithm to find largest among three numbers. Let's write a c code to find largest among three input numbers.

#include <stdio.h>
 
int main() {
 
      int a, b, c;
 
      printf("Enter three numbers: ");
      scanf("%d %d %d", &a, &b, &c);
 
      //If a is greater than b and c

      if(a >= b && a >= c){
         printf("Largest number is %d", a);
 
      //if b is greater than a and c

      }else if(b >= a && b >= c){
         printf("Largest number is %d", b);
 
      //If c is greater than a and b

      }else if(c >= a && c >= b){
         printf("Largest number is %d", c);
 
      } 
      return 0;
}

Output -

Enter three numbers :

14
34
23

Largest number is : 34


C Program Find Largest of three Numbers using Ternary Operator


In this programming example, We are going to use conditional operator to print largest of three numbers.
#include <stdio.h>

int main(void) {
 
    int a, b, c, larg;
 
    printf("Enter three numbers \n");
    scanf("%d %d %d", &a , &b , &c);
 
 
    larg =  (a > b && a > c) ? a : b > c ? b : c ;
 
    printf("Largest number is %d ", larg);

 
    return 0;
}


C++ Program Find Largest of three Numbers


We have written a code, Let's write a C++ code to print larges among three numbers.
#include <iostream>
using namespace std;

int main() {
 
    int a, b, c;
 
    cout << "Enter three numbers \n";

    /* Taking input */
    cin >> a >> b >> c;
 
    /* If a is greater than b and c. */

    if (a > b && a > c) {
       cout << "Largest number is " << a;

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

    } else {
       cout << "Largest number is "<< c;

    }
 
     return 0;
}




Programming question on Arrays

Programming question on Linked List

Sorting algorithm and their time complexity

Stack implementation

Programming questions using Recursion

C, C++ interview questions

Programming Books

No comments:

Post a Comment