Sunday 12 December 2010

Program for To find both the largest and smallest number in a list of integers


This program contains n number of elements, in these elements we can find the largest and smallest numbers and display these two numbers

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

{
int a[10],i,n,min,max;
clrscr();

printf("enter the array size:"); scanf("%d",&n);
printf("Enter the elements of array"); for(i=0;i<n;i++) // read the elements of an array scanf("%d",&a[i]);
min=a[0]; max=a[0];
for(i=0;i<n;i++)// read the elements of an array
{
if(a[i]<min)// check the condition for minimum value min=a[i];
if(a[i]>max)//check the condition for maximum  value max=a[i];
}
printf("maximum value is:%d\n",max);
printf("minimum value is:%d\n",min);
getch();
}


Output:

1. enter the array size:4
Enter the elements of array 36 13 2 45
maximum value is:45
    minimum value is:2
2.enter the array size:5
Enter the elements of array 6 2  1   3 8
maximum value is:8
minimum value is:1
3.enter the array size:5
Enter the elements of array-6 9 -9 2 5
   maximum value is:9
minimum value is:-9

conclusion: the program is error free

VIVA QUESTIONS:

1) What is an array ?
Ans: The collection of similar elements is called array
2) How many types of arrays are there ?
 Ans: Three types. They are one dimensional ,two dimensional and multi dimensional arrays




C for Loop Examples

No comments:

Post a Comment