Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Program to find sum of digits of a number . Please help with logical error of program.

Program to find sum of digits of a number . Please help with logical error of program.

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++
7 Posts 6 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 10938902
    wrote on last edited by
    #1

    #include"c:\turboc3\bin\header.cpp" /*i have created a file with all the header files*/
    void main()
    {
    long num,num1,i=0,j=0,multiplier=1,sum=0,a[100];
    clrscr();
    cout<<"Enter the number:";
    cin>>num;
    num1=num;
    while(num!=0)
    {
    num=num/10;
    i++;
    }
    for(j=i;j>0;j--)
    { multiplier=pow(10,j);
    a[j]=num/multiplier;
    num=num-(a[j]*multiplier);

    }
    for(j=0;j
    
    P D C 3 Replies Last reply
    0
    • U User 10938902

      #include"c:\turboc3\bin\header.cpp" /*i have created a file with all the header files*/
      void main()
      {
      long num,num1,i=0,j=0,multiplier=1,sum=0,a[100];
      clrscr();
      cout<<"Enter the number:";
      cin>>num;
      num1=num;
      while(num!=0)
      {
      num=num/10;
      i++;
      }
      for(j=i;j>0;j--)
      { multiplier=pow(10,j);
      a[j]=num/multiplier;
      num=num-(a[j]*multiplier);

      }
      for(j=0;j
      
      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      If you want to iterate the digits, then why get the number as a number rather than as a string? I'd get the string, iterate the digits, then get the numeric value of each digit, and then sum them. That makes getting the number of digits simpler as well.

      You'll never get very far if all you do is follow instructions.

      1 Reply Last reply
      0
      • U User 10938902

        #include"c:\turboc3\bin\header.cpp" /*i have created a file with all the header files*/
        void main()
        {
        long num,num1,i=0,j=0,multiplier=1,sum=0,a[100];
        clrscr();
        cout<<"Enter the number:";
        cin>>num;
        num1=num;
        while(num!=0)
        {
        num=num/10;
        i++;
        }
        for(j=i;j>0;j--)
        { multiplier=pow(10,j);
        a[j]=num/multiplier;
        num=num-(a[j]*multiplier);

        }
        for(j=0;j
        
        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        Are you wanting a "digital root" algorithm?

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

        1 Reply Last reply
        0
        • U User 10938902

          #include"c:\turboc3\bin\header.cpp" /*i have created a file with all the header files*/
          void main()
          {
          long num,num1,i=0,j=0,multiplier=1,sum=0,a[100];
          clrscr();
          cout<<"Enter the number:";
          cin>>num;
          num1=num;
          while(num!=0)
          {
          num=num/10;
          i++;
          }
          for(j=i;j>0;j--)
          { multiplier=pow(10,j);
          a[j]=num/multiplier;
          num=num-(a[j]*multiplier);

          }
          for(j=0;j
          
          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          The 'quick fix' of your bugs produce the following program:

          int main()
          {
          long num,num1,i=0,j=0,multiplier=1,sum=0,a[100];
          // clrscr();
          cout<<"Enter the number:";
          cin>>num;
          num1=num;
          while(num1!=0)
          {
          num1=num1/10;
          i++;
          }
          num1 = num;
          for(j=i-1;j>=0;j--)
          {
          multiplier=pow(10,j);
          a[j]=num1/multiplier;
          num1=num1-(a[j]*multiplier);
          }
          for(j=0;j

          However, I would rather write it this way:

          int main()
          {
          long num, sum = 0;

              cout<<"Enter the number:";
              cin>>num;
          
              while (num)
              {
                      int remainder = num % 10;
                      sum += remainder;
                      num /= 10;
              }
          
              cout <<"The sum of the digits of is:" << sum << endl;
          
              getchar();
          

          }

          THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?!
          -- C++ FQA Lite

          S S 2 Replies Last reply
          0
          • C CPallini

            The 'quick fix' of your bugs produce the following program:

            int main()
            {
            long num,num1,i=0,j=0,multiplier=1,sum=0,a[100];
            // clrscr();
            cout<<"Enter the number:";
            cin>>num;
            num1=num;
            while(num1!=0)
            {
            num1=num1/10;
            i++;
            }
            num1 = num;
            for(j=i-1;j>=0;j--)
            {
            multiplier=pow(10,j);
            a[j]=num1/multiplier;
            num1=num1-(a[j]*multiplier);
            }
            for(j=0;j

            However, I would rather write it this way:

            int main()
            {
            long num, sum = 0;

                cout<<"Enter the number:";
                cin>>num;
            
                while (num)
                {
                        int remainder = num % 10;
                        sum += remainder;
                        num /= 10;
                }
            
                cout <<"The sum of the digits of is:" << sum << endl;
            
                getchar();
            

            }

            THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?!
            -- C++ FQA Lite

            S Offline
            S Offline
            Stefan_Lang
            wrote on last edited by
            #5

            I'd like to remind you that you probably meant to write remainder, not reminder. ;) Good stuff though. :thumbsup:

            GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

            C 1 Reply Last reply
            0
            • S Stefan_Lang

              I'd like to remind you that you probably meant to write remainder, not reminder. ;) Good stuff though. :thumbsup:

              GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

              C Offline
              C Offline
              CPallini
              wrote on last edited by
              #6

              Stefan_Lang wrote:

              I'd like to remind you that you probably meant to write remainder, not reminder.

              :-O :-O :-O Thank you, fixed.

              THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

              1 Reply Last reply
              0
              • C CPallini

                The 'quick fix' of your bugs produce the following program:

                int main()
                {
                long num,num1,i=0,j=0,multiplier=1,sum=0,a[100];
                // clrscr();
                cout<<"Enter the number:";
                cin>>num;
                num1=num;
                while(num1!=0)
                {
                num1=num1/10;
                i++;
                }
                num1 = num;
                for(j=i-1;j>=0;j--)
                {
                multiplier=pow(10,j);
                a[j]=num1/multiplier;
                num1=num1-(a[j]*multiplier);
                }
                for(j=0;j

                However, I would rather write it this way:

                int main()
                {
                long num, sum = 0;

                    cout<<"Enter the number:";
                    cin>>num;
                
                    while (num)
                    {
                            int remainder = num % 10;
                            sum += remainder;
                            num /= 10;
                    }
                
                    cout <<"The sum of the digits of is:" << sum << endl;
                
                    getchar();
                

                }

                THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?!
                -- C++ FQA Lite

                S Offline
                S Offline
                Satya Chamakuri
                wrote on last edited by
                #7

                Hi, Don't use so many loops unnecessarly, here is the one way to get your answer in optimistic way. int main() { long num,num1,i=0,sum=0; short remainder = 0; cout<<"Enter the number:"; cin>>num; num1=num; while(num!=0) { remainder = num % 10; num = num / 10; sum = sum + remainder; i++; } cout<<"The sum of the digits of the "<

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups