Knowee
Questions
Features
Study Tools

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.

🧐 Not the exact question you are looking for?Go ask a question

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:

  1. We first include the stdio.h library which allows us to use printf and scanf functions.
  2. We declare the main function where our program starts.
  3. We declare two integer variables, i and n. i is initialized to 1.
  4. We ask the user to enter a number and store it in the variable n.
  5. 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.
  6. 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.
  7. Finally, we return 0 to indicate that the program has finished successfully.

This problem has been solved

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)

1/3

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.