Long multiplication Help

Alright so first I'll ask my question and than post what my professor wants. First I'm trying to line the numbers up on the right side just like he does in the examples. Next I have to show of the each steps, and I'm not exactly sure how to do that or if I'm missing something :) Havent coded in a while. Also, if I'm missin anythin else please let me know! Thank you in advance!! Also I'm not exactly done with everythin in the code so I'll post updates.

Now here is what my professor says : In traditional "long multiplication" we determine the product of two integers, x and y, by
multiplying x and the individual digits of y, in turn, starting with the unit’s digit. The results of these
multiplications are arranged appropriately and added, yielding the completed product. The
representation of these operations are usually done in a particular manner. For example, the
multiplication of 123 by 95 is:
123
95
x ---
615
1107
-----
11685
The numbers to be multiplied, x and y, are each displayed on a separate line, followed by a horizontal
line. The results of multiplying each digit of y by x are then displayed on separate lines, followed by
another horizontal line, and then the final product. Write a C++ program to perform a sequence of
such multiplications, displaying the results in this traditional representation. Input will be two
integers, x and y from the keyboard. Each integer will have no more than 10 digits.
For each pair of integers, perform the multiplication of x by y, displaying the results similar to
example shown above. If y contains only a single significant digit, omit the second horizontal line and
the sum (since it would be overkill). Display 0 digits only when they are significant. The number of
hyphens (-) in the first horizontal line should be the same as the number of digits in the larger value of
x and y. The number of hyphens in the second horizontal line, if it is produced, should be the same as
the number of digits in the product of x and y. Output the letter x for the multiplication symbol in the
answer. Check only for positive numbers (including 0). Finally, ask the user if he/she wishes to run the
program again. No credit will be given for just giving the answer without showing the steps involved.
Output should look similar to below.
Sample Runs:
Enter the first number: 4
Enter the second number: 7
4
7
x --
28
Run again (Y/N)? y
Enter the first number: 135
Enter the second number: 46
5
135
46
x ---
810
540
----
6210
Run again (Y/N)? N

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 namespace std;

int main()

{

	int X;  //Variable for the first number
	int Y;  // Variable for the second number
	char Answer;   //Variable for the run again 


	cout << "Please enter the First number :";
		cin >> X;

	cout << "Please enter the Second number : ";
		cin >> Y;

		int Total = X * Y;           // To get the multiplication total

		cin.get();


		cout << X << endl;      //Displays the first number
		cout << Y << endl;        //Displays the Second number
		cout << "x----" << endl;
		
		cout << Total << endl;

		cout << "Run Again? (Y/N) :";
			cin >> Answer;	
		

}
Last edited on
Could you reformat your questions so your question/what you have tried/what your teacher said is separate from each other? Your description is so hard to read.

Anyway, line up right output by cout<<std::right; (need include iomanip ) you can simply find in this site by search "right" on the very top search box, didn't you tried that?
Last edited on
Unisaurus,

Your program is fine down to cin >> Y after that it is more involved than Total = X * Y. What your professor wants long multiplication not 123 * 95, but 5 * 3 carry the 1 then 5 * 2 + 1and carry the 1 and then 5 * 1 + 1 to get 615. Then do the same with the 9 and finely add the 615 and 11070 for your final answer.

You might consider changing X and Y to strings to pick apart individual numbers. It is easy to change to an int with stoi().

In the end printing out the results is a minor part.

In your OP line 21 should be just above the return 0; that you missed. Lines 24 - 31 will work OK.

Hope that helps,

Andy
Last edited on
Alright y'all I've gotten the code to be able to run again, now I just have to do the steps, and calculate the 'x---' to equal the numbers. If that makes sense.

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
45
46
47
48
#include <iostream>
#include <iomanip>
using namespace std;



int main()

