Print numbers upto n in C (user-provided number) using loops

Spread the love

Program Description:

Write a C Program to print numbers upto n in C Language. Here n is the user-provided number. So the program should accept a number from the user and print all the numbers from 1 to the given number n. if the user enters 10, Program should print numbers from 1 to 10. Here is an example of input and output.

Example Input and Output:


Enter a Positive Number: 10


1 2 3 4 5 6 7 8 9 10

Pre Requisites:

We will write the program using the while, for, and do while loop. So it is good to have knowledge of the C loops. Please follow the below articles to learn more about the above concepts.

Print Numbers upto n in C – Program Algorithm:

  • The Program starts by asking the user for Input, User needs to provide a Positive Number. We are storing the number in a variable num. If the user provides a negative number or zero. We are going to show the error message.
  • Once we got the number, Then we will create a loop, which is going to iterate from the number 1 to number num ( user provided number)
  • We iterate with the help of a loop control variable or counter. here it is count. which starts from 1 and incremented by 1 at every iteration.
  • The loop will stop once the count reaches the num , Here is our loop condition – count <= num.
  • At Each Iteration, we print the value of counter and increment the count variable by 1. This way we will be able to print the numbers from 1 to num.

Let’s convert the above logic into the program.

Print Numbers up to n in C using the While loop:

Program Output:

We are using the GCC compiler to run the program. Learn more about the program compilation on Linux in the following article.

If the user enters Invalid Input

As you can see from the above output, The program is displaying the error message and suggesting the user to provide the positive number.

Print Numbers up to in n in C using For loop:

Program Output:

Compile and run the program.

$ gcc print-numbers.c

Run the program – Print numbers up to 5

Print numbers up to 20

Print Numbers up to in n in C using do while loop:

To print numbers using the do-while loop, The program logic is going to be the same as the above two methods.

Program Output:

Here is the do-while program output.

Pretty Print Output – print numbers upto n in C Language :

We can also introduce a new line after every 10 numbers So that the output loops like a Table. Let’s re-write the above program to print the new line after every 10 numbers.

Program Output:

As you can see from the above output, We are printing the new line after every 10 numbers. We accomplished it using the following code

We can change the count % 10 as your need to change the table size. If you want to print new line after every 20 numbers, Then perform modulo division with 20, Like this – count % 20 == 0.

Exercise Program:

  • Print the numbers up to n in C using the goto statement.

You can refer to Reverse a Number Program in C where we have used a goto statement to create a loop. ( Check the Method 4).


Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

2 Responses

  1. […] C Program to Print Numbers up to n ( user-provided number) […]

  2. […] 📢 Related Program: We have already looked at the program to print n natural numbers using Loops. […]

Leave a Reply