-->
Showing posts with label display 1. Show all posts
Showing posts with label display 1. Show all posts

program to print/display 1,11,111,1111,11111,.... to nth term.

//program to print/display 1,11,111,1111,11111,.... to nth term.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int k=1,m=1,n;
printf("enter value of 'n'\n");
scanf("%d",&n);
while(m<=n)
   {
      printf("%d\n",k);
      
      k=k+pow(10,m);
      m++;
   }
getch();
}

logic applied:

1,11,111,1111...
can be written as
1,10+1,100+11,1000+111....
can be written as
1,10 1+1,10 2+11,10 3+111
converting it into formula
k=k+pow(10,m);