unique, ad-free, freemium downloads

8 December 2017

How to Find Smallest of N Integers (Easy and Simple)

SMALLEST INTEGER IN C:

In this program, we have taken an input of n integers from the user and then find the smallest of them.

LOGIC:

First, we initialize the smallest integer with the first input.Then we find whether the next input is smaller or greater than the smallest integer.If it is smaller than it than smallest integer takes its value.Otherwise, the loop continues until it has taken all the input.

CODE:

int main()
{   int small,num,input;
    printf("\n\tPlease enter the number of integers: ");
    scanf("%d",&num);
    for(int a=1;a<=num;a++)
    {
        printf("\tPlease enter the number %d: ",a);
        scanf("%d",&input);
        if(a==1)//to initialize small to first input 
        small = input ;
        if(input<small)
        small = input ;//small becomes equal to input if it is greater than input
            }
    printf("\n\tThe smallest number is:%d\n\n",small);
    system("pause");
    return 0;
}

OUTPUT:


No comments:

Post a Comment