C Program to find greatest among two numbers
Objective
Write a C program to find the greatest among two numbers using a simple if-else statement.
Algorithm / Approach
- Declare two integer variables
aandb. - Read the numbers from the user.
- Use the condition
if(a > b)to check which is larger. - If true, print "A is Greatest".
- Otherwise (using
else), print "B is Greatest".
main.c
#include<stdio.h>
int main( ){
int a, b;
printf("Enter Values for A : ");
scanf("%d",&a);
printf("Enter Values for B : ");
scanf("%d",&b);
if(a>b) {
printf("A is Greatest\n") ;
}
else {
printf("B is Greatest\n");
}
return 0;
}
Expected Output
Enter Values for A : 62 Enter Values for B : 119 B is Greatest
Explanation of the Program
- The
if-elsestatement is the most basic form of decision making in C. - It evaluates a condition (which must result in a boolean true/false or a non-zero/zero integer). If the condition is true, the block of code inside the
ifruns. If it evaluates to false, theelseblock runs instead.
Complexity
Time Complexity
O(1)
Space Complexity
O(1)