š„ C Program to Print Odd Numbers from 1 to N using Do-While Loop (Step-by-Step )
Want to quickly learn how to print odd numbers in C? š¤
In this example, we will print all odd numbers from 1 to N using a do-while loop in a very simple way.
š Odd numbers are numbers like: 1, 3, 5, 7... (not divisible by 2)
š» C Program Code
scanf("%d",&n);
printf("Odd Numbers from 1 to %d:\n",n);
do
{
if(c % 2 != 0)
printf("%d ", c);
c++;
} while(c <= n);
return 0;
}
š Output
Enter the value of N: 7
Odd Numbers from 1 to 7:
1 3 5 7
š§ Easy Explanation (Understand in 30 Seconds)
ā Step 1: Start from number 1
ā Step 2: Check ā number % 2 != 0 (means odd)
ā Step 3: If true ā print number
ā Step 4: Increase number by 1
ā Step 5: Repeat until it reaches N
ā” Important Concept (Must Know)
š In do-while loop, the loop runs at least one time.
š Condition is checked after execution.
š This is the main difference from while loop.
šÆ Why This Program is Important?
ā Very common in exams
ā Frequently asked in interviews
ā Builds strong loop logic
ā Helps in solving advanced problems
š Try This Yourself
š Modify the program to print even numbers
š Try printing numbers in reverse order
š Try using only while loop and compare output