Write a program that determines
the largest value among the integers.
#include <stdio.h>
#include <conio.h>
int main()
{
int i, num[5], largest = 0;
int stop = 0;
while( stop != -1)
{
printf("Enter 5 integers separated by a space: ");
for(i=0; i <=4; i++)
scanf_s("%d", &num[i]);
largest = num[0]; /* assign the 1st element to largest*/
for(i=1; i<=4; i++) /* compare the others and keep storing the largest*/
if(num[i] > largest)
largest = num[i];
printf("The largest number among ");
for(i=0; i <=4; i++)
printf("%d ", num[i]);
printf("is %d\n", largest);
printf("\nMore data? -1 to stop, others to continue: ");
scanf_s("%d", &stop);
}
return 0;
}
#include <conio.h>
int main()
{
int i, num[5], largest = 0;
int stop = 0;
while( stop != -1)
{
printf("Enter 5 integers separated by a space: ");
for(i=0; i <=4; i++)
scanf_s("%d", &num[i]);
largest = num[0]; /* assign the 1st element to largest*/
for(i=1; i<=4; i++) /* compare the others and keep storing the largest*/
if(num[i] > largest)
largest = num[i];
printf("The largest number among ");
for(i=0; i <=4; i++)
printf("%d ", num[i]);
printf("is %d\n", largest);
printf("\nMore data? -1 to stop, others to continue: ");
scanf_s("%d", &stop);
}
return 0;
}
No comments:
Post a Comment