how do you promt the program from positive integars terminatd by -1 .. and then ask for smallest of these integars.
I did everything except when I add (!= EOF) .. It shows the same prompt multiple times
should I still keep the code or is there a specific error?
#include <stdio.h>
int
main (void)
{
int n, smallest, i=1;
while (n != EOF)
{
printf ("Enter a list of positive integars: ",i);
scanf ("%d", &n);
if (n<=smallest)
smallest=n;
i++;
}
printf("The smallest of these integars is %d.\n ");
return (0);
}
2
Contributors
3
Replies
2 Hours
Discussion Span
1 Year Ago
Last Updated
4
Views
Question Answered
Related Article:EOF in C
is a C discussion thread by Vinod Supnekar that has 7 replies, was last updated 1 year ago and has been tagged with the keywords: eof.
That's because the 'printf' statement is inside the loop and you don't need to pass that 'i'.You can do like this:
int main (void)//this is more readable, isn't it?
{
int n, smallest=0, i=0;
printf ("Enter a list of positive integars:\n");//printf outside the loop
while (scanf("%d", &n)!=EOF){//press ctrl+Z to break out of the loop
if(n==-1)
break;
if (n<=smallest)
smallest=n;
i++;
}
printf("The smallest among the %d integars is %d.\n ",i,smallest);
return (0);
}