Pages

Find Out The Smallest Value

Monday 7 January 2013
 Write a program that determines the smallest value among the integers.

#include <stdio.h>
#include <conio.h>

int main()
{
    int i, num[5], smallest = 0;
    int stop = 0;

    while( stop != -1)
    {
        printf("Enter 5 integers separated by a space: ");
      
        for(i=0; i <=4; i++)
        scanf_s("%d", &num[i]);
        smallest = num[0];

        for(i=1; i<=4; i++)          /* compare the others and keep storing the smallest*/
        if(num[i] < smallest)
        smallest = num[i];
        printf("The smallest number among ");
      
        for(i=0; i <=4; i++)
        printf("%d ", num[i]);
        printf("is %d\n", smallest);   /* print the smallest number*/
        printf("\nMore data? -1 to stop, others to continue: ");
        scanf_s("%d", &stop);
    }
    return 0;
}

No comments:

Post a Comment