Java program to increment by 1 all the digits of a given integer

Java program to increment by 1 all the digits of a given integer. This is a Java program that increments all of a given integer’s digits by one. As input, you may use any integer. Following that, we extract each digit and increment it by one using various operations such as modulus and division. The Java Program to Increment All the Digits of a given Integer by 1 source code is available here.

import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int n, m = 0, a;
Scanner s = new Scanner(System.in);
System.out.print("Enter any number:");
n = s.nextInt();
while(n > 0)
{
a = n % 10;
a++;
m = m * 10 + a;
n = n / 10;
}
n = m;
m = 0;
while(n > 0)
{
a = n % 10;
m = m * 10 + a;
n = n / 10;
}
System.out.println("Output"+m);
}
}

 

Output:

Enter any number: 45

Output 56

What is while loop in java program

A “While” Loop is used to repeat a block of code an unknown number of times until it meets a condition. If we want a user to enter a number between 1 and 10, for example, we don’t know how many times the user might enter a larger number, so we keep asking “while the value is not between 1 and 10.”

What is pre increment and post increment in java program

The only difference is the order of operations between the variable’s increment and the value returned by the operator. I returns the value after it has been incremented, whereas i++ returns the value before it has been incremented. In the end, the value of I will be increased in both circumstances.

Both increase the value, but I does so before the current expression is evaluated, whereas i++ does so after the expression has been evaluated. Before assigning a value to a variable, the pre increment operator is used to increase the value by one. After assigning a value to a variable, the post increment operator is used to increase the value by one.