In this C program, we will learn about how to read two numbers and find maximum numbers using if else statement. We will first take two numbers as input from user using scanf function. Then we print the maximum number on screen.
Required Knowledge
C program to find maximum of two numbers using If Else statement
#include <stdio.h> int main() { int a, b; printf("Enter Two Integers\n"); scanf("%d %d", &a, &b); if(a > b) { /* a is greater than b */ printf("%d is Largest\n", a); } else if (b > a){ /* b is greater than a*/ printf("%d is Largest\n", b); } else { printf("Both Equal\n"); } return 0; }Output
Enter Two Integers 3 6 6 is Largest
Enter Two Integers 7 2 7 is Largest
Enter Two Integers 5 5 Both Equal
C program to find maximum of two numbers using ternary operator
#include <stdio.h> int main() { int a, b, max; printf("Enter Two Integers\n"); scanf("%d %d", &a, &b); if(a == b){ printf("Both Equal\n"); } max = (a > b) ? a : b ; printf("%d is Largest\n", max); return 0; }Output
Enter Two Integers 5 6 6 is Largest
Enter Two Integers 5 5 Both EqualRelated Topics