Displaying first n natural numbers using different loops [ C Program Code]

The following C programming code displays the first n natural numbers on the screen. To carry out the process, we have used three different logics - the while loop, do while loop and the for loop.
 

Using while loop
#include<stdio.h>
int main()
{
int n,i=1;
printf("Enter the limit: ");
scanf("%d",&n);
printf("The natural numbers are: ");
while(i<=n)
{
printf("\t%d",i);
i=i+1;
}
getch();
return(0);
}
view raw gistfile1.txt hosted with ❤ by GitHub
Using do while loop
#include<stdio.h>
int main()
{
int n,i=1;
printf("Enter the limit: ");
scanf("%d",&n);
printf("The natural numbers are: ");
do
{
printf("\t%d",i);
i=i+1;
}
while(i<=n);
getch();
return(0);
}
view raw gistfile1.txt hosted with ❤ by GitHub
Using for loop
#include<stdio.h>
int main()
{
int n,i;
printf("Enter the limit: ");
scanf("%d",&n);
printf("The natural numbers are: ");
for(i=1;i<=n;i++)
{
printf("\t%d",i);
}
getch();
return(0);
}
view raw gistfile1.txt hosted with ❤ by GitHub

Comments