Saturday, 7 December 2019

Mini-program: Write a console program that prompts the user for an integer.

Q: Mini-program: Write a console program that prompts the user for an integer. As long as the user keeps entering positive integers, continue prompting for the next integer. Once the user enters an integer of 0 or less, report the largest integer that was entered and end the program.

Solution:

save it as test.c and in linux or ubuntu
Please run it using command : gcc -o test test.c
                                                        :./test
#include <stdio.h>
void main(){
int i;
int big=0;
printf("enter number : ");
scanf("%d",&i);
while(1){
printf("enter number : ");
if(i>0) {if(i>big) big=i;}
else {printf("largest number entered : %d\n",big);
return;}
scanf("%d",&i);}
}

No comments:

Post a Comment