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 infoAdvertise with us Next Article Java Program to Increment by 1 to all the Digits of a given Integer A aditya_taparia Follow Improve Article Tags : Java Java Programs Practice Tags : Java Similar Reads Java Program to Rotate digits of a given number by K Given two integers N and K, the task is to rotate the digits of N by K. If K is a positive integer, left rotate its digits. Otherwise, right rotate its digits. Examples: Input: N = 12345, K = 2Output: 34512 Explanation: Left rotating N(= 12345) by K(= 2) modifies N to 34512. Therefore, the required 2 min read Java Program to Extract Digits from A Given Integer Problem Statement: For any random number taken into consideration the goal is to access every digit of the number. Illustration: Simply taking two numbers be it, 12345 and 110102 absolutely random pick over which computation takes place as illustrated: Input: 12345 Output: 1 // 1 digit 2 // Digit ne 3 min read Java Program to Increment All Element of an Array by One Given the array, the task is to increment each element of the array by 1. Complete traversal is required for incrementing all the elements of an array. An optimized way to complete a given task is having time complexity as O(N). Examples: Input : arr1[] = {50, 25, 32, 12, 6, 10, 100, 150} Output: ar 4 min read Java Program to Convert a Decimal Number to Binary & Count the Number of 1s As per the number system, default computations are carried over decimal numbers whose base is standardized as 10. Machine computes all the execution at the physical layer in 0s and 1s. So arises a need for a number system with base 2 known as a binary number system. A binary number can be converted 4 min read Java Program to Generate all rotations of a number Given an integer n, the task is to generate all the left shift numbers possible. A left shift number is a number that is generated when all the digits of the number are shifted one position to the left and the digit at the first position is shifted to the last.Examples: Input: n = 123 Output: 231 31 2 min read Like