Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

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:

Display first 10 natural numbers

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:

Flowchart: Display first 10 natural numbers

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
i(int): ?

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

Static variable inside of a function in C

The scope of variable is where the variable name can be seen. Here, x is visible only inside function foo().

The lifetime of a variable is the period over which it exists. If x were defined without the keyword static, the lifetime would be from the entry into foo() to the return from foo(); so it would be re-initialized to 5 on every call.

The keyword static acts to extend the lifetime of a variable to the lifetime of the programme; e.g. initialization occurs once and once only and then the variable retains its value - whatever it has come to be - over all future calls to foo().

Ref : https://bit.ly/3fOq7XP