Fundamentals of Computer Programming - Old Questions
6. Write a program to input any 10 numbers then find out greatest and smallest number. [6]
Answer
AI is thinking...
#include<stdio.h>
int main()
{
int a[50], i, big, small;
int n=10;
printf("\\n\\nEnter the %d elements of the array: \\n\\n", n);
for(i = 0; i < n; i++)
scanf("%d", &a[i]);
big = a[0]; // initializing
/* from 2nd element to the last element find the bigger element than big and update the value of big */
for(i = 1; i < n; i++)
{
if(big < a[i]) // if larger value is encountered
{
big = a[i]; // update the value of big
}
}
printf("\\n\\nThe largest element is: %d", big);
small = a[0]; // initializing
/* from 2nd element to the last element find the smaller element than small and update the value of small*/
for(i = 1; i < n; i++)
{
if(small>a[i]) // if smaller value is encountered
{
small = a[i]; // update the value of small
}
}
printf("\\n\\nThe smallest element is: %d", small);
return 0;
}