 Open in App
Not now

# Check if Y can be made multiple of X by adding any value to both

• Last Updated : 21 Nov, 2022

Given two numbers X and Y (X ≤ Y), you can select any positive integer Z and add them to both X and Y. The task is to find whether it is possible to make X a multiple of Y.

Examples:

Input: X = 7, Y = 15
Output: Yes
Explanation: We can choose Z = 1 and add them to 7 and 15. Thus, 7 + 1 = 8 is a factor of 15 + 1 = 16.

Input: X = 9, Y = 10
Output: No

Approach: The problem can be solved based on the following idea:

• First, if X = Y, then the answer is obviously “Yes”.
• Otherwise, note that no matter which Z we choose, the difference between X and Y remains constant.
Let d = Y – X. A valid Z exists if and only if X ≤ d.

Follow the steps mentioned below to implement the above idea:

• First, we find the difference between X and Y let d = Y – X.
• If d = 0 or X ≤ d, then print “Yes”
• Else print “No”

Below is the implementation of the above approach.

## C++

```#include <bits/stdc++.h>
using namespace std;

// Function to whether it is possible
// to make X a multiple of Y
void check(int X, int Y)
{
int d = Y - X;
if (d == 0 || X <= d)
cout << ("Yes");
else
cout << ("No");
}

// Driver code
int main()
{
int X = 7;
int Y = 15;

// Function Call
check(X, Y);
}

// This code is contributed by garg28harsh.```

## Java

```// Java code to implement the approach

import java.io.*;
import java.util.*;

class GFG {

// Function to whether it is possible
// to make X a multiple of Y
public static void check(int X, int Y)
{
int d = Y - X;
if (d == 0 || X <= d)
System.out.println("Yes");
else
System.out.println("No");
}

// Driver code
public static void main(String[] args)
{
int X = 7;
int Y = 15;

// Function Call
check(X, Y);
}
}```

## Python3

```# Python code to implement the approach

# Function to whether it is possible
# to make X a multiple of Y
def check(X, Y):
d = Y - X
if(d == 0 or X <= d):
print("Yes")
else:
print("No")

# Driver code
X = 7
Y = 15

# Function Call
check(X, Y)

# This code is contributed by Pushpesh Raj.```

## C#

```// C# code to implement the approach
using System;
public class GFG {

// Function to whether it is possible
// to make X a multiple of Y
public static void check(int X, int Y)
{
int d = Y - X;
if (d == 0 || X <= d)
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}

// Driver code
public static void Main(string[] args)
{
int X = 7;
int Y = 15;

// Function Call
check(X, Y);
}
}

// This code is contributed by AnkThon```

## Javascript

```// JavaScript code for the above approach

// Function to whether it is possible
// to make X a multiple of Y
function check(X, Y)
{
let d = Y - X;
if (d == 0 || X <= d)
console.log("Yes");
else
console.log("No");
}

// Driver code

let X = 7;
let Y = 15;

// Function Call
check(X, Y);

// This code is contributed by Potta Lokesh```
Output

`Yes`

Time Complexity: O(1)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Related Articles