write a program to print integer no, from 1 to n where n is the input from the user using do while loop in c language use getch
Question
Write a program to print integer no, from 1 to n where n is the input from the user using do while loop in C language use getch.
Solution
Here is a simple C program that uses a do-while loop to print integers from 1 to n, where n is input from the user. Please note that C does not have a built-in function for getch, so I will use getchar instead.
#include <stdio.h>
int main() {
int i = 1, n;
printf("Enter a number: ");
scanf("%d", &n);
do {
printf("%d\n", i);
i++;
} while(i <= n);
printf("Press any key to exit.\n");
getchar();
getchar();
return 0;
}
In this program:
- We first include the stdio.h library which allows us to use printf and scanf functions.
- We declare the main function where our program starts.
- We declare two integer variables, i and n. i is initialized to 1.
- We ask the user to enter a number and store it in the variable n.
- We start a do-while loop. In each iteration, we print the value of i and then increment it by 1. The loop continues as long as i is less than or equal to n.
- After the loop, we print a message asking the user to press any key to exit. We use getchar twice to wait for the user to press a key.
- Finally, we return 0 to indicate that the program has finished successfully.
Similar Questions
create a C program to print Fibonacci series upto a specified number N using while loop.
The output of the code will be ____. int i=5; printf(“%d %d %d”, i, i++, ++i); in c
main( ){int i, j ;for ( i = 1 ; i <= 2 ; i++ ){for ( j = 1 ; j <= 2 ; j++ ){if ( i == j )continue ;printf ( “n%d %dn”, i, j ) ;}}}1 22 12 11 23 22 3Error
What is the output of the following piece of code?int i;i = 10;while (i < 20){ printf("%d", i % 2); i++;}
What will be the output of the given Python code?n=7c=0while(n): if(n > 5): c=c+n-1 n=n-1 else: breakprint(n)print(c)
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.