Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Numerical AnalysisRSS Feeds

Program to compute and print out the decimal notation for the reciprocal of each input integer

Posted By: Easy Tutor     Category: C++ Programming     Views: 22645

Write a program to compute and print out the decimal notation for the reciprocal of each input integer.

The input to your program will contain a list of such positive integers, one per line. Each integer will be greater than 1 and will contain no more than 72 digits. The input will be terminated by a zero appearing on a line itself. No integers whose reciprocals are infinitely repeating decimals will be entered.

Your output should print the reciprocal for each input integer in decimal form.

Code for Program to compute and print out the decimal notation for the reciprocal of each input integer in C++ Programming

 # include <iostream.h>
 # include <fstream.h>
 # include <string.h>
 # include <stdlib.h>
 # include <conio.h>
 # include <math.h>

 int main( )
    {
       clrscr( );

       fstream File("CP-08.txt",ios::in|ios::nocreate);

       if(!File)
      {
         cout<<"\n Unable to open the input file."<<endl;
         cout<<"\n Press any key to exit.";

         getch( );
         exit(EXIT_FAILURE);
      }

       char Quotient[5]={NULL};
       char Input_integer[85]={NULL};
       char Reciprocal[200]={NULL};

       char *Endptr=NULL;

       int quotient=0;

       longdouble divisor=0;
       longdouble divident=0;
       longdouble remainder=0;

       do
      {
         strset(Input_integer,NULL);
         strset(Reciprocal,NULL);

         File.getline(Input_integer,80);

         if(strcmp(Input_integer,"0")==0)
        break;

         divisor=0;
         divident=1;
         quotient=0;
         remainder=0;
         Endptr=NULL;

         divisor=_strtold(Input_integer,&Endptr);

         if(divisor>1)
        {
           divident=10;

           strcpy(Reciprocal,"0.");
        }

         while(divisor>divident)
            {
          divident*=10;

          strcat(Reciprocal,"0");
            }

         do
        {
           remainder=fmod(divident,divisor);
           quotient=divident/divisor;
           divident=remainder;

           strset(Quotient,NULL);
           itoa(quotient,Quotient,10);
           strcat(Reciprocal,Quotient);

           if(divisor>divident)
              divident*=10;

           if(divisor>divident)
              {
             divident*=10;

             strcat(Reciprocal,"0");
              }

           if(divident<divisor)
              break;
        }
         while(1);

         if(Reciprocal[strlen(Reciprocal)-1]=='0')
        Reciprocal[strlen(Reciprocal)-1]=NULL;

         cout<<Reciprocal<<endl;
      }
       while(1);

       getch( );
       return 0;
    }

 
  
Share: 



Easy Tutor
Easy Tutor author of Program to compute and print out the decimal notation for the reciprocal of each input integer is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
Patriciar Carlton from India Comment on: Dec 10
A computer programmer is never out of job! Every organization keeps looking for smart programmers who can write, test, debug and control computer programs.Shares to Buy

View All Comments