This is a Java Program to Increment by 1 All the Digits of a given Integer.
Enter any integer as input. After this we perform various operations like modulus and division to extract each digit and increment it by one.
Here is the source code of the Java Program to Increment by 1 All the Digits of a given Integer. The Java program is successfully compiled and run on a Windows system.The program output is also shown below.
import java.util.Scanner;
public class Increment_Digits
{
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("Result:"+m);
}
}
Output:
$ javac Increment_Digits.java $ java Increment_Digits Enter any number:4567 Result:5678
Sanfoundry Global Education & Learning Series – 1000 Java Programs.
advertisement
advertisement
Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.
Related Posts:
- Check Java Books
- Apply for Java Internship
- Apply for Computer Science Internship
- Practice BCA MCQs
- Practice Information Technology MCQs