Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3430708
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:14:13+00:00 2026-05-18T07:14:13+00:00

I’m trying to learn c++ on my own and I’ve hit a bit of

  • 0

I’m trying to learn c++ on my own and I’ve hit a bit of a road block. The problem is I need to take an integer,split it into its digits and get the sum of the digits and display them.

Example:

input number: 123456
digits in the integer: 1 2 3 4 5 6
sum: 21

I have it all done, but when I rip the integer, into digits I can’t display it correctly. It displays in reverse order.

So in the program below I enter 1234 and it spits out 4 3 2 1. I know why, I just don’t know how to fix it.

Here is my code so far:

#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include <math.h>

int countDigitsInInteger(int n)
{
    int count =0;
    while(n>0)
    {
       count++;
       n=n/10;
    }
    return count;
}

using namespace std;

int main(int argc, char *argv[])
{  
    int intLength =0;
    int number;
    int digit;      
    int sum = 0;
    string s;    
    cout << "Please enter an integer ";
    cin >>number;
    cout << "Orginal Number = "<<number <<endl;
    //make the number positive
    if (number<0)
    number = -number;    
    intLength = countDigitsInInteger(number);
    //break apart the integer into digits
    while(number>0)
    {                         
        digit = number % 10;
        number = number / 10;        
        cout <<digit << " "; 
        sum = sum+digit; 
    } 
    cout <<endl <<"Sum of the digits is: "<<sum<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

Here is my solution

can’t see 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T07:14:14+00:00Added an answer on May 18, 2026 at 7:14 am

    Your problem comes from the fact that you are reading the digits backwards, thus you need to print them out backwards. A stack will help you tremendously.

    #include "stdafx.h"
    #include <cstdlib>
    #include <iostream>
    #include <math.h>
    #include <stack>
    
    int countDigitsInInteger(int n)
    {
        int count =0;
        while(n>0)
        {
            count++;
            n=n/10;
        }
        return count;
    }
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {  
        int intLength =0;
        int number;
        int digit;      
        int sum = 0;
        string s;    
        cout << "Please enter an integer ";
        cin >>number;
        cout << "Orginal Number = "<<number <<endl;
        //make the number positive
        if (number<0)
            number = -number;    
    
        intLength = countDigitsInInteger(number);
        //break apart the integer into digits
    
        stack<int> digitstack;
        while(number>0)
        {                         
            digit = number % 10;
            number = number / 10;
            digitstack.push(digit);
            sum = sum+digit; 
        }
    
        while(digitstack.size() > 0)
        {
            cout << digitstack.top() << " ";
            digitstack.pop();
        }
    
        cout <<endl <<"Sum of the digits is: "<<sum<<endl;
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    

    Oh, and BTW, keep your indentation clean. Its important.

    EDIT: In response to Steve Townsend, this method is not necessarily overkill, it is just different from yours. The code can be slimmed down so that it seems less like overkill:

    #include <iostream>
    #include <stack>
    #include <string>
    
    using namespace std;
    
    int getInput(string prompt)
    {
        int val;
        cout << prompt;
        cin >> val;
        return val < 0 ? -val : val;
    }
    
    int main(int argc, char** argv)
    {
        int num = getInput("Enter a number: ");
        cout << "Original Number: " << num << endl;
    
        stack<int> digits;
        int sum = 0;
        while(num > 0)
        {
            digits.push(num % 10);
            sum += digits.top();
            num = num / 10;
        }
    
        while(digits.size() > 0)
        {
            cout << digits.top() << " ";
            digits.pop();
        }
    
        cout << endl << "Sum of digits is " << sum << endl;
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to select an H1 element which is the second-child in its group
I am currently running into a problem where an element is coming back from
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.