Seeing ways to solve different problems on the internet, I have seen that many times the % operator is used to obtain one or more figures. I don't quite understand why they use it and how it is that the desired result is obtained, for example:
import java.util.Scanner;
public class Start {
public static void main(String[] args) {
// TODO Auto-generated method stub
int dia, mes, anno, suma, numeroSuerte = 0;
Scanner scanner = new Scanner(System.in);
System.out.print("Introduzca el día de nacimiento: ");
dia = scanner.nextInt();
System.out.print("Introduzca el mes de nacimiento: ");
mes = scanner.nextInt();
System.out.print("Introduzca el año de nacimiento: ");
anno = scanner.nextInt();
suma = dia + mes + anno;
while (suma != 0) {
numeroSuerte = numeroSuerte + (suma % 10); //Es aquí donde me fijo que se encuentra el % y no entiendo como funciona.
suma = suma / 10;
}
System.out.print("Su numero de la suerte es " + numeroSuerte);
}
}
Thanks for the explanation :)
The % in mathematical operations is normally used to find the remainder of an integer division.
% is an arithmetic operator that returns the remainder of a division, when you divide two numbers and their division is not integer this division has a remainder or remainder r eg 8%2 gives 0 as a remainder since its division is exact, it does not give a number with commas, now 8%3 gives a remainder of 2 since its division is not exact 8/3 gives a number with commas you can check it yourself with any calculator, to better understand you can search the internet for Euclid's algorithm that explains how if there is this residue, and you can even program it in the language you want, I think it would be a good exercise