Monday, 2 June 2014
0
C Program 10: To Find The Smallest Number in an Array
Aim: To Find the smallest number in an array
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, arr[5], smallest;
printf("Enter the values in an array: ");
for (i=0; i<=5; i++) /* enter values into array */
{
scanf("%d", &arr[i]);
}
/* checking the smallest no. */
smallest = arr[0];
for (i=0; i<=5; i++)
{
if (arr[i] < smallest )
{
smallest = arr[i];
}
}
printf("The smallest number is: %d", smallest);
getch();
}
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, arr[5], smallest;
printf("Enter the values in an array: ");
for (i=0; i<=5; i++) /* enter values into array */
{
scanf("%d", &arr[i]);
}
/* checking the smallest no. */
smallest = arr[0];
for (i=0; i<=5; i++)
{
if (arr[i] < smallest )
{
smallest = arr[i];
}
}
printf("The smallest number is: %d", smallest);
getch();
}
First we will ask the user to enter the values into the array. Now, we will take an integer smallest and put the initial value of array arr[0] into it. Now, we have to compare each value present in array with the smallest. If this value is less than the value in smallest we will replace the value of the latter with the value present in array at that time.
We will repeat that procedure 6 times for our array contains six values.
Following is the sample output:
Subscribe to:
Post Comments (Atom)
0 Responses to “C Program 10: To Find The Smallest Number in an Array”
Post a Comment