Java Program to Increment by 1 to all the Digits of a Given Integer

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.

  1. import java.util.Scanner;
  2. public class Increment_Digits
  3. {
  4.    public static void main(String[] args) 
  5.    {
  6.         int n, m = 0, a;
  7.         Scanner s = new Scanner(System.in);
  8.         System.out.print("Enter any number:");
  9.         n = s.nextInt();
  10.         while(n > 0)
  11.         {
  12.             a = n % 10;
  13.             a++;
  14.             m = m * 10 + a;
  15.             n = n / 10;
  16.         }
  17.         n = m;
  18.         m = 0;
  19.         while(n > 0)
  20.         {
  21.             a = n % 10;
  22.             m = m * 10 + a;
  23.             n = n / 10;
  24.         }
  25.         System.out.println("Result:"+m);
  26.     }
  27. }

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.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.