C Program to Print Numbers 1 to 10 Using While & Do-While Loop [Step-by-Step]
👨💻 About the Author
SDT
Created by Sankalan Data Tech Team
✅ Verified Trusted
Data Engineers, Analysts, Scientists & Trainers
With 15+ years of hands-on experience in programming, data science, and enterprise solutions. We have successfully trained 15,000+ students globally and delivered solutions for Fortune 500 companies.
Our content is created by experienced developers, data engineers, and data scientists to make programming easy through:
- Practical examples
- Real-world experience
- Clear explanations
We help you master programming the right way — from basics to advanced concepts.
⭐ 4.8/5 Rating
📖 15,000+ Students
🎓 Free Tutorial
⏱️ 5-min Read
🏢 Fortune 500 Trusted
📋 C Program to Print 1 to 10 - Quick Overview
| Feature |
Details |
| Language |
C Programming |
| Loop Type |
While & Do-While |
| Time to Learn |
5 Minutes |
| Author Experience |
15+ Years | 10,000+ Students |
📑 What You'll Learn in This Tutorial
Ever wondered how computers count? Whether you're just starting your programming journey or brushing up on your C skills, loops are everywhere—and once you master them, you'll be able to write code that repeats tasks effortlessly. In this guide, we're going to tackle a classic beginner exercise: printing numbers from 1 to 10 using two of the most important loop structures in C—the while loop and the do-while loop.
Think of loops like a chef repeating a recipe step over and over until a dish is ready. In programming, we use loops to repeat actions without writing the same code a hundred times. Pretty neat, right? Here, we'll start with a simple counter, print it out, increase it by one, and keep going until we hit our target. The while loop is like checking the recipe first—"Do we still need to cook?" If yes, keep going. The do-while loop, on the other hand, is like taking a taste first and then checking if more cooking is needed—it always runs at least once.
🎓 From Our Experience
Here's what we've observed teaching this topic
- Common Mistake: 60% of students forget to increment the counter variable inside the loop, causing infinite loops. Always check your j++ inside the while block!
- Why Do-While Matters: In our university exams, 8 out of 10 questions requiring at least one iteration use do-while. It's not just syntax—it's about understanding when execution must happen before condition checking.
- Real Project Use: This loop pattern helped one of our students build a student attendance system where the program had to display the menu at least once before asking the user to exit.
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.
Printing 1 to 10 with a While Loop (The "Check First" Approach)
Imagine you're counting on your fingers. You start at 1, say it out loud, then move to 2, and so on until you reach 10. That's exactly what this program does—but in C, of course!
We begin by declaring a variable j and setting it to 1. This is our counter. Then, the while loop checks: "Is j still less than or equal to 10?" If the answer is yes, it prints the current value of j and increases it by 1. This dance continues until j becomes 11, at which point the condition fails and the loop gracefully bows out. The result? A clean print of 1 through 10.
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. 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 (The "Run First" Approach)
Now, what if you want to let the user decide how far to count? That's where the do-while loop shines. Instead of hardcoding 10, we ask the user for a number n. The loop then prints from 1 all the way up to n.
Here's the cool part: the do-while loop always executes its body at least once before checking the condition. So even if someone enters 0 (which would normally break a while loop), this loop would still print 1 before stopping. It's like a persistent little worker that always shows up for duty at least one time. In our case, if the user enters 15, the output will be 1 through 15. If they enter 10, you get 1 through 10. Simple, flexible, and powerful.
💻 C Program Code to Print Numbers from 1 to 10 Using While Loop
📌 Output
Series....:
1 2 3 4 5 6 7 8 9 10
🧠 How This Program Works (Step-by-Step)
- Step 1: We create a variable
j and give it the starting value of 1—like setting the first domino.
- Step 2: The program prints the header "Series....:" so you know the numbers are coming.
- Step 3: The
while loop checks if j is still 10 or less. As long as it is, the loop runs.
- Step 4: Inside the loop, we print
j to the screen. The first time, that's 1.
- Step 5: We then increase
j by 1 using j++. So it becomes 2, then 3, and so on.
- Step 6: Once
j hits 11, the condition j <= 10 becomes false. The loop stops, and the program says "I'm done!" and prints a newline. End of story.
Related Programs:
Even Numbers |
Factorial Using While |
Prime Numbers
💻 C Program Code to Print Numbers from 1 to N Using Do-While Loop
📌 Output
Enter the limit for the series: 10
Series....:
1 2 3 4 5 6 7 8 9 10
🧠 How This Program Works (Step-by-Step)
- Step 1: The program asks you, "Hey, how far do you want to count?" You type a number, and it gets stored in
n.
- Step 2: We set
j back to 1—fresh start!
- Step 3: The
do-while loop kicks in. It doesn't check anything first; it just jumps right in and prints j.
- Step 4: After printing, it increases
j by 1 and then—now it checks: "Is j still less than or equal to n?"
- Step 5: If yes, it goes back and prints the next number. If no, it stops and the program ends with a cheerful newline.
- Step 6: The beauty of this is that even if you accidentally enter 0, the loop will still run once (printing 1) before it realizes it should stop. That's the do-while magic!
🤔 Complete FAQ - Supporting Questions
Primary Question: "How to print 1 to 10 in C?"
🔹 Follow-up Question 1: What is the syntax of a while loop in C?
The syntax is: while(condition) { statements; }. The condition is evaluated before each iteration. If true, the statements execute. This repeats until the condition becomes false. The semicolon after while is not used.
🔹 Follow-up Question 2: Can I print 1 to 10 without using loops?
Yes, you can use printf 10 times, but that's inefficient. Loops make your code shorter, flexible, and easier to maintain. For 1 to 100, imagine writing 100 printf statements!
🔹 Follow-up Question 3: What happens if the condition in while is false initially?
The loop body never executes. Control moves to the next statement after the loop. This is the key difference from do-while, which always executes at least once.
🔹 Follow-up Question 4: How do I print a series of numbers with a specific step value?
Change the increment part. For step 2: j = j + 2. For step 3: j = j + 3. This gives you 1,3,5... or 1,4,7... as needed.
🔹 Follow-up Question 5: What is an infinite loop and how do I avoid it?
An infinite loop runs forever because the condition never becomes false. In this program, forgetting j++ causes infinite loop. Always ensure your counter variable changes inside the loop.
🔹 Follow-up Question 6: Which loop is faster in C: while or do-while?
Both have similar performance. The difference is purely based on whether you need the body to run at least once. Choose based on logic, not speed.
🚀 Practice Question
Okay, time to put your skills to the test. Try modifying this program to do the following:
- Print only odd numbers from 1 to N
- Print only even numbers from 1 to N
- Calculate and print the sum of all odd numbers within a given range
Hint: You'll need to add an if condition inside the loop to check whether a number is odd or even. Give it a shot—you've got this!