I am a student in my first programming course CS 101. I have learned control structures, file input/output, and the basics. This assignment requires data manipulation without using any user-defined functions: "Write a program that prompts the user to input an integer and the outputs the 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, and the sum 18. It should also output the digits of -2345 as 2 3 4 5."
#include <iostream>
using namespace std;
int main()
{
int a, b, c, d;
cout<<"Enter a four digit number sperated by spaces: "<<endl;
cin>>a>>b>>c>>d;
cout<<endl<<a<<endl<<b<<endl<<c<<endl<<d<<endl;
return 0;
}
This was a previous assignment, but the number of integers used was limited to 4 and I am lost about how to read each integer as a seperate variable without knowing how many integers are going to be used. The program does not want me to ask the user, so setting up a loop control variable is out of the question. Does anyone have any pointers or advice? Thank you much!
The next assignment is asking me to output the number in reverse, i.e. if the input is 12345, then the output needs to be 54321. The cin.get() function only takes char as a parameter, or is there a way that I can implement that here and with the above assignment?
AnyHelp = MuchAppreciated
#include <iostream>
usingnamespace std;
int main()
{
int a, b, c, d, e;
cout<<"Enter a five digit number with spaces and i will reverse it:"<<endl;
cin>>a>>b>>c>>d>>e;
cout<<e<<" "<<d<<" "<<c<<" "<<b<<" "<<a<<endl;
return 0;
}
That is how I thought about coding it from the beginning, but 1 problem: what if the input number is more than 5 integers? My test data ranges from -2345 to 2345526, I need a way to seperate the integers with the same variable so I can add them as well.
sum = sum + num;
The answer is right there but it's not coming to me. And I've been at this one all day, maybe if I started when I was 13, lol. Programming is some of the coolest stuff I've ever seen though and I can't get enough.
#include <iostream>
usingnamespace std;
int main()
{
int a, b, c, d, e;
cout<<"Enter a five digit number with spaces and i will reverse it and add them:"<<endl;
cin>>a>>b>>c>>d>>e;
cout<<e<<" "<<d<<" "<<c<<" "<<b<<" "<<a<<endl;
cout<<"the sum is: "<<e + d + c + b + a<<endl;
return 0;
}
#include <iostream>
#include <cmath>
usingnamespace std;
int main()
{
int i;
int number, sum, temp;
cout<<"Enter a positive integer: "<<endl;
cin>>number;
cout<<endl;
temp = number;
sum = 0;
do
{
sum = sum + number % 10; //extract last digit and add to sum
number = number / 10; //remove the last digit
}while(number > 0);
cout<<temp<<endl;
cout<<"The sum of the digits = "<<sum<<endl;
cin>>i;
return 0;
}
This is what I came up with, I am posting it because you marked the issue as being resolved, I dont know how kosher it is to mix ints and doubles in equasions, ill probably get slapped around for doing it,, but it seems to be exact, and accomplishes the parameters you were looking for(separating digits, listing in order, reverse order, removing negative denotation, sum of the digits)
@BettyBoopTS, Thank you for posting. That is some really complex math for my brain to compute, brilliant stuff there. I tested it with some of my data and all output fits to a tee. I haven't "officially" covered arrays in college, I do know how to implement them (sort of) and I was hoping to complete this without implementing them, I'm still toying around with the mod operator and dividing the whole number by 10. This peice is working for the first part of my test data: 12345. I'm getting close!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
usingnamespace std;
int main()
{
int number, temp;
cout<<"Enter a number: "<<endl;
cin>>number;
cout<<endl;
temp = number;
do
{
number = number / 10;
temp = (number % 10) + 1;
cout<<temp<<" ";
}while(number > 0);
return 0;
}
very good, I can see a solution with printing the individual numbers out in order, and getting the sum, but I am at a loss presently on how to output them in reverse order without the implementation of an array, but I am sure it probably there is relatively straight forward way to do it, it just eludes me at the moment.
What in the name of-
The formulas involved in solving this are quite simple. How did you manage this?
Breaking down a number starting from the least significant digit is actually simpler than from the most significant one. It's just iterated modulo and division (except for base 2, which is iterated AND and shift). Starting from the most significant digit involves either digit counting or logarithms, but once that's done, the process is very similar.
ok here is an example I am posting only because for it to be a good program it requires more code, which is an exercise you can do. I suggest looking at ways you can perhaps find the length or number of digits of the number, perhaps string class? anyway... This code only excepts numbers with 5 digits.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int number, sum;
int dig1, dig2, dig3, dig4, dig5;
cout << "Enter a number: " << endl;
cin >> number;
dig1 = number / 10000;
dig2 = number / 1000 % 10;
dig3 = number / 100 % 10;
dig4 = number % 100 / 10;
dig5 = number % 10;
sum = dig1+dig2+dig3+dig4+dig5;
cout << "The digits of the number you entered was: "
<< endl << dig1 << endl << dig2 << endl
<< dig3 << endl << dig4 << endl << dig5 << endl;
cout << "The sum of all digits is: " << sum << endl;
cout << "The number backwards is: " << dig5 << dig4
<< dig3 << dig2 << dig1 << endl;
return 0;
}
3.5/10.0
-5.0: Breaks with sufficiently large input.
-.8: Full loop unwinding.
-.2: Inconsistent expressions.
-.5: Doesn't follow the convention of using index zero for the least significant digit.