Program to Find the Greatest of Three Numbers in C | Learn Coding

29 Dec 2022 Balmiki Kumar 0 C Programming

C Program to Find the Greatest of Three Numbers

The C program to find the greatest of three numbers is a simple program that first declares three variables, a, b, and c, to store the three numbers. Then, it prompts the user to enter the three numbers. Next, it compares a to b and c and assigns the greatest number to greatest. Finally, it prints the value of greatest.

Here is a breakdown of the program:

C Programming
#include <stdio.h>

int main() {
  int a, b, c;

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

  int greatest = a;
  if (b > greatest) {
    greatest = b;
  }
  if (c > greatest) {
    greatest = c;
  }

  printf("The greatest number is %d\n", greatest);

  return 0;
}

Output of this program:

 

Enter three numbers: 10 20 30
The greatest number is 30

Solution: The #include <stdio.h> statement tells the compiler to include the stdio.h header file, which contains the declarations for the printf() and scanf() functions.The int main() { statement defines the main() function, which is the entry point for the program. The printf("Enter three numbers: "); statement prints a message to the console prompting the user to enter three numbers. The scanf("%d %d %d", &a, &b, &c); statement reads three numbers from the console and stores them in the variables a, b, and c. The int greatest = a; statement assigns the value of a to the variable greatest. The if (b > greatest) { statement checks if the value of b is greater than the value of greatest. If it is, the greatest variable is assigned the value of b. The if (c > greatest) { statement checks if the value of c is greater than the value of greatest. If it is, the greatest variable is assigned the value of c. The printf("The greatest number is %d\n", greatest); statement prints the message "The greatest number is greatest" to the console. The return 0; statement tells the compiler that the main() function has completed successfully.

 

C Program to Find the Greatest of Three Numbers using if else

#include<stdio.h>
int main(){
 int a, b, c;
 printf("Enter a,b,c: \n");
 scanf("%d %d %d", &a, &b, &c);
 if (a > b && a > c) {
 printf("a is Greater than b and c");
 }
 else if (b > a && b > c) {
 printf("b is Greater than a and c");
 }
 else if (c > a && c > b) {
 printf("c is Greater than a and b");
 }
 else {
 printf("all are equal or any two values are equal");
 }
 return 0;
}

Output:

Enter a,b,c: 3 5 8
c is Greater than a and b 

BY: Balmiki Kumar

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.