Hello all,
What I am trying to do is find out what is the formula for determining whether one number is a multiple of the other. I have been at this for awhile and the closest that I came so far is if number1 % number2 == 0. Would this be the correct java statement or not?
Multiples
Page 1 of 17 Replies  592 Views  Last Post: 16 July 2010  01:41 PM
Replies To: Multiples
#3
Re: Multiples
Posted 16 July 2010  08:20 AM
Indeed.
Modulus is used to get a remainder when divided. If the remainder is 0 when one number is divided by another, the you have yourself a multiple.
Modulus is used to get a remainder when divided. If the remainder is 0 when one number is divided by another, the you have yourself a multiple.
#4
Re: Multiples
Posted 16 July 2010  11:34 AM
Hello again,
I am trying to write a program that will determine whether one number is a multiple of the other. I have the formula correct I think but I am having a problem getting it to work and calculate. I am a tyotal novice at this. Below is as far as I have gotten.
Iknow that Iam missing something but I do not know what it is. Thank You for your help.
Edited by macosxnerd101: Welcome to DIC! Please remember to post your code using code tags, like so: .
I am trying to write a program that will determine whether one number is a multiple of the other. I have the formula correct I think but I am having a problem getting it to work and calculate. I am a tyotal novice at this. Below is as far as I have gotten.
/* * To change this template, choose Tools  Templates * and open the template in the editor. */ import java.util.Scanner; /** * * @author Vincent53 */ public class PartB { public static void main ( String args []) { // variable declarations int num1; // first number to compare int num2; // second number to compare int num3 =0; // variable to hold multiple // create Scanner to capture input from console Scanner input = new Scanner(System.in); // get user input, num1 and num2 System.out.print("Enter first number: "); num1 = input.nextInt(); System.out.print("Enter second number: "); num2 = input.nextInt(); // Calculate multiple if ( num1 % num2 == num3 ); } }
Iknow that Iam missing something but I do not know what it is. Thank You for your help.
Edited by macosxnerd101: Welcome to DIC! Please remember to post your code using code tags, like so: .
#5
Re: Multiples
Posted 16 July 2010  11:40 AM
hey make sure when you post your code put it in between code tags please.
when you want to see if you have a multiple of another, just check if your first number modded by your second is 0. if it is then its a multiple.
System.out.println("like this please");
when you want to see if you have a multiple of another, just check if your first number modded by your second is 0. if it is then its a multiple.
#6
Re: Multiples
Posted 16 July 2010  11:41 AM
There is no need for num3 and I think it's always the greater number mod lesser number, just try :
int big=(num1>num2)?num1:num2; int small=(num1>num2)?num2:num1; if(big%small==0) { }
#7
Re: Multiples
Posted 16 July 2010  11:42 AM
Your code is completely correct except for right here:
You do not want the semicolon there. If the statement is true then you are probably going to want to let the user know somehow. If it's not true, you should also tell the user.
So follow this format:
Something like that should do the trick!
if ( num1 % num2 == num3 );
You do not want the semicolon there. If the statement is true then you are probably going to want to let the user know somehow. If it's not true, you should also tell the user.
So follow this format:
if(some expession) { System.out.println("yes"); } else{ System.out.println("no"); }
Something like that should do the trick!
This post has been edited by eZACKe: 16 July 2010  11:44 AM
#8
Re: Multiples
Posted 16 July 2010  01:41 PM
Duplicate topics merged. Please avoid duplicate posting.
Page 1 of 1
