7 Replies - 481 Views - Last Post: 16 July 2010 - 01:41 PM Rate Topic: -----

#1 Vincent53  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 07-May 10

Multiples

Posted 16 July 2010 - 08:17 AM

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?
Is This A Good Question/Topic? 0
  • +

Replies To: Multiples

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10646
  • View blog
  • Posts: 39,536
  • Joined: 27-December 08

Re: Multiples

Posted 16 July 2010 - 08:18 AM

Yes- this is the correct formula. :)
Was This Post Helpful? 1
  • +
  • -

#3 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

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.
Was This Post Helpful? 1
  • +
  • -

#4 Vincent53  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 07-May 10

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.

  /*
 * 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: :code:.
Was This Post Helpful? 0
  • +
  • -

#5 irmoathlete  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 98
  • Joined: 09-July 10

Re: Multiples

Posted 16 July 2010 - 11:40 AM

hey make sure when you post your code put it in between code tags please.
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.
Was This Post Helpful? 0
  • +
  • -

#6 sh1n3  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 164
  • Joined: 22-April 10

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) {
}

Was This Post Helpful? 1
  • +
  • -

#7 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: Multiples

Posted 16 July 2010 - 11:42 AM

Your code is completely correct except for right here:
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

Was This Post Helpful? 2
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10646
  • View blog
  • Posts: 39,536
  • Joined: 27-December 08

Re: Multiples

Posted 16 July 2010 - 01:41 PM

Duplicate topics merged. Please avoid duplicate posting. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1