Can anyone help me solve this C-Programming question? a) Write a program that as
ID: 3887976 • Letter: C
Question
Can anyone help me solve this C-Programming question?
a) Write a program that asks for a number. Then the program should print 1 through the given number on separate lines.
b) Encapsulate your code in a while-loop that asks the user if he/she would like to run the program again. Note that when reading a character from the input stream, the newline from the previous input is still buffered and considered as input. To discard the newline, start the scanf string with a space like this: scanf(" %c", &input);
Give a number: 5
1
2
3
4
5
Run again (y/n)? Y
Give a number: 2
1
2
Run again (y/n)? n
Exiting... [.../exercise]$
Explanation / Answer
#include <stdio.h>
int main() {
int n;
char con= 'y';
while(1){
int i=1;
if(con == 'y'){
printf("Give a number: ");
scanf("%d",&n);
while(i<=n){
printf("%d ",i);
i++;
}
printf("Run again (y/n)? ");
scanf("%s",&con);
}else{
printf("Exciting....");
return 0;
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.