C Program to find Largest of Three Numbers

Programs to find the largest or greatest of three numbers in c; In this tutorial, i am going to show you how to find the largest of three numbers in the c program using if statement, ternary operator and nested if else statement.

Algorithm and Programs to find Largest of Three Numbers in C

  • Aglorithm to Find Largest of Three Numbers
  • C Program to Find Largest of Two Numbers using Else If Statement
  • C Program to Find Largest of Two Numbers using Ternary Operator
  • C Program to Find Largest of Two Numbers using Nested If Else Statement

Aglorithm to Find Largest of Three Numbers

Follow the below given algorithm to write a c program to find largest of three number; as follows:

  1. Start writing program
  2. Read the three integer values in program.
  3. Check if num1 is greater than num2.
  4. If true, then check if num1 is greater than num3.If true, then print ‘num1’ as the greatest number.
  5. If false, then print ‘num3’ as the greatest number.
  6. If false, then check if num2 is greater than num3.If true, then print ‘num2’ as the greatest number.
  7. If false, then print ‘num3’ as the greatest number.
  8. End program

C Program to Find Largest of Three Numbers using Else If Statement

#include <stdio.h>
int main()
{
    int num1, num2, num3;
    printf(" Enter the number1 = ");
    scanf("%d", &num1);
    printf("\n Enter the number2 = ");
    scanf("%d", &num2);
    printf("\n Enter the number3 = ");
    scanf("%d", &num3);
    if (num1 > num2)
    {
        if (num1 > num3)
        {
            printf("\n Largest number = %d \n",num1);
        }
        else
        {
            printf("\n Largest number = %d \n",num3);
        }
    }
    else if (num2 > num3)
    {
        printf("\n Largest number = %d \n",num2);
    }
    else
    {
        printf("\n Largest number = %d \n",num3);
    }
    return 0;
}

The result of the above c program; as follows:

Enter the number1 = 10
Enter the number2 = 25
Enter the number3 = 65
Largest number = 65 

C Program to Find Largest of Three Numbers using Ternary Operator

#include <stdio.h>
int main()
{
    int num1, num2, num3, largest;
    
    printf(" Enter the number1 = ");
    scanf("%d", &num1);
    printf("\n Enter the number2 = ");
    scanf("%d", &num2);
    printf("\n Enter the number3 = ");
    scanf("%d", &num3);
   
    largest =((num1>num2 && num1>num3)?num1: (num2>num3)?num2:num3);
    printf("Largest number = %d \n",largest);
    return 0;
}

The result of the above c program; as follows:

Enter the number1 = 15
Enter the number2 = 25
Enter the number3 = 35
Largest number = 35 

C Program to Find Largest of Two Numbers using Nested If Else Statement

#include <stdio.h>
int main() {
    int num1, num2, num3;
    printf("Enter 1st number : ");
    scanf("%d", &num1);
    printf("\nEnter 2nd number : ");
    scanf("%d", &num2);
    printf("\nEnter 3rd number : ");
    scanf("%d", &num3);
    if(num1 >= num2) {
        if(num1 >= num3) {
            printf("\n%d is largest number", num1);
        } else {
            printf("\n%d is largest number", num3);
        }
    } else {
        if(num2 >= num3) {
            printf("\n%d is largest number", num2);
        } else {
            printf("\n%d is largest number", num3);
        }
    }
    return 0;
}

The result of the above c program; as follows:

Enter 1st number : 85
Enter 2nd number : 65
Enter 3rd number : 78
85 is largest number

More C Programming Tutorials

Leave a Comment