Algorithm and Flowchart to find Largest of Three Numbers

[64812 views]




There are various methods to find the largest number. Like you can use the maximum function of the programming language directly but the main thing here is understanding how you solve any problem or what is the logic behind the problem and its time complexity. Everyone seeks the shortest time to solve a problem for efficient performance. Coding is all about brainstorming and what was your logic behind any problem. The more you go through problems the better you will be at it.

To solve this problem, you can use only If statements or If and else or nested If and else or using Ternary Operator. These are all of the same type comparing the numbers which each other. The only difference is the way of writing. I will be using the Ternary operator as many of them don't use it but is very useful as with few lines you can write the program.

In this algorithm, we will be comparing two numbers. If the first number is greater then first number will be compared with the third number whichever number is greater print that. If the first number is smaller then compare second number with the third number, whichever is greater among two print that number.

Flowchart for Largest of three numbers:

Flowchart for largest of three numbers
Remove WaterMark from Above Flowchart

Pseudocode for largest of three numbers:

Declare a variable a, b, c and largest as integer; Read the number a, b and c; max = a > b ? (a > c ? a : c) : (b > c ? b : c); print max;

In this algorithm we declare four variables a, b and c for reading the numbers and largest for storing it. Then read the three variables. Then we use Ternary operator before question mark condition is given. If it is true then the condition before ":" it is taken otherwise after that. a > b? (a > c? a: c): (b > c? b: c). In this first we compare a > b. If a is greater then (a > c? a: c) will be followed otherwise (b > c? b: c). Let us take both the cases where

  • a is greater
  • If a is greater then (a > c? a: c) this is followed. Now also there will be two case a is smaller than c or a is greater than c. If a is greater than c then a will be stored in the LHS. If a is smaller than c will be stored.
  • a is smaller
  • Similarly in this case (b > c? b: c) will be followed. If b is greater then b will be stored in the LHS. If c is greater then c will be stored in the LHS.

At last we print the value of Max.

Note: If all the three numbers are equal then it will print zero as this condition is not stated. We have declared the number as int so, this algorithm will be valid up to the range of int.

Implementation of largest of Three numbers in C

#include<stdio.h> int main() { int a, b, c, max; printf("Enter three numbers:"); printf("\na:"); scanf("%d", & a); printf("b:"); scanf("%d", & b); printf("c:"); scanf("%d", & c); max = a > b ? (a > b ? a : c) : (b > c ? b : c); printf("%d is the largest number.", max); }

Output of the program

C Program for Largest of 3 numbers
                 



Want to Learn How to write own Algorithm and Flowcharts



Want to test your logical skills in Algorithms?




Comments

4 comment
  • k n v srinivas

    IN program logic is wrong . max = a > b ? (a > b ? a : c) : (b > c ? b : c);

  • k n v srinivas

    Note: If all the three numbers are equal then it prints the maximum number. For example input: 30 30 30 Output: 30

  • Shaddy

    No Srinivas, The algorithm is still correct if you pass same number 3 times. Let's say input is 30, 30, 30. So the Maximum among all those will be 30 as their are no other values provided to compare.

  • KRITHIKA S.B

    Nicest explanation ever i got










Search Anything:

Sponsored Deals ends in





Technical Quizzes Specially For You:

Search Tags

    Pseudocode for finding largest of 3 numbers

    Greatest of Three Numbers Algorithm

    Algorithm to find Maximum of Three Numbers

    Algorithm to find Largest of 3 numbers