P
P
PRAIT2019-06-14 13:37:19
Java
PRAIT, 2019-06-14 13:37:19

What is the correct way to calculate the remainder of a division?

Hello everyone, there is a code example in which everything is very clear.
You need to write a program in which the user enters the number 13767 and receives it in response.

import pack;

public class Test {

  public static void main(String[] args) {
    try (Scanner sc = new Scanner(System.in)) {
      int number, a, b, c, d, e;
      System.out.println("number");
      number = sc.nextInt();
      a = number / 10000;
      b = number % 10000 / 1000;
      c = number % 1000 / 100;
      d = number % 100 / 10;
      e = number % 10;
      System.out.println(+a);
      System.out.println(+b);
      System.out.println(+c);
      System.out.println(+d);
      System.out.println(+e);
    }
  }
}

A = 10000 is placed in 13767 once, etc.
There is another program number 45139
import java.util.Scanner;

public class Test {

  public static void main(String[] args) {
    try (Scanner sc = new Scanner(System.in)) {
      int number, a, b, c, d, e;
      System.out.println("number");
      number = sc.nextInt();
      a = number / 11284;
      b = number % 10000 / 1000;
      c = number % 1000 / 100;
      d = number % 100 / 10;
      e = number % 10;
      System.out.println(+a);
      System.out.println(+b);
      System.out.println(+c);
      System.out.println(+d);
      System.out.println(+e);
    }
  }
}

Here, too, it displays everything correctly, but I don’t understand why if the remainder of the division in b is written the correct 11284, the program breaks? I don't understand exactly what. Please guide me, what am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Antony, 2019-06-14
@RiseOfDeath

To begin with, decide what you mean by the word "breaks" - does it fall (Exeption), gives an incorrect result (different from the expected one), asks you to first take it to the cinema? (By the way, as far as I can see it works)
Just simply in a few columns for each line write: What (in your opinion) should your variables be equal before execution, what (in your opinion) should be the result, what your variables actually equal before executing what actually happened as a result.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question