Write a c program to enter the array of 10 integer numbers and print the smallest number.
#include<stdio.h>
#include<conio.h>
void main()
{
int num[10];
int temp=0,i;
printf("enter the numbers");
for(i=0;i<10;i++)
scanf("%d",&num[i]);
temp=num[0];
for(i=1;i<10;i++)
{
if(num[i]<temp)
temp=num[i];
}
printf("\n smallest number=%d",temp);
getch();
}
Output:
enter the numbers
23 42 67 89 66 45 78 55 34 77
smallest number=23
Explanation:
The program is to take an input of an integer array and print the smallest number in the array.
num[10] is the integer array of size 10.
Variable i is taken as a subscript, and variable temp, which is initialized as zero, is taken as a temporary variable.
Run time input of array is taken using subscript "i" from 0 to 9.
If you have any Misconceptions related to for-loop, follow the below link,
Now, we have to find the smallest number among the array of 10 numbers,
Initialize temp as array[0].
temp=array[0].
Use the for loop to run the entire loop.
Inside for loop, we are using the if-statement to compare two int values.
This for-loop will run from num[1] to num[9].
The value of num[0] is stored in the temp variable.
This will save one iteration of for loop.
At each iteration, num[i] is compared with the temp variable in the if-statement.
If num[i] is smaller than the temp variable, then the value of num[i] will be stored in temp. Otherwise, the loop will continue forward.
In this way, the temp will get the smallest number from the array.
1 Comments
very helpful keep posting,,,
ReplyDeleteHow to print arrays in Java
How to sort arrays in Java
Post a Comment