Hi I really appreciate you taking the time to help me. I'm in my first semester in C++ and I'm having a lot of trouble with this lab. I just learned what the while loop is and using a counter in it. My problem needs to prompt the user to input an integer and then outputs both the individual digits of the number and the sum of the digits. An example would be entering 8030 and it spits out 8 0 3 0 as well as 8+0+3+0=11 and it needs to work with negative numbers. I've spent hours trying to figure this out and I can't so I would appreciate any help I could get. I'll post what I have so far (which isn't much sorry).
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
int base;
int fouth;
int first;
int second;
int third;
cout << " Please enter a whole number." << endl;
cin >> base ;
third = base % 45;
first = base / 1000;
second = base / 500;
fouth = base % 10;
cout << first << " " << second << " " << third<< " " << fourth << " " << base << endl;
return 0;
}
Now I don't know if any of this is right a hint my professor gave us is that to get the fourth digit you would do base mod 10 and to get the first digit you do base divided 1000 but for the rest I have no idea. Below is another hint he gave us
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
{
int power;
int counter=0;
int value=1;
cout << "Enter the power of 10 you want: ";
cin >> power;
while (counter < power)
{
value = value * 10;
counter++;
}
cout << "The " << power << " of 10 is: " << value << endl;
return 0;
}
Again thank you a lot for your help I'm completely lost
Thank you Smac89 your first hint helped me figure out how to isolate the second and third digits! However I haven't learned anything about strings yet so I couldn't use that to help me. The program is supposed to work for any amount of numbers however I have no idea where to go about doing that, so I'll stick to four for now. I really appreciate your help though, the last step I have to do is make it so negative numbers work as well. I tried adding an IF statement but it still isn't working correctly, perhaps you could take a look?
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
int base;
int fourth;
int first;
int second;
int third;
int power;
int counter=0;
int value;
int love;
int heart =0;
cout << " Please enter a whole number." << endl;
cin >> base ;
if (base<0)
base = base + (base*2);
else
first = base / 1000;
fourth = base % 10;
while (counter<base)
{
value=base/10;
counter++;
}
third = value % 10;
while (heart<value)
{
love=value/10;
heart++;
}
second = love % 10 ;
cout << first << " " << second << " " << third << " " << fourth << endl;
cout <<"The invdividual digits added together = " << first+second+third+fourth << endl;
return 0;
}
You can use an array to store all the digits you find or if you know how to write recursive functions, you can recursively repeat hint 1 then display the integers as the recursion unwinds.
Also note that the hint 1 can be applied multiple times to the integer until it gets to zero. So another hint:
1234 % 10 = 4
1234 / 10 = 123
123 % 10 = 3
123 / 10 = 12
...
You can get the length of a number by continuously dividing it by 10 until it gets to zero or recognize that this is the same as taking the log10 of the number and adding 1.