Java program to reverse a number

How to reverse a number?

Take a number “num”.

  1. Initialize rev_num = 0.
  2. Loop while num > 0
    (a) Multiply rev_num by 10 and add remainder of num
    divide by 10 to rev_num
    rev_num = rev_num*10 + num%10;
    (b) Divide num by 10.
  3.  Return rev_num

Java Program to reverse a number:

import java.util.Scanner;
class ReverseNumber
{
   public static void main(String args[])
   {
      int num=0;
      int reversenum =0;
      System.out.println("Input your number and press enter: ");
      //This statement will capture the user input
      Scanner in = new Scanner(System.in);
      //Captured input would be stored in number num
      num = in.nextInt();
      //While Loop: Logic to find out the reverse number
      while( num != 0 )
      {
          reversenum = reversenum * 10;
          reversenum = reversenum + num%10;
          num = num/10;
      }

      System.out.println("Reverse of input number is: "+reversenum);
   }
}

OUTPUT:

Input your number and press enter:
4562
Reverse of input number is: 2654

Let’s dry run an example:

num = 4562
rev_num = 0

rev_num = rev_num *10 + num%10 = 2
num = num/10 = 456

rev_num = rev_num *10 + num%10 = 20 + 6 = 26
num = num/10 = 45

rev_num = rev_num *10 + num%10 = 260 + 5 = 265
num = num/10 = 4

rev_num = rev_num *10 + num%10 = 265 + 4 = 2654
num = num/10 = 0

 

There is another way of reversing a number using Recursion.

Lets see how to implement a java program to reverse a number using recursion.

PROGRAM:

import java.util.Scanner;
class RecursionReverseDemo
{
   //A method for reverse
   public static void reverseMethod(int number) {
       if (number < 10) {
       System.out.println(number);
       return;
       }
       else {
           System.out.print(number % 10);
           //Method is calling itself: recursion
           reverseMethod(number/10);
       }
   }
   public static void main(String args[])
   {
    int num=0;
    System.out.println("Input your number and press enter: ");
    Scanner in = new Scanner(System.in);
    num = in.nextInt();
    System.out.print("Reverse of the input number is:");
    reverseMethod(num);
    System.out.println();
   }
}

 

OUTPUT:

Input your number and press enter:
8901
Reverse of the input number is:1098

 

Java program to implement If-else statement

We are all aware of what is an if-else statement, still for a revision we will go through it:

  • Use if to specify a block of code to be executed, if a specified condition is true.
  • Use else to specify a block of code to be executed, if the same condition is false.
  • Use else if to specify a new condition to test, if the first condition is false.

Java program to implement If-else statement:

import java.util.Scanner;

class IfElse {
  public static void main(String[] args) {
    int marksObtained, passingMarks;

    passingMarks = 40;

    Scanner input = new Scanner(System.in);

    System.out.println("Input marks scored by you");

    marksObtained = input.nextInt();

    if (marksObtained >= passingMarks) {
      System.out.println("You passed the exam.");
    }
    else {
      System.out.println("Unfortunately you failed to pass the exam.");
    }
  }
}

 

OUTPUT:

#javac IfElse.java
#java IfElse

Input marks scored by you
58
You passed the exam.

 

Java program to implement nested If-else statement:

import java.util.Scanner;

class NestedIfElse {
  public static void main(String[] args) {
    int marksObtained, passingMarks;
    char grade;

    passingMarks = 40;

    Scanner input = new Scanner(System.in);

    System.out.println("Input marks scored by you");

    marksObtained = input.nextInt();

    if (marksObtained >= passingMarks) {

      if (marksObtained > 90)
        grade = 'A';
      else if (marksObtained > 75)
        grade = 'B';
      else if (marksObtained > 60)
        grade = 'C';
      else
        grade = 'D';

      System.out.println("You passed the exam and your grade is " + grade);
    }
    else {
      grade = 'F';
      System.out.println("You failed and your grade is " + grade);
    }
  }
}

 

OUTPUT:

javac NestedIfElse.java
java NestedIfElse

Input marks scored by you
58
You passed the exam and your grade is D

Hello World Program in Java

Hello World program in java is the basic example.

hello world program in java

Simplest java program to print “Hello World”. This is the first program for any programmer in any language.

The basic steps of java programming include the following 3 steps:

  1. Create the program by typing it into a text editor and saving it to a file named, say, Test.java
  2. Compile it by typing “javac Test.java” in the terminal window.
  3. Run (or execute) it by typing “java Test” in the terminal window.

The first step creates the program; the second translates it into a language more suitable for machine execution (and puts the result in a file named Test.class); the third actually runs the program.

So lets start with Creating the program:

class HelloWorld
{
   public static void main(String[] args)
   {
      System.out.println("Hello World");
   }
}

Now lets compile this program:
javac HelloWorld.java

Finally lets execute this program and see the Output:
java HelloWorld

Now the output is:
Hello World

 

One of the very first skills that you will learn is to identify errors, one of the next will be to be sufficiently careful when coding to avoid many of them.

That’s it. This way we can easily create, compile and execute our programs in java programming language, that’s all for Hello World Program in Java.

Comment for any queries.