C Exercises: Display first 10 natural numbers
C For Loop: Exercise-1 with Solution
Write a program in C to display the first 10 natural numbers.
Pictorial Presentation:

Sample Solution:
C Code:
#include <stdio.h>
void main()
{
int i;
printf("The first 10 natural numbers are:\n");
for (i=1;i<=10;i++)
{
printf("%d ",i);
}
printf("\n");
}
Sample Output:
The first 10 natural numbers are: 1 2 3 4 5 6 7 8 9 10
Flowchart:

Step wise execution of C program :
- Green color means line that has just executed
- Red color means next line to excute
- Stack : a stack is an abstract data type that serves as a collection of elements.
Steps | Code | Stack | Output |
---|---|---|---|
Step-1 | 1 int main(){
2 int i;
3 printf("The first 10 natural numbers are:\n");
4 for (i=1;i<=10;i++)
5 {
6 printf("%d ",i);
7 }
8 printf("\n");
9 return 0;
10 }
|
main |
|
Step-2 | 1 int main(){ 2 int i; 3 printf("The first 10 natural numbers are:\n"); 4 for (i=1;i<=10;i++) 5 { 6 printf("%d ",i); 7 } 8 printf("\n"); 9 return 0; 10 } |
main i(int): ? |
|
Step-3 | 1 int main(){ 2 int i; 3 printf("The first 10 natural numbers are:\n"); 4 for (i=1;i<=10;i++) 5 { 6 printf("%d ",i); 7 } 8 printf("\n"); 9 return 0; 10 } |
main i(int): ? |
The first 10 natural numbers are: |
Step-4 | 1 int main(){ 2 int i; 3 printf("The first 10 natural numbers are:\n"); 4 for (i=1;i<=10;i++) 5 { 6 printf("%d ",i); 7 } 8 printf("\n"); 9 return 0; 10 } |
main i(int): 1 |
The first 10 natural numbers are: |
Step-5 | 1 int main(){ 2 int i; 3 printf("The first 10 natural numbers are:\n"); 4 for (i=1;i<=10;i++) 5 { 6 printf("%d ",i); 7 } 8 printf("\n"); 9 return 0; 10 } |
main i(int): 1 |
The first 10 natural numbers are: 1 |
Step-6 | 1 int main(){ 2 int i; 3 printf("The first 10 natural numbers are:\n"); 4 for (i=1;i<=10;i++) 5 { 6 printf("%d ",i); 7 } 8 printf("\n"); 9 return 0; 10 } |
main i(int): 2 |
The first 5 natural numbers are: 1 |
Step-7 | 1 int main(){ 2 int i; 3 printf("The first 10 natural numbers are:\n"); 4 for (i=1;i<=10;i++) 5 { 6 printf("%d ",i); 7 } 8 printf("\n"); 9 return 0; 10 } |
main i(int): 2 |
The first 5 natural numbers are: 1 2 |
Step-8 | 1 int main(){ 2 int i; 3 printf("The first 10 natural numbers are:\n"); 4 for (i=1;i<=10;i++) 5 { 6 printf("%d ",i); 7 } 8 printf("\n"); 9 return 0; 10 } |
main i(int): 3 |
The first 5 natural numbers are: 1 2 |
Step-9 | 1 int main(){ 2 int i; 3 printf("The first 10 natural numbers are:\n"); 4 for (i=1;i<=10;i++) 5 { 6 printf("%d ",i); 7 } 8 printf("\n"); 9 return 0; 10 } |
main i(int): 3 |
The first 5 natural numbers are: 1 2 3 |
Step-10 | 1 int main(){ 2 int i; 3 printf("The first 10 natural numbers are:\n"); 4 for (i=1;i<=10;i++) 5 { 6 printf("%d ",i); 7 } 8 printf("\n"); 9 return 0; 10 } |
main i(int): 4 |
The first 10 natural numbers are: 1 2 3 |
Step-11 | 1 int main(){ 2 int i; 3 printf("The first 10 natural numbers are:\n"); 4 for (i=1;i<=10;i++) 5 { 6 printf("%d ",i); 7 } 8 printf("\n"); 9 return 0; 10 } |
main i(int): 4 |
The first 10 natural numbers are: 1 2 3 4 |
Step-12 | 1 int main(){ 2 int i; 3 printf("The first 10 natural numbers are:\n"); 4 for (i=1;i<=10;i++) 5 { 6 printf("%d ",i); 7 } 8 printf("\n"); 9 return 0; 10 } |
main i(int): 5 |
The first 10 natural numbers are: 1 2 3 4 |
Step-13 | 1 int main(){ 2 int i; 3 printf("The first 10 natural numbers are:\n"); 4 for (i=1;i<=10;i++) 5 { 6 printf("%d ",i); 7 } 8 printf("\n"); 9 return 0; 10 } |
main i(int): 5 |
The first 10 natural numbers are: 1 2 3 4 5 |
Step-14 | 1 int main(){ 2 int i; 3 printf("The first 10 natural numbers are:\n"); 4 for (i=1;i<=10;i++) 5 { 6 printf("%d ",i); 7 } 8 printf("\n"); 9 return 0; 10 } |
main i(int): 6 |
The first 10 natural numbers are: 1 2 3 4 5 |
Step-15 | 1 int main(){ 2 int i; 3 printf("The first 10 natural numbers are:\n"); 4 for (i=1;i<=10;i++) 5 { 6 printf("%d ",i); 7 } 8 printf("\n"); 9 return 0; 10 } |
main i(int): 6 |
The first 10 natural numbers are: 1 2 3 4 5 6 |
Step-16 | 1 int main(){ 2 int i; 3 printf("The first 10 natural numbers are:\n"); 4 for (i=1;i<=10;i++) 5 { 6 printf("%d ",i); 7 } 8 printf("\n"); 9 return 0; 10 } |
main i(int): 7 |
The first 10 natural numbers are: 1 2 3 4 5 6 |
Step-17 | 1 int main(){ 2 int i; 3 printf("The first 10 natural numbers are:\n"); 4 for (i=1;i<=10;i++) 5 { 6 printf("%d ",i); 7 } 8 printf("\n"); 9 return 0; 10 } |
main i(int): 7 |
The first 10 natural numbers are: 1 2 3 4 5 6 7 |
Step-18 | 1 int main(){ 2 int i; 3 printf("The first 10 natural numbers are:\n"); 4 for (i=1;i<=10;i++) 5 { 6 printf("%d ",i); 7 } 8 printf("\n"); 9 return 0; 10 } |
main i(int): 8 |
The first 10 natural numbers are: 1 2 3 4 5 6 7 |
Step-19 | 1 int main(){ 2 int i; 3 printf("The first 10 natural numbers are:\n"); 4 for (i=1;i<=10;i++) 5 { 6 printf("%d ",i); 7 } 8 printf("\n"); 9 return 0; 10 } |
main i(int): 8 |
The first 10 natural numbers are: 1 2 3 4 5 6 7 8 |
Step-20 | 1 int main(){ 2 int i; 3 printf("The first 10 natural numbers are:\n"); 4 for (i=1;i<=10;i++) 5 { 6 printf("%d ",i); 7 } 8 printf("\n"); 9 return 0; 10 } |
main i(int): 9 |
The first 10 natural numbers are: 1 2 3 4 5 6 7 8 |
Step-21 | 1 int main(){ 2 int i; 3 printf("The first 10 natural numbers are:\n"); 4 for (i=1;i<=10;i++) 5 { 6 printf("%d ",i); 7 } 8 printf("\n"); 9 return 0; 10 } |
main i(int): 9 |
The first 10 natural numbers are: 1 2 3 4 5 6 7 8 9 |
Step-22 | 1 int main(){ 2 int i; 3 printf("The first 10 natural numbers are:\n"); 4 for (i=1;i<=10;i++) 5 { 6 printf("%d ",i); 7 } 8 printf("\n"); 9 return 0; 10 } |
main i(int): 10 |
The first 10 natural numbers are: 1 2 3 4 5 6 7 8 9 |
Step-23 | 1 int main(){ 2 int i; 3 printf("The first 10 natural numbers are:\n"); 4 for (i=1;i<=10;i++) 5 { 6 printf("%d ",i); 7 } 8 printf("\n"); 9 return 0; 10 } |
main i(int): 10 |
The first 10 natural numbers are: 1 2 3 4 5 6 7 8 9 10 |
Step-24 | 1 int main(){ 2 int i; 3 printf("The first 10 natural numbers are:\n"); 4 for (i=1;i<=10;i++) 5 { 6 printf("%d ",i); 7 } 8 printf("\n"); 9 return 0; 10 } |
main i(int): 11 |
The first 10 natural numbers are: 1 2 3 4 5 6 7 8 9 10 |
Step-25 | 1 int main(){ 2 int i; 3 printf("The first 10 natural numbers are:\n"); 4 for (i=1;i<=10;i++) 5 { 6 printf("%d ",i); 7 } 8 printf("\n"); 9 return 0; 10 } |
main i(int): 11 |
The first 10 natural numbers are: 1 2 3 4 5 6 7 8 9 10 |
Exit | 1 int main(){ 2 int i; 3 printf("The first 10 natural numbers are:\n"); 4 for (i=1;i<=10;i++) 5 { 6 printf("%d ",i); 7 } 8 printf("\n"); 9 return 0; 10 } |
main i(int): 11 |
The first 10 natural numbers are: 1 2 3 4 5 6 7 8 9 10 |
C Programming Code Editor:
Improve this sample solution and post your code through Disqus.
Previous: C For Loop Exercises Home
Next: Write a C program to find the sum of first 10 natural numbers.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
C Programming: Tips of the Day
Printing hexadecimal characters in C:
You are seeing the ffffff because char is signed on your system. In C, vararg functions such as printf will promote all integers smaller than int to int. Since char is an integer (8-bit signed integer in your case), your chars are being promoted to int via sign-extension.
Since c0 and 80 have a leading 1-bit (and are negative as an 8-bit integer), they are being sign-extended while the others in your sample don't.
char int c0 -> ffffffc0 80 -> ffffff80 61 -> 00000061Here's a solution:
char ch = 0xC0; printf("%x", ch & 0xff);
This will mask out the upper bits and keep only the lower 8 bits that you want.
Ref : https://bit.ly/3vOLizM
- Exercises: Weekly Top 12 Most Popular Topics
- Pandas DataFrame: Exercises, Practice, Solution
- Conversion Tools
- JavaScript: HTML Form Validation
- SQL Exercises, Practice, Solution - SUBQUERIES
- C Programming Exercises, Practice, Solution : For Loop
- Python Exercises, Practice, Solution
- Python Data Type: List - Exercises, Practice, Solution
- C++ Basic: Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - exercises on Employee Database
- SQL Exercises, Practice, Solution - exercises on Movie Database
- SQL Exercises, Practice, Solution - exercises on Soccer Database
- C Programming Exercises, Practice, Solution : Recursion