Home How to divide larger number by smaller
Reply: 1

How to divide larger number by smaller

Adrian
1#
Adrian Published in 2017-12-07 21:21:55Z

I am trying to divide the larger number in this problem by the smaller number and print the answer in a quotient form. I have done the code to where it will print the correct answer, but I am wondering how to divide the larger by smaller number. Do I use an if statement?

case 1:
    int number1;
    int number2;

    System.out.println("Please enter your first number");
    number1 = s.nextInt();

    System.out.println("Please enter your second number");
    number2 = s.nextInt();

    int quotient = number1 / number2;
    int remainder = number1 % number2;  

    if(number1 > number2 || number2 > number1) {
        System.out.println("Quotient = " + quotient + "r" + remainder);
    }

    break;
Adrian
2#
Adrian Reply to 2017-12-07 21:37:33Z

maybe something like that:

int smallerNumber;
int biggerNumber;

System.out.println("Please enter your first number");
smallerNumber = s.nextInt();


int temp;
System.out.println("Please enter your second number");
temp =  s.nextInt();

smallerNumber=Math.min(temp,smallerNumber);
biggerNumber=Math.max(temp,smallerNumber);

Or using array and then sort it:

List<Integer> arr= new ArrayList<>(2);

System.out.println("Please enter your first number");
arr.add( s.nextInt());


System.out.println("Please enter your second number");
arr.add( s.nextInt());

Collections.sort(arr);

//now arr.get(0) is smaller and arr.get(1) is bigger
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.304912 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO