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
Using do while loop
Using for loop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} |
Comments
Post a Comment