In this C program, we will learn about how to read three numbers and find maximum of three numbers using if else statement. We will first take three numbers as input from user using scanf function. Then we print the maximum of three numbers on screen.
Required Knowledge
C program to find maximum of three numbers using if-else statement
It first finds largest of first two numbers and then compares it with third number.
#include <stdio.h> int main() { int a, b, c, max; printf("Enter Three Integers\n"); scanf("%d %d %d", &a, &b, &c); if(a > b){ if(a > c) max = a; else max = c; } else { if(b > c) max = b; else max = c; } /* Print Maximum Number */ printf("Maximum Number = %d\n", max); return 0; }Output
Enter Three Integers 2 8 4 Maximum Number = 8
C program to find largest of three numbers using function
Function getMax takes two numbers as input and returns the largest of two numbers. We will use this function to find largest of three numbers as follows:
#include <stdio.h> int getMax(int num1, int num2) { if (num1 > num2){ return num1; } else { return num2; } } int main() { int a, b, c, max; printf("Enter Three Integers\n"); scanf("%d %d %d", &a, &b, &c); max = getMax(getMax(a, b), c); /* Print Maximum Number */ printf("Maximum Number = %d\n", max); return 0; }Output
Enter Three Integers 32 45 87 Maximum Number = 87Related Topics