+92 332 4229 857 99ProjectIdeas@Gmail.com

Sum Ndigit number uptill 1digit-Recurssion (C++)


Sum Ndigit number uptill 1digit through recurssion
sumUptil1Dig() is a recurssive function which takes a number in form of string as input parameter and sums that number untill the value of the sum comes out to be a 1 digit number.
For example:
Let’s take a number i.e.: 1234567899
This function sums each digit until a single digit sum value is obtained as shown below:
  1  +  2  +  3  +  4  +  5  +  6  +  7  +  8  +  9  +  9  =  54
  5  +  4  =   9

sumUptil1Dig() uses convertCharToInt() function to convert a char value to its decimal form.
convertCharToInt() takes the character and returns its decimal form.
For example:
The decimal form of ‘0’ is 0
The decimal form of ‘1’ is 1
The decimal form of ‘2’ is 2
The decimal form of ‘3’ is 3
The decimal form of ‘4’ is 4
The decimal form of ‘5’ is 5
The decimal form of ‘6’ is 6
The decimal form of ‘7’ is 7
The decimal form of ‘8’ is 8
The decimal form of ‘9’ is 9

sumUptil1Dig() is valid only for below mentioned characters:
0 1 2 3 4 5 6 7 8 9

Code

#include "stdafx.h"
#include "iostream"
#include "iomanip"
#include "conio.h"

using namespace std;

int convertCharToInt(int num)
{
       if(num==48)
              return 0;
       if(num==49)
              return 1;
       if(num==50)
              return 2;
       if(num==51)
              return 3;
       if(num==52)
              return 4;
       if(num==53)
              return 5;
       if(num==54)
              return 6;
       if(num==55)
              return 7;
       if(num==56)
              return 8;
       if(num==57)
              return 9;
return -1;
}

int sumUptil1Dig(char*str)
{
       int len=strlen(str);
       int sum=0;

       for(int i=0;str[i]!='\0';i++)
       {
              if(str[i+1]=='\0')
                     cout<<setw(3)<<str[i]<<setw(3);
              else
                     cout<<setw(3)<<str[i]<<setw(3)<<"+";
              sum=sum+convertCharToInt(str[i]);
       }
      
       char tempStr[10];
       itoa(sum,tempStr,10);
       int l=strlen(tempStr);

              cout<<"="<<setw(4)<<sum<<endl;

       if(l==1)
              return 0;
       else
              sumUptil1Dig(tempStr);
}

int main()
{     
       cout<<"         9 : "<<endl;sumUptil1Dig("9");cout<<endl;cout<<endl;
       cout<<"        99 : "<<endl;sumUptil1Dig("99");cout<<endl;cout<<endl;
       cout<<"       999 : "<<endl;sumUptil1Dig("999");cout<<endl;cout<<endl;
       cout<<"      9999 : "<<endl;sumUptil1Dig("9999");cout<<endl;cout<<endl;
       cout<<"     99999 : "<<endl;sumUptil1Dig("99999");cout<<endl;cout<<endl;
       cout<<"    123456 : "<<endl;sumUptil1Dig("123456");cout<<endl;cout<<endl;
       cout<<"   1234567 : "<<endl;sumUptil1Dig("1234567");cout<<endl;cout<<endl;
       cout<<"  12345678 : "<<endl;sumUptil1Dig("12345678");cout<<endl;cout<<endl;
       cout<<" 123456789 : "<<endl;sumUptil1Dig("123456789");cout<<endl;cout<<endl;
       cout<<"1234567899 : "<<endl;sumUptil1Dig("1234567899");cout<<endl;cout<<endl;

       _getche();
       return 0;
}

Output
         9 :
  9  =   9


        99 :
  9  +  9  =  18
  1  +  8  =   9


       999 :
  9  +  9  +  9  =  27
  2  +  7  =   9


      9999 :
  9  +  9  +  9  +  9  =  36
  3  +  6  =   9


     99999 :
  9  +  9  +  9  +  9  +  9  =  45
  4  +  5  =   9


    123456 :
  1  +  2  +  3  +  4  +  5  +  6  =  21
  2  +  1  =   3


   1234567 :
  1  +  2  +  3  +  4  +  5  +  6  +  7  =  28
  2  +  8  =  10
  1  +  0  =   1


  12345678 :
  1  +  2  +  3  +  4  +  5  +  6  +  7  +  8  =  36
  3  +  6  =   9


 123456789 :
  1  +  2  +  3  +  4  +  5  +  6  +  7  +  8  +  9  =  45
  4  +  5  =   9


1234567899 :
  1  +  2  +  3  +  4  +  5  +  6  +  7  +  8  +  9  +  9  =  54
  5  +  4  =   9

0 comments: