Java Program to Increment by 1 to all the Digits of a given Integer Last Updated : 24 Nov, 2020 Comments Improve Suggest changes Like Article Like Report Given an integer, the task is to generate a Java Program to Increment by 1 All the Digits of a given Integer. Examples: Input: 12345 Output: 23456 Input: 110102 Output: 221213 Approach 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. Take the integer input.Find its length and then generate the number containing only 1 as digit of the length.Add both numbers.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); } } Output221213 Approach 2: In this approach, we will take an integer variable with value 1, we will multiply that variable with 10 and keep on adding the variable to the number till both of them have the same length. Take the integer input.Add value 1 to the input.Multiply 1 with 10 and again add them.Keep on repeating step 2 and 3 till both of them have the same length.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; // Declaring another variable with value 1 int add = 1; for (int i = 0; i < String.valueOf(number).length(); i++) { // Adding variable add and number number = number + add; // Multiplying value of the add with 10 add = add * 10; } // Printing result System.out.println(number); } } Output221213 Time Complexity: O(l) where l is the length of an integer. Space Complexity: O(1) Comment More info A aditya_taparia Follow Improve Article Tags : Java Java Programs Explore Java BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java6 min readArrays in Java9 min readJava Strings8 min readRegular Expressions in Java7 min readOOP & InterfacesClasses and Objects in Java10 min readAccess Modifiers in Java6 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking15+ min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples8 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like