Lets write a simple C program to print natural numbers from 1 to N, using for loop.
Related Read:
For Loop In C Programming Language
Source Code: C Program to Print Natural Numbers from 1 to N using for loop
#include<stdio.h> int main() { int num, count; printf("Enter a positive number\n"); scanf("%d", &num); printf("\nNatural numbers from 1 to %d are:\n", num); for(count = 1; count <= num; count++) { printf("%d\t", count); } printf("\n"); return 0; }
Output:
Enter a positive number
5
Natural numbers from 1 to 5 are:
1 2 3 4 5
C Program to Print Natural Numbers from 1 to N using for loop
[youtube https://www.youtube.com/watch?v=sbIwx-_A5D0]
Logic To Print Natural Numbers from 1 to N using for loop
We start by assigning 1 to variable count. Now we ask the user to enter a positive number. Now for loop keeps executing until value of count is less than or equal to user entered value. Inside for loop we printout the value of count and then increment the value of count by one for each iteration of loop.
This way we printout all the natural numbers from 1 to N.
For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List
For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert