One more line of code I need to make it work

My program inputs two integers and determines if the first number is a multiple of the second number. I want my program to output three things:

If the first number is a multiple of the second then say it.

If the modulus operation cannot be performed because we are dividing by zero then output a message saying so.

If the first number is not a multiple of the second then say it.

Here is my code but I need to add a line saying if the first number is not a multiple of the second but I don't know how to incorporate that:

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
int x, y, remainder;

cout << "Enter first integer: ";
cin >> x;
cout << "Enter second integer: ";
cin >> y;
cout << endl;

if(y != 0 && (x % y) == 0)
cout << x << " is a multiple of " << y;

else
cout << "Cannot divide by zero. Operation aborted...";

getch();
return 0;
}
I think I got it figured out. I duplicated the if condition but now added and else if.
Last edited on
Cool you got it , your code look like:

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
#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
int x, y, remainder;

cout << "Enter first integer: ";
cin >> x;
cout << "Enter second integer: ";
cin >> y;
cout << endl;

// x % y
if (y != 0) {
	if (x % y == 0)
		cout << "x: " << x << " is a multiple of " << "y: " << y << endl;
	else
		cout << "x: " << x << " is not a multiple of " << "y: " << y << endl;
} else
	cout << "Can not divided by zero, operation faild.";

getch();
return 0;
}

I think your original code is preferable. 4 is not a multiple of 0, so if I enter 4 and 0, it should just say that 4 isn't a multiple of 0.

Put another way, why would I, the user of the program, care if the program had to divide by zero? I'm not interested in division, I'm interested in multiplication. The fact that you had to divide to determine the answer is your problem, not mine. It's an internal error that I shouldn't have to deal with.
Topic archived. No new replies allowed.