C Program To Determine Youngest of Three

If the ages of Ram, Shyam and Ajay are input through the keyboard, write a C program to determine the youngest of the three.

Related Read:
Decision Control Instruction In C: IF
Relational Operators In C

Expected Output for the Input

User Input:
Enter the age of Ram, Shyam and Ajay
20
20
30

Output:
Ram is the youngest
Shyam is the youngest

Video Tutorial: C Program To Determine Youngest of Three



YouTube Link: https://www.youtube.com/watch?v=EPq2lvHXs-k [Watch the Video In Full Screen.]

Source Code: C Program To Determine Youngest of Three

#include<stdio.h>

int main()
{
    int ram, shyam, ajay;

    printf("Enter the age of Ram, Shyam and Ajay\n");
    scanf("%d%d%d", &ram, ­­­­&shyam, &ajay);

    if(ram <= shyam && ram <= ajay)
    {
        printf("Ram is the youngest\n");
    }

    if(shyam <= ram && shyam <= ajay)
    {
        printf("Shyam is the youngest\n");
    }

    if(ajay <= ram && ajay <= shyam)
    {
        printf("Ajay is the youngest\n");
    }

    return 0;
}

Output 1:
Enter the age of Ram, Shyam and Ajay
20
30
20
Ram is the youngest
Ajay is the youngest

Output 2:
Enter the age of Ram, Shyam and Ajay
20
30
19
Ajay is the youngest

Logic To Determine Youngest of Three

In this C program we ask the user to enter age for 3 people – Ram, Shyam and Ajay. We store it inside the address of variables ram, shyam and ajay.

We check if Ram’s age is less than or equal to Shyam and Ajay’s age. If true Ram is the youngest person.

We check if Shyam’s age is less than or equal to Ram and Ajay’s age. If true Shyam is the youngest person.

We check if Ajay’s age is less than or equal to Ram and Shyam’s age. If true Ajay is the youngest person.

We write separate if condition for each person. If we take else if clause it gives wrong result when 2 or more people have same age.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

View Comments

  • As you mentioned in code
    Line no 8 : scanf("%d%d%d", &ram, ­am, &ajay);
    There is issue in this command line.

    It should come like
    scanf("%d%d%d", &ram, ­&shyam, &ajay);

    Please note this.