Index « Previous Next »

Question

Write a program to enter the numbers till the user wants and at the end the program should display the largest and smallest numbers entered.

Source Code

#include <stdio.h>

int main()
{
    int number, max = 0, min = 32767;
    char choice;

    do
    {
        printf("Enter a number :");
        scanf("%d", &number);

        if (number > max)
        {
            max = number;
        }
        if (number < min)
        {
            min = number;
        }

        printf("Do you want to Continue(y/n)? ");
        scanf("%c", &choice);

    }while (choice == 'y' || choice == 'Y');

    printf("\nMaximum number :%d\nMinimum Number :%d", max, min);

    return 0;
}

Output

Enter a number :15
Do you want to Continue(y/n)? y
Enter a number :7
Do you want to Continue(y/n)? y
Enter a number :3
Do you want to Continue(y/n)? y
Enter a number :12
Do you want to Continue(y/n)? n

Maximum number :15
Minimum Number :3