{

	int X;  //Variable for the first number
	int Y;  // Variable for the second number
	char Answer;   //Variable for the run again 
	int carry = 0;

	do
	{
		cout << "Please enter the First number :";
		cin >> X;

		cout << "Please enter the Second number : ";
		cin >> Y;

		int Total = X * Y;           // To get the multiplication total

		cin.get();

		cout << right << endl;
		cout << X << endl;      //Displays the first number
		cout << Y << endl;      //Displays the Second number
		cout << "x-----" << endl;
		cout << Total << endl;


		cout << "Run Again? (Y/N) :";
		cin >> Answer;
	}
			while (Answer == 'y' || Answer == 'Y');

			


			return 0;
		
		cin.get();

}
Here you go:
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
  #include <iostream>
using namespace std;

int main()

{

	int X;  //Variable for the first number
	int Y;  // Variable for the second number
	char Answer;   //Variable for the run again 


	cout << "Please enter the First number :";
		cin >> X;

	cout << "Please enter the Second number : ";
		cin >> Y;

		int Total = X * Y;           // To get the multiplication total

		cin.get();


		cout << X << endl;      //Displays the first number
		cout << Y << endl;        //Displays the Second number
		cout << "x----" << endl;
		while(Y != 0) {//this loop will reverse the Y element
        int mod = Y%10;
        int reverse = reverse*10 + mod;//for more reverse elements
        Y/=10;
        cout<<X*mod<<endl;//multiplying the reverse element with the X element
        
    }
		
		cout << Total << endl;

		cout << "Run Again? (Y/N) :";
			cin >> Answer;	
		

}



Please enter the First number :135
Please enter the Second number : 46
135
46
x----
810
540
6210
Run Again? (Y/N) :n


HOPE IT HELPS
Last edited on
Alright y'all I've gotten almost everything done. All I have to do the hyphens. I need to figure out how to make the hyphens equal the largest number.

146
15
X--- <----This needs to come out equaling the largest number in the equation
730
1460
X---- <----As well as this one, and it needs to only needs to pop up when it is like this and not Total single digit equation.

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
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <iomanip>
using namespace std;



int main()

{

	int X;  //Variable for the first number
	int Y;  // Variable for the second number
	char Answer;   //Variable for the run again 
	

	do
	{
		cout << "Please enter the First number :";
		cin >> X;

		cout << "Please enter the Second number : ";
		cin >> Y;

		int Total = X * Y;           // To get the multiplication total

		
		
		cout << setw(5) << X << endl;      //Displays the first number
		cout << setw(5) << Y << endl;      //Displays the Second number
		
		cout << X << endl;
		while (Y != 0)                    //this loop will reverse the Y element
		{
			 
			int math = Y % 10;
			Y /= 10;
			cout << X * math << endl;
		}

		
		cout << setw(5) << Total << endl;
		

		cout << "Run Again? (Y/N) :";
		cin >> Answer;
	}
		
		while (Answer == 'y' || Answer == 'Y');

			


	cin.get();

	return 0;

}
Alright so can someone check this for me. I need to make the dashes equal the largest number, but not sure how. I think I have all of the numbers lined up, now I just have to figure out how to get ride of the second dash line for a single digit answers.
7
1
--
7

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <iomanip>

using namespace std;



int main()

{

	int X;  //Variable for the first number
	int Y;  // Variable for the second number
	char Answer;   //Variable for the run again 


	do
	{
		cout << "Please enter the First number :";
		cin >> X;

		cout << "Please enter the Second number : ";
		cin >> Y;

		int Total = X * Y;           // To get the multiplication total
		


		cout << setw(5) << X << endl;      //Displays the first number
		cout << setw(5) << Y << endl;      //Displays the Second number 
		cout << "+-----";

		while (Y != 0)                    //this loop will reverse the Y element
		{

			int math = Y % 10;
			Y /= 10;
			int reverse;
			reverse * 10 + math;
			
			{
				if (math >= 2)
				{
					cout << setw(5) << endl;
				}
				else
				{
					cout << setw(4) << endl;
				}

				cout << X * math << setw(5) << endl;
			}
		
		}

		cout << "X------" << endl;
		cout << setw(5) << Total << endl;

		cout << "Run Again? (Y/N) :";
		cin >> Answer;
	}

	while (Answer == 'y' || Answer == 'Y');




	cin.get();

	return 0;

}
Topic archived. No new replies allowed.