'Want to see if a one digit of a 2 digit number is divisible by the other digit [closed]
The problem I am having is how to say if(//code=Whole) I dont know how to tell the program if the number equals whole number because if its a number like 39 9 divided 3 equals a whole number if a number is not divisible it will go to the decimals which is how I am willing to solve the problem by saying if digit 1 or 2 divisible by the other digit is whole then cout<<Divisible
I tried searching up didnt work heres my code
#include <iostream>
using namespace std;
int main()
{
int x,z,n;
cin>>x;
x%10=z;
x/10=n;
if(z/n==int)
{
cout<<"YES";
}
else if(n/z==int)
{
cout<<"YES"
}
else
{
cout<<"NO";
}
}
I dont know how to say if it equals whole So i dont know what to put there plus there might be something wrong in the program other than that.
Solution 1:[1]
This is your code.
int main()
{
int x,z,n;
cin>>x;
x%10=z;
x/10=n;
if(z/n==int)
{
cout<<"YES";
}
else if(n/z==int)
{
cout<<"YES"
}
else
{
cout<<"NO";
}
}
A few things. First, please use whitespace to make your code readable. It's going to safe you from a wide variety of bugs over the years.
Next, this is bad code:
x%10=z;
x/10=n;
So is this:
if(z/n==int)
The first few lines of your main method should be like this:
int x;
cin >> x;
int z = x % 10;
int n = x / 10;
Note that I've done two things. First, I moved where the variables are declared to as late as possible so that they aren't hanging around, uninitialized for a while. This is just good practice. Define them and assign them at the same time.
Next, the way you wrote that code doesn't make sense. An assignment statement can't be written backwards the way you did it.
In your head, you could say, "z becomes x % 10" if you have to.
This line won't compile:
if(z/n==int)
If you want to see if z divides evenly by n:
if ( z % n == 0 )
You have the same problem in the else-if a few lines later.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Joseph Larson |
