Print 1 to n numbers C program

Here is a simple program to print numbers 1 to n.
To print number use a for loop and print numbers one by one. For example if we want to print number form 1 to 10 then run a for loop from 1 to 10 and print numbers one by one.

#include<stdio.h>
int main()
{
 int n,i;
 printf("Enter a number: ");
 scanf("%d",&n);
 printf("Number till %d: \n",n);
 for(i=1;i<=n;i++)
 {
 printf("%d\n",i); 
 }
 return 0;
}

Print Numbers 1 to n using while loop
#include<stdio.h>
int main()
{
 int n,i=1;
 printf("Enter a number: ");
 scanf("%d",&n);
 printf("Number till %d: \n",n);
 while(i<=n)
 {
  printf("%d\n",i);
  i++;
 }
 return 0;
}


Print numbers 1 to n using function 
#include<stdio.h>
void PrintNumbers(int a)
{
 int i;
 for(i=1;i<=a;i++)
 {
  printf("%d\n",i);
 }
}
int main()
{
 int n,i=1;
 printf("Enter a number: ");
 scanf("%d",&n);
 printf("Number till %d: \n",n);
 PrintNumbers(n);
 return 0;
}
 

Popular posts from this blog