Программа на Java для увеличения на 1 всех цифр заданного целого числа

Опубликовано: 4 Февраля, 2022

Учитывая целое число, задача состоит в том, чтобы сгенерировать программу на Java для увеличения на 1 всех цифр заданного целого числа.

Примеры:

 Ввод: 12345
Выход: 23456

Ввод: 110102
Выход: 221213

Подход 1:

In this approach, we will create a number which will be of the same length as the input and will contain only 1 in it. Then we will add them.

  1. Take the integer input.
  2. Find its length and then generate the number containing only 1 as digit of the length.
  3. Add both numbers.
  4. Print the result.

Java

// Java Program to Increment by 1 All the Digits of a given
// Integer
  
// Importing Libraries
import java.util.*;
import java.io.*;
  
class GFG {
    // Main function
    public static void main(String[] args)
    {
        // Declaring the number
        int number = 110102;
  
        // Converting the number to String
        String string_num = Integer.toString(number);
  
        // Finding the length of the number
        int len = string_num.length();
  
        // Declaring the empty string
        String add = "";
  
        // Generating the addition string
        for (int i = 0; i < len; i++) {
            add = add.concat("1");
        }
  
        // COnverting it to Integer
        int str_num = Integer.parseInt(add);
  
        // Adding them and displaying the result
        System.out.println(number + str_num);
    }
}
Output

221213