-->

program to print/display 1 to n natural numbers.

first with while loop
//A program  to print/display 1 to n natural numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int k=1,n;
printf("enter value of 'n'\n");
scanf("%d",&n);
while(k<=n)
   {
      printf("%d,",k);
      k++;
   }
getch();
}

now with for loop

//A program  to print/display 1 to n natural numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int k,n;
printf("enter value of 'n'\n");

scanf("%d",&n);
for(k=1;k<=n;k++)
   {
      printf("%d,",k);
   }
getch();
}

now using do..while loop

//A program  to print/display 1 to n natural numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int k=1,n;
printf("enter value of 'n'\n");

scanf("%d",&n);
do
  {
       printf("%d,",k);
       k++;

   }
   while(k<=n);
getch();
}

No comments:

Post a Comment