C Program to Print 1 to 10 Using While Loop (With Output)
Complete guide with code examples, sample output, and step-by-step explanations for while and do-while loops
📚 Part of the Loops in C series. New to loops? Start with: Introduction to Loops in C
Last Updated: September 21, 2026
About the Author
Sankalan Data Technologies
Programming & Data Analytics Tutorials
Published by Sankalan Data Technologies. Our tutorials and data guides are independently developed and curated to provide practical, hands-on learning resources for Python, SQL, and data analytics practitioners.
Quick Answer: Print 1 to 10 in C
To print numbers from 1 to 10 in C, initialize a counter to 1 and increment it until it reaches 10. Here's the simplest way using a while loop:
#include <stdio.h>
int main() {
int i = 1;
while (i <= 10) {
printf("%d ", i);
i++;
}
return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10
What You'll Learn in This Tutorial
- Print 1 to 10 using While Loop
- Print 1 to N using Do-While Loop
- While vs Do-While Comparison
- Practice Questions
Loops are a fundamental concept in C programming. They allow you to repeat a block of code multiple times without writing the same code over and over. In this tutorial, we'll learn how to print numbers from 1 to 10 using two important loop structures: while and do-while loops.
Whether you're just starting your programming journey or brushing up on your C skills, understanding these loops is essential. They're used in almost every program you'll write—from simple counting to complex data processing.
What is a while loop in C?
A while loop in C checks a condition before executing its body. It starts with the
keyword while, followed by a condition in parentheses. If the condition
is true, the loop body runs. The condition is re-evaluated after each iteration.
When the condition becomes false, the loop terminates. This makes it ideal for
situations where code should only run if a certain condition is met.
Syntax: while(condition) { statements; }
Key Point: The while loop checks the condition before executing the loop body. If the condition is false initially, the loop body will never execute.
Printing 1 to 10 with a While Loop (Check First)
The while loop checks the condition first. If the condition is true, it executes the loop body. This process repeats until the condition becomes false.
We start by declaring a variable i and setting it to 1. This is our counter.
Then, the while loop checks: "Is i still less than or equal to 10?"
If yes, it prints the current value of i and increases it by 1. This continues
until i becomes 11, at which point the condition fails and the loop stops.
C Program to Print 1 to 10 Using While Loop
#include <stdio.h>
int main() {
int i = 1;
while (i <= 10) {
printf("%d ", i);
i++;
}
printf("\n");
return 0;
}
Output
1 2 3 4 5 6 7 8 9 10
How the While Loop Works (Step-by-Step)
- Step 1: Declare variable
iand initialize it to 1. - Step 2: The
whileloop checks ifi <= 10is true. - Step 3: Inside the loop, print the current value of
i. - Step 4: Increment
iby 1 usingi++. - Step 5: Repeat steps 2-4 until
ibecomes 11. - Step 6: When
i = 11, the condition becomes false and the loop exits.
From experience: When I first learned loops, I kept making the same mistake — forgetting to increment the counter, which caused my program to run forever. If you ever see your program freeze or never stop printing, check your loop's increment step first. That one line prevents 90% of beginner loop bugs.
What is a do-while loop in C?
A do-while loop in C executes its body once before checking the condition.
It begins with the keyword do, followed by the loop body, and ends
with while(condition);. The body runs at least once, then the condition
is checked. If true, the loop repeats. If false, it stops.
Syntax: do { statements; } while(condition);
Key Point: The do-while loop checks the condition after executing the loop body. This guarantees that the loop body runs at least once.
This is useful for menus, user input validation, and any scenario where the code must run before the condition is tested.
Printing 1 to N with a Do-While Loop (Run First)
The do-while loop is different because it executes the body at least once before checking the condition. This makes it perfect for situations where you want to guarantee at least one execution.
In this program, we ask the user for a number n. The loop then prints
from 1 up to n. Even if the user enters 0, the loop will still print 1
once before stopping.
C Program to Print 1 to N Using Do-While Loop
#include <stdio.h>
int main() {
int i = 1, n;
printf("Enter the limit: ");
scanf("%d", &n);
printf("Series....:\n");
do {
printf("%d ", i);
i++;
} while (i <= n);
printf("\n");
return 0;
}
Output
Enter the limit: 10
Series....:
1 2 3 4 5 6 7 8 9 10
How the Do-While Loop Works (Step-by-Step)
- Step 1: Ask user to enter a limit N.
- Step 2: Initialize counter
i = 1. - Step 3: The
do-whileloop printsiand increments it. - Step 4: After printing, check if
i <= n. - Step 5: If true, repeat. If false, stop.
- Note: The loop always runs at least once, even if n is 0.
While vs Do-While: Key Differences
| Feature | While Loop | Do-While Loop |
|---|---|---|
| Condition Check | Before execution | After execution |
| Minimum Executions | 0 (may not run at all) | 1 (always runs at least once) |
| When to Use | When you want to check condition first | When you want to guarantee at least one execution |
| Example Use Case | Processing data until a condition is met | Displaying a menu at least once |
| Syntax | while(condition) { ... } |
do { ... } while(condition); |
Note: You can also print 1 to 10 using a for loop:
for (int i = 1; i <= 10; i++) {
printf("%d ", i);
}
The for loop is commonly used when you know the exact number of iterations.
Common Mistakes to Avoid
- Forgetting to increment the counter - Creates an infinite loop. Always include
i++inside the loop body. - Using the wrong condition -
i < 10prints only 1-9, not 1-10. - Starting the counter with the wrong value -
int i = 0;prints 0-9 instead of 1-10. - Missing semicolon in do-while - The do-while loop ends with a semicolon:
} while(condition); - Using = instead of == -
=is assignment,==is comparison. - Infinite loop - If the condition never becomes false, the loop runs forever.
Program Variations
Print 1 to N (Using While Loop)
#include <stdio.h>
int main() {
int n, i = 1;
printf("Enter a number: ");
scanf("%d", &n);
while (i <= n) {
printf("%d ", i);
i++;
}
printf("\n");
return 0;
}
Print Even Numbers 1 to 10 (Using While Loop)
#include <stdio.h>
int main() {
int i = 2;
while (i <= 10) {
printf("%d ", i);
i += 2;
}
printf("\n");
return 0;
}
Print Odd Numbers 1 to 10 (Using Do-While Loop)
#include <stdio.h>
int main() {
int i = 1;
do {
printf("%d ", i);
i += 2;
} while (i <= 10);
printf("\n");
return 0;
}
Print Numbers in Reverse (10 to 1) Using While
#include <stdio.h>
int main() {
int i = 10;
while (i >= 1) {
printf("%d ", i);
i--;
}
printf("\n");
return 0;
}
Calculate Sum of 1 to N (Using Do-While)
#include <stdio.h>
int main() {
int n, i = 1, sum = 0;
printf("Enter a number: ");
scanf("%d", &n);
do {
sum += i;
i++;
} while (i <= n);
printf("Sum: %d\n", sum);
return 0;
}
Practice Questions
Try these exercises to reinforce your understanding:
- Write a program to print 1 to 10 in reverse order (10 to 1) using a while loop.
- Write a program to print the sum of numbers from 1 to 10 using a do-while loop.
- Write a program to print the multiplication table of 5 using a while loop.
- Write a program to print only even numbers from 1 to 20 using a do-while loop.
- Write a program to print the factorial of a number using a while loop.
- Write a program to print the Fibonacci series up to N terms using a while loop.
Hint: Use if conditions inside loops to check for even/odd numbers. For factorial, multiply the counter with a running total.
Why This Program Matters
- Foundation Concept: While and do-while loops are fundamental building blocks in C programming.
- Exam Relevance: This is a common question in BCA, MCA, and engineering exams.
- Real-World Use: Loops are used everywhere—from data processing to game development.
- Pattern Building: Understanding this helps with printing patterns, number triangles, and more.
- Interview Preparation: Loop questions are frequently asked in coding interviews.
Frequently Asked Questions
How does a C program print numbers from 1 to 10 using a while loop?
In a C program, to print numbers from 1 to 10 with a while loop, you start by setting a variable i to 1. The program then uses a while loop that continues as long as i is less than or equal to 10. Inside the loop, it displays the current value of i and then increments i by 1. This process repeats until i exceeds 10, resulting in the sequence 1 to 10 being printed.
How does a C program print numbers from 1 to N using a do-while loop?
To print numbers from 1 to a user-defined limit N using a do-while loop in C, the program first prompts the user to enter the value of N. It initializes a variable i to 1 and then enters a do-while loop. Inside the loop, it prints the current value of i and increments i by 1. The loop continues as long as i is less than or equal to N, ensuring the sequence from 1 up to N is displayed.
What is the difference between while and do-while loops in C?
The key difference is that a while loop checks its condition before executing the loop body, so if the condition is false initially, the loop may not run at all. In contrast, a do-while loop executes its body at least once before checking the condition, guaranteeing that the loop runs at least once regardless of the condition.
How is the variable 'i' used in these C programs?
In both programs, 'i' acts as a loop counter starting at 1. It is printed during each iteration and then increased by 1 to generate the sequence of numbers from 1 to N (or 10).
What happens if I forget to increment the counter in a while loop?
If you forget to increment the counter variable (i++) inside a while loop, the condition will always remain true, creating an infinite loop. The program will run forever until you force it to stop.
Can I use a do-while loop to print 1 to 10 without user input?
Yes, you can replace the user input with a fixed value like 10. Simply set N=10 in the program instead of asking the user to enter it. The do-while loop will still work the same way.