Q:

Find the largest of two numbers using the if-else condition

belongs to collection: C Programming Examples

0

C program to find largest of two given numbers is discussed here. Input two integers from the user and find the largest number among them. Given two numbers num1 and num2. The task is to find the largest number among the two.

Example:

Input: num1 = 2, num1 = 8
Output: Largest number = 8
 
 
Input: num1 = 20, num1 = 18
Output: Largest number = 20

Algorithm to find the greatest of two numbers

  1.  Ask the user to enter two integer values.
  2. Read the two integer values in num1 and num2 (integer variables).
  3. Check if num1 is greater than num2.
  4. If true, then print ‘num1’ as the greatest number.
  5. If false, then print ‘num2’ as the greatest number.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <stdio.h>
int main()
{
    int num1, num2;
    // Ask user to enter the two numbers
    printf("Please Enter Two different values\n");
    // Read two numbers from the user
    scanf("%d %d", &num1, &num2);
    if(num1 > num2)
    {
        printf("%d is Largest\n", num1);
    }
    else if (num2 > num1)
    {
        printf("%d is Largest\n", num2);
    }
    else
    {
        printf("Both are Equal\n");
    }
    return 0;
}

 

Output:

Please Enter Two different values: 27  6
27 is Largest

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Find the largest of two given number using ternary... >>