Write a program that prompts the usr to input an integer and then output both the individual digits of the number and the sum of the digits. For example, it should output the following digits 3456 as 3 4 5 6, 8030 as 8 0 3 0, 2345526 as 2 3 4 5 5 2 6, 4000 as 4 0 0 0, and -2345 as 2 3 4 5. (I've tried so many of times and cannot get a solution for it. This is just for me, I'm trying to understand how to solve these logical problems, but I seem to have a hard time with it.)
Thank you for this solution, unfortunately I cannot used this solution because I have not learned arrays yet. inside your for loop block in the if statement you're declaring digits[i] which is an array. In the chapter that I've just learned it's only nested loops or loops. I know what an array is but in this chapter I'm trying to solve the problems from what I've learned.
I've tried it a different way using dowhile and I still can't get the correct solution, at this point I would consider myself a loser because I cannot figure this out.
Ok let's go through some things here. First of all you are using the variable digits but you haven't given it any value yet. So digits>10 inside your if statement is wrong. Even if you did initialized though what values is it meant to hold?
I just edited do while loop and I saw what you meant, it wasn't initializing to anything, so I fixed it. So any whole digits less than 10 will be outputted.
Ok, lets look at an example here, it doesnt completely write the whole thing for you but will give you a good starting point and a better understanding on how to break down the digits.. lets say I enter 1001 as a integer and store it in Num.
We first create a loop to loop around until Num hits zero.
So, inside my loop...
I first do modulus with 10 (1001 % 10) which gives gives a result of 1 (my first digit)
I then divide my number by 10 giving 100
I add my modulus result (1 so far) to my total
Loop again as Num is 100 and still over 0
I do modulus with 10 (100 % 10) which gives gives a result of 0 (my second digit)
I then divide my number by 10 giving 10
I add my modulus result (0) to my total
Loop again as Num is 10 and still over 0
I do modulus with 10 (10 % 10) which gives gives a result of 0 (my third digit)
I then divide my number by 10 giving 1
I add my modulus result (0) to my total
Loop again as Num is 10 and still over 0
I do modulus 10 which gives gives a result of 1 (my fourth digit)
I then divide my number by 10 giving 0
I add my modulus result (1) to my total
Num is now 0 so we break from loop.
Total now would hold the sum of digits.
A bit of pseudo:
1 2 3 4 5 6 7
Program Digits
Read Integer into Num
While Num > 0
Digit = Num % 10
Num = Num / 10
Total = Total + Digit
End While
#include <iostream>
int main()
{
int myNum; // holds number I input
int total = 0; // holds sum of digits.
int digit; // temp to hold each digit
std::cout << "Enter Integer: ";
std::cin >> myNum;
while (myNum>0)
{
digit = myNum % 10;
myNum /= 10;
total += digit;
}
std::cout << std::endl << "Sum of digits is: " << total;
return 0;
}
Is this correct? Thank you for the reply and explaining how to break down the digits, it made sense, I just edit your code by adding another while loop if I did it correct, and I do not take credit for this because this your work, so thank you for explaining to me. I think I'm going to take a break from solving problems and really take my time on learning the syntax and understand the concept of them. But again, thank you, you're awesome!
#include <iostream>
usingnamespace std;
int main()
{
int myNum;
int total = 0;
int digit;
cout << "Enter an integer" << endl;
cin >> myNum;
while(myNum >0)
{
digit =myNum %10;
myNum/=10;
total += digit;
}
while (myNum < 0){
digit =myNum %10;
myNum /=10;
total +=digit;
}
cout << "The sum of digit is:" << total << endl;
return 0;
}
Oh, what would I need to add the total of a negative number? Because it said in the problem we need to output the sum of the -2345. So I thought if I created another loop it would be fine because it did that. I think I rush myself working on the problems without learning the concept of the syntax, because I just finish taking my program 1 class this semester, and I did not learn a thing in the class, but manage to finish with a decent grade a B. So it's my 2nd week on learning C++ on my own I start it all over. And I thought I was ready to solve problems but I guess I'm not.
^
Turn it to positive by using absolute value?
Although does he want the first integer to remain negative?
If you were to turn them all into positive and add them you would get
2+3+4+5 = 14
If the first integer remains negative, you would get
-2 + 3 + 4 + 5 =10
I think it was the total of the -2345 to equal a negative number of that output. But I was thinking the same it could be the 2nd option. I'm not sure though in the practice problem from the book it did not specify what they are looking for after output -2345.
Sorry I see why you added the second while loop.. had a memory blank earlier and forgot it needed to support negative numbers. I would think the negative would be ignored and the digits added together in the normal way, but I may be wrong.. depends what he wants.
So if you entered -1001, you expect -2? - seems a little odd to me, or as Momothegreat said is it to take into account the first digit being -1 giving result of 1
If you expect -2 then your while loop would return that.
I think you're right because -1001 should be 0 and not -2, but then again if -1001 is negative why wouldn't the other digits after after -1 would not be negative? For example if you take the sum of two negative digits shouldn't it result as a negative? Like -1-1=-2 or in this -1 + (-1) =-2