output

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
im 13 and im new in c++.
try this

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace 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.
this might be wrong but how about this?

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace 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;
}
Thanks, I had no idea that solution was related to math, I was looking everywhere else. My output isn't exact but I am happy with it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cmath>

using namespace 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;
}
nice job yoked, you get the idea so you should be able to figure it out. if not ask away.
Last edited on
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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44



#include <iostream>
using namespace std;

int main ()
{


int a,e,i,j;
double b,d,g;
double f[30];
e=g=0;


  cout << "Input Number:  ";
  cin >> a;

  if (a<0)a = a*-1;


do  {

  b=a; e=e+1; f[e]=d=((b/10)-(a/10))*10; a=a/10; g=g+d;
 
  }  while ( a>=1);

  cout << "In Order: ";
  for(i=e;i>=1;i--) {cout << f[i] << " ";}
  cout << "\n";

  cout << "Reverse Order: " ;
  for(j=1;j<=e;j++) cout << f[j] << " ";
  cout << "\n";

  cout << "Sum of the Digits: " << g;
  cout << "\n";


  return 0;
}





Input Number: -8954768
In Order: 8 9 5 4 7 6 8
Reverse Order: 8 6 7 4 5 9 8
Sum of the Digits: 46

Last edited on
@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>

using namespace 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.
((b/10)-(a/10))*10
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.
I took a wrong turn in Albuquerque,,,
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#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;
}


Last edited on
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.
Last edited on
3.5/10.0

is that my score?


edited, i assume not as I don't use array
Last edited on
Topic archived. No new replies allowed.