Skip to main content

ProwessApps

Learn · Practice · Excel

C Program to find greatest among two numbers

C Code Example — Conditional Programs

ADVERTISEMENT

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

  1. Declare two integer variables a and b.
  2. Read the numbers from the user.
  3. Use the condition if(a > b) to check which is larger.
  4. If true, print "A is Greatest".
  5. 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-else statement 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 if runs. If it evaluates to false, the else block runs instead.

Complexity

Time Complexity O(1)
Space Complexity O(1)
ADVERTISEMENT