Monday, December 27, 2010

find the smallest from array

/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for scannign n numbers and finding the smallest from them
*/

#include<stdio.h>
void main()
{


int
n = 0 , i =0 , j = 0;

int
no[100];
int
smallest;
printf("How many numbers you want to scan ? ");

scanf("%d",&n);
if
(n < 0 || n>100)
{


// if the user enters invalid no of elemtns then we come here
printf("Invalid number. It must be brtween 1 to 100");
}

else

{

// logic for scanning the numbers
for(i = 0 ; i < n ;i++)
{


printf("Enter element %d: ", i);
scanf("%d",&no[i]);
}



// assign biggest the first element's value
biggest = no[0];
// the logic for finding the smallest number
for(i = 0 ; i < n ;i++)
{

if
(no[i] < smallest)
{


smallest = no[i];
}
}


printf("The smallest number among those numbers is: %d" , smallest);

}


printf("\n");
}

No comments:

Post a Comment