-->
Showing posts with label 0.9999.....nth terms.. Show all posts
Showing posts with label 0.9999.....nth terms.. Show all posts

program to get 0.9,0.99,0.999,0.9999.....nth terms.

//program to get 0.9,0.99,0.999,0.9999.....nth terms.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float n,j, k=0.9;
clrscr();
printf("enter number of terms\n");
scanf("%f",&n);
for(j=1;j<=n;j++)
{
printf("%f,",k);
k=k+0.9/(pow(10,j));
}
getch();
}
-------------------------------------------------------------------------------------------------------------------------
logics in mind for this:-
->0.9,0.99,0.999............. can be written as
             0.9,0.9+0.09,0.99+0.009.......
->         for second term 0.99, it can be written as 0.9+0.09
 ->further it can be as(taking only two terms)
            0.9+( 0.9+0.9/10)...
->it can be as
          0.9+(0.9+0.9/101)...
->now we have to convert it in a formula as
        k=k+0.9/(pow(10,j)).here k=0.9 is taken
->and to display all the terms we have used loop

or

we can apply following technique:
//getting 0.9,0.99,0.999.....nth term
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float k=9,n,p=10,j,dis,m=1;
clrscr();
printf("enter number of terms\n");
scanf("%f",&n);
for(j=1;j<=n;j++)
{
dis=k/(pow(10,j));
printf("%f,",dis);
m++;
k=pow(p,m)-1;
}
getch();
}

->take 9 as a fixed value
->we enter no. of terms for 'n'.
->to display 0.9, we divide it (9) by 101
->to get 0.99, we take here 99, and for this we use 100-1 concept. we do this using k=pow(p,m)-1
        here pow finds power of 10(p).
->in next execution we get 0.99 and so on..

-> this is how loop continues and we get all terms

we can use different technique for same program.

         similarly we can also get 
1)9,99,999,9999......
2)2,22,222,2222....
3)5,55,555,5555......
4)0.99999,0.9999,0.999,0.99,0.9
    for this,
      there are 5 nines so it can be written as
         (100000-1)/105,(10000-1)/104........
         (105-1)/105,(104-1)/104........  
       now make a formula to get this one. And it is easy task...