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

Write a C program to count number of digits in a number. Using do-while loop

Develop a C program to print the following pattern of N rows using whileloop (Read N value from the Keyboard)For Example: N value : 512 34 5 67 8 9 1011 12 13 14

Note: This question helps in clearing Cognizant conceptual technical interview12345678910111213#include <stdio.h> int main() { for (int num = 1; num <= 5; num++) { if (num % 2 == 0) { continue; } for (int i = 1; i <= num; i++) { printf("%d ", i); } printf("\n"); } return 0; }

What will be the output of the following program?#include<stdio.h> int main() {     char j=1;     while(j < 5)     {         printf("%d, ", j);         j = j+1;     }     printf("\n");     return 0; }Select one:4, 3, 2, 11 2 3 42 3 4 5 61, 2, 3, 4,

Alex is teaching his younger sister how to count the number of digits in an integer. He wants to write a program that takes an integer n as input and counts the number of digits using a do-while loop. Company Tags: CapgeminiInput format :The input consists of a single integer n.Output format :The output prints a single integer representing the count of digits in the given number.Refer to the sample output for formatting specifications.Code constraints :1 ≤ n ≤ 107Sample test cases :Input 1 :1Output 1 :1Input 2 :1000Output 2 :4Input 3 :54782Output 3 :5

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.