C Program to find the greatest of three numbers

In this C Programming example, we will implement the program to get the greatest number among three numbers through the user’s input and print the result on the screen.

1. Programs to find the greatest of three numbers in C

Helpful topics to understand this program better are

The program will first prompt the user to enter three integer numbers and then based on those inputs the comparison will be made among these numbers and the output will be printed on the console.

1.1. Using only if statement

Let us implement the above concept in the c program using If statement to print the greatest number among three numbers.

#include <stdio.h>

int findGreatestOf3Numbers(int a, int b, int c) {
  // if a is greater than both b and c, a is the greatest
  if (a >= b && a >= c){
    return a;
  }

  // if b is greater than both a and c, b is the greatest
  if (b >= a && b >= c){
    return b;
  }

  // if c is greater than both a and b, c is the greatest
  if (c >= a && c >= b){
    return c;
  }
  return 0; // this will never run
}

int main() {
  int a, b, c;
  printf("Enter three different numbers: ");
  scanf("%d %d %d", &a, &b, &c);

  printf("\n%d is the greatest number using only if.",
         findGreatestUsingOnlyIf(a, b, c));

  return 0;
}
Output
Enter three different numbers: 11
22
10
22 is the greatest number.

1.2. Using if-else-if Statement.

In the last section, we saw the implementation of the If statement, let us see how to implement the program using If-Else-If statement to print the greatest number among three numbers.

We will just update the method findGreatestOf3Numbers and the rest of the program is the same as before.

void findGreatestOf3Numbers(int a, int b, int c) {
  // if a is greater than both b and c than a is the greatest number.
  if ((a > b) && (a > c)) {
    return a;
  }
  // if b is greater than both a and c than b is the
  // greatest number or else c is the greatest number.
  else if ((b > c) && (b > a)) {
    return b;
  } else {
    return c;
  }
}
Output
Enter three different numbers: 11
22
10
22 is the greatest number.

1.3. Using Nested if-else statement

In this section, we will implement the same program but we will use Nested If-Else statement to print the greatest number among three numbers.

We will just update the method findGreatestOf3Numbers and the rest of the program is the same as before.

void findGreatestOf3Numbers(int a, int b, int c) {
  if (a >= b) {
    if (a >= c)
      return a;
    else
      return c;
  }
  // if b is greater than both a and c then b is the greatest number.
  // or else c will be the greater number.
  else {
    if (b >= c)
      return b;
    else
      return c;
  }
}
Output
Enter three different numbers: 11
22
10
22 is the greatest number.

2. Understanding the program’s building blocks

Let’s discuss various sections of the program in the previous section:

  • int a,b,c – declaration of a variable of type integer to hold the value of user input.
  • scanf("%d %d %d", &a, &b, &c); – Reads and stores the value of the input value as an integer. %d is the format specifier used here compiles only if the variable is of type integer.
  • Printing the output to the console using %d which again notifies that the output is an integer.
  • if, if-else-if, and Nested if-else statements are conditional operators which can also use an arithmetic expression within them.

Additional Tips

  • Similar to reading integer value we can read other numbers like long and double.
  • Use correct data type for the variable like long or double.
  • Use proper format specifier i.e.
    • %ld for long
    • %lf for double

3. Conclusion

In this C Programming example, we have discussed how to get the user’s input and find the greatest among the three numbers using different if-else combinations. We have also discussed the meaning of all the different important statements and the format specifiers used for different types of input.


Helpful Links

Please follow C Programming tutorials or the menu in the sidebar for the complete tutorial series.

Also for the example C programs please refer to C Programming Examples.

All examples are hosted on Github.

An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! šŸ˜Š

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x