Jun 1, 2020

C program to print 1 to n natural numbers

Ex: write a C program to print 1 to n natural numbers. How to write a C program to print 1 to n natural numbers. C program to print 1 to n natural numbers.

Input from user:

Enter the limit: 20

Expected output:

Natural numbers from 1 to 20:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20





Step by step logic to print 1 to n natural numbers:


1. First declare two intiger variable's no for accept limit from user and i variable use in for loop.

2. Accept limit from user.

3. After that use one for loop to print 1 to n natural numbers.
loop like for(i=1; i<=no;i++)

How its work?

i=1 because we  need to print natural numbers from 1.


i<=no because we need to print natural numbers upto n. (n=no=20). 

i++ =i+1 because we need to increment it by one. 

 i=1=i+1=2=i+1=3=i+1=4=i+1=5...upto n.


4. Print value of i inside the for loop. Because we need to print natural numbers from 1 to n. (i will iterate from 1 to n)





Program:


#include <stdio.h>
int main()
{
  int no,i;
  
  printf("Enter the limit:");
  scanf("%d",&no);
  
  printf("\nNatural numbers from 1 to %d : ",no);
  
  for(i=1;i<=no;i++)
  {
  printf("%d ",i);
  }
  return 0;

}




Above prgram shows the following output:





Share This:    Facebook Twitter

0 comments:

Post a Comment

If you have any doubts, please discuss here...