Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Write a Java program to calculate the modules of two numbers without using any inbuilt modulus operator.

  1. #1
    Junior Member
    Join Date
    Apr 2018
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Write a Java program to calculate the modules of two numbers without using any inbuilt modulus operator.

    I have two codes for this one, the first one using the modulus operator and the second one not using the modulus operator
    //Using the modulus operator.
    import java.util.Scanner; 
     
     
     
    class RicMain  
     
    {  
     
        public static void main(String args[])  
     
        { 
     
            Scanner a = new Scanner(System.in); 
     
            int x, y, z; 
     
     
     
            System.out.print("Enter first integer: "); 
     
            x = a.nextInt(); 
     
     
     
            System.out.print("Enter second integer: "); 
     
            y = a.nextInt(); 
     
     
     
            z = x % y; 
     
            System.out.println("The modulus of x and y is: " + z); 
     
        }  
     
    }

    //not using the modulus operator
    import java.util.Scanner; 
     
     
     
    class RicMain  
     
    {  
     
        public static void main(String args[])  
     
        { 
     
            Scanner a = new Scanner(System.in); 
     
            int x, y, divided, result; 
     
     
     
            System.out.print("Enter first integer: "); 
     
            x = a.nextInt(); 
     
     
     
            System.out.print("Enter second integer: "); 
     
            y = a.nextInt(); 
     
     
     
            divided = (x / y); 
     
            result = (x - (divided * y)); 
     
            System.out.println("The modulus of x and y is: " + result); 
     
        }  
     
    }
    Now, for the second code, I do not understand
    divided = (x / y);

    result = (x - (divided * y));
    When I start with the integers 19 and 7, for the first one is 5, but for the second one, when I try to do it on a calculator, it prints out something totally different, but on Java, it prints out 5? Why is that?

  2. #2
    Junior Member
    Join Date
    Mar 2018
    Posts
    10
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Write a Java program to calculate the modules of two numbers without using any inbuilt modulus operator.

    I'm assuming the "totally different" answer you're getting on a calculator is zero.
    If that is the case then the reason is in the type of the variables... particularly the variable "divided".

    The int java type will store whole numbers without the fractional/decimal part.
    So: 19 / 7 = 2.7142...
    but when the result is store in an int variable it is reduced to just "2".

    Try printing out the value of divided to see what is going on.
    Also what happens if the type of the variable "divided" is changed to float or double?

  3. #3
    Junior Member
    Join Date
    Apr 2018
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Write a Java program to calculate the modules of two numbers without using any inbuilt modulus operator.

    Quote Originally Posted by User2009 View Post
    I'm assuming the "totally different" answer you're getting on a calculator is zero.
    If that is the case then the reason is in the type of the variables... particularly the variable "divided".

    The int java type will store whole numbers without the fractional/decimal part.
    So: 19 / 7 = 2.7142...
    but when the result is store in an int variable it is reduced to just "2".

    Try printing out the value of divided to see what is going on.
    Also what happens if the type of the variable "divided" is changed to float or double?
    For my first code, I just did 19%7 and it gave me 5. However, I'm not really understanding what's going on in my second code, where I have to find the modulus of two numbers without using the modulus operator . I tried it with the same numbers on my calculator: (19/7), then
    (19-((19/7)*7)) and got 0, when the remainder is supposed to be 5.

  4. #4
    Junior Member
    Join Date
    Apr 2018
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Write a Java program to calculate the modules of two numbers without using any inbuilt modulus operator.

    Quote Originally Posted by Ricky Buen View Post
    For my first code, I just did 19%7 and it gave me 5. However, I'm not really understanding what's going on in my second code, where I have to find the modulus of two numbers without using the modulus operator . I tried it with the same numbers on my calculator: (19/7), then
    (19-((19/7)*7)) and got 0, when the remainder is supposed to be 5.
    IDK where that ying yang symbol came from. I mean't the percent symbol, not this random yin yang symbol

  5. #5
    Junior Member
    Join Date
    Mar 2018
    Posts
    10
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Write a Java program to calculate the modules of two numbers without using any inbuilt modulus operator.

    The key is in the data types of the variables.

    Now, for the second code, I do not understand
    divided = (x / y);
    The value in the variable "divided" will be 2 because of its data type which is int.

    You can confirm this by adding this line:
    System.out.println("divided = " + divided);

    If the data type for "divided" and "result" were changed to float as in:
    float divided, result;

    The output would more closely match what one would get on a calculator.

Similar Threads

  1. Replies: 1
    Last Post: April 11th, 2018, 01:50 PM
  2. Replies: 1
    Last Post: May 13th, 2013, 05:09 PM
  3. [SOLVED] Write a java program to display even numbers between 2 and 200 using netbeans java
    By Shalom in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 11th, 2012, 05:16 AM
  4. I need code to write the following Repetition Numbers Using Java
    By Esmael in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 23rd, 2012, 08:07 AM
  5. Creating Block cipher without the use of inbuilt java funcs
    By fortune2k in forum Algorithms & Recursion
    Replies: 0
    Last Post: November 15th, 2010, 09:43 AM