Java introduction 9/12


#1


The code itself is fine. It just doesn't make any sense to me. I was taught math differently, so that might be to blame.
In the exercise if I divide 7 % 5 my calculator doesn't tell me it's 2. It's 1,4. Now I thought ''okay, so you gotta round it up to an equal number''. But I did a quiz with a similar question, where the answer on my calculator was 3, and the actual answer was 1. Please explain the logic behind 7 % 5 being ''2''. Because I can't for the life of me see it.

https://www.codecademy.com/courses/learn-java/lessons/introduction-to-java/exercises/math-2?action=lesson_resume


public class Modulo {
	public static void main(String[] args) {

		int myRemainder = 7 % 5;
		System.out.println(myRemainder);

	}
}


#2

The moduo operator in programming returns the reminder of a division operation. i.e

7 % 5 would return 2.

but 5 % 7 returns 5.

Why?Reduced Residue System.

What that means is that because 5 cannot unerringly divide 7 it reverts back to it's initial value 5.

Modulo division is basically,

a % b = a - floor(a / b) * b

example,

5 % 7 = 5 - floor(5 / 7) * 7 = 5


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.