Hello, I'm new to Java, can someone help with the task? You need: 1. Enter a three-digit number 2. Enter the number of numbers on the right.

In general, the question itself: how to divide this three-digit number into numbers? If for example to keep the number 657 and the number of numbers on the right 2, to get 57. What should I use?

  • The division must be used and the remainder of the division. - Vladimir Martyanov
  • @ Vladimir Martiyanov why? if there is a substring and parseInt? - Alexey Shimansky
  • @pwnsauce take the number of convertible into a string. Next, we use the substring method to take the last characters, then through Integer.parseInt(ВАША_СТРОКА_С_ЧИСЛОМ) converted into an integer - Alexey Shimansky
  • You just wrote to the answer to my question that it’s better the other way around) - Flippy

2 answers 2

We are looking for the remainder of the division by 10 ^ a.

 import java.util.*; class Program { public static void main(String[] args) { int n,a; try (Scanner in = new Scanner(System.in)) { n = in.nextInt(); a = in.nextInt(); } System.out.println(n%(int)Math.pow(10,a)); } } 
     class Program { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.println("Введите число"); int num = s.nextInt(); System.out.println("Введите количество цифр"); int numcount = s.nextInt(); if(numcount.length > count) System.out.println("Не могу не умею"); else { //способ 1 String s = Integer.toString(num); System.out.println(s.substring(s.length() - numcount)); //способ 2 int j = (int) Math.pow(10, numcount); int result = numcount % j; } } }