# 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,...
show more
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.

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.

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

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

Follow

2 answers

Are you sure you want to delete this answer?