Skip to search.

Breaking News Visit Yahoo! News for the latest.

×Close this window

  1. Home >
  2. All Categories >
  3. Computers & Internet >
  4. Programming & Design >
  5. Resolved Question
m2009 m2009
Member since:
January 26, 2009
Total points:
1,376 (Level 3)

Resolved Question

Show me another »

C++ Programming Exercises?

1. Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits. For example, it should output the individual digits of 3456 as 3 4 5 6, output the individual digits of 8030 as 8 0 3 0, output the digits of 2345526 as 2 3 4 5 5 2 6, the digits of 4000 as 4 0 0 0, and the digits of -2345 as 2345.


2. Write a program that prompts the user to input an integer and then outputs the number with the digits reversed. For example, if the input is 12345 the output should be 54321. Your program should also output 5000 as 0005 and 980 as 089

These are the only two programs I am stuck on, and they're are probably the easiest!



Here is what 'i have so far, for the first one, of course it isn't going to work because I shouldn't use n in the while loop but i don't know what to do to fix it.



#include <iostream>



using namespace std;



int main()
{

int number = 0, n = 1, numdigit = 0, digit = 0;


cout << "Enter the number: ";
cin >> number;
cout << endl << endl;



cout << "The digits of your number are : ";


do{
digit = number / 10^n;

cout << digit << " ";

n++;

}while( n > 0 );




system ("pause");
return 1;
}

I Really have no idea of what to do, whe didn't do much in class with extracting digits.

Additional Details

What would I have to do to a string to make the digits print out individually?

4 years ago

Okay that works, but how do I get the numbers to output forwards instead of reversed?

4 years ago

Farzan by Farzan
Member since:
April 05, 2008
Total points:
1,393 (Level 3)

Best Answer - Chosen by Asker

Hi dude,

Your while loop will never gets end because n variable will always remains greater than zero until it overflows.

Replace your while loop with the following one:

// store number in a temporary variable,
// or you can use number variable directly.
int temp = number;
// for negative numbers:
if (temp < 0) temp *=-1;

while (temp)
{
cout << (temp % 10) << " ";
temp /= 10;
}

======================================
// First calculate number of digits:
int digitsNum = 0;
int temp = number;
if (temp < 0) temp *=-1;
for (; temp; temp/=10, digitsNum++);

// after that...
int *list = new int[digitsNum];
temp = number;
if (temp < 0) temp *=-1;

// Store digits in the list
for (int i=0; temp; i++, temp /= 10)
*(list + i) = (temp % 10);

// At last, print it at reverse side
for (int i=digitsNum - 1; i >= 0; i--)
cout << list[i] << " ";

good luck...

Source(s):

My knowledge and experience
Asker's Rating:
5 out of 5
Asker's Comment:
Thanks!

There are currently no comments for this question.

Other Answers (1)

  • The Phlebob by The Phlebob
    Member since:
    March 24, 2007
    Total points:
    393,847 (Level 7)
    Your code could be much simpler if you input the number as a string and manipulated the characters rather than dealt with an int. For one thing, you wouldn't have a problem with numbers that exceed the range of an int (often -32767 to +32768 or so).

    As a footnote to that, since you do have to do some numerical manipulation, note that any ASCII digit can be converted to its integer value simply by subtracting the character '0' from it.

    Hope that helps.

Answers International

Yahoo! does not evaluate or guarantee the accuracy of any Yahoo! Answers content. Click here for the Full Disclaimer.

Help us improve Yahoo! Answers. Send Feedback