-->

Hello,This is me!

ITEngineer8995

Professional Coder Coding is my passion

About me

Hello

I'mITEngineer8995

Spatial Data Specialist

Do you want all of your codes in one place?? Then you are landed on the right blog... Here you will find varieties of codes of various languages starting from noob to advance with their samples. Do learn, execute and share your knowledge with others. Do visit my YouTube Channel for execution of these codes .

Experience

Video Clone Detector using Hadoop

2017-2018

A detector which differentiates copied videos from the original ones using Hadoop.

Portfolio

Java Program To Reverse a String using Recursion

 

Java Program To Reverse a String using Recursion

In this Java Program, we reverse a string using recursion.

The process in which the function calls itself directly or indirectly is called recursion and the corresponding function is called as recursive function

Logic :

 String reversed = reverseString(str);

Step 1 : Create obj str of String class and store the output string in it.

Step 2 : Create another object as reversed and apply the logic of the reverse string in it.

Step 3 : Output the reversed string using the System.out.println method.

Step 4 : Create reverseString method

Step 5 : Apply if statement, if str.isEmpty then return str.

Step 6 : Call function recursively.

Program :

public class JavaExample {

    public static void main(String []args)

    {

        String str = "Welcome To my Channel";

        String reversed = reverseString(str);

        System.out.println("The reversed string is : "+reversed);

    }

    public static String reverseString(String str)

    {

        if(str.isEmpty())

            return str;

        //Calling function recursively

        return reverseString(str.substring(1)) + str.charAt(0);

  }

}

Output :

The reversed string is : lennahC ym oT emocleW

For execution of the same do visit my YouTube Channel :

https://youtu.be/0XA6NlNoIQA




Java Program To Reverse a Number using Recursion

 

Java Program To Reverse a Number using Recursion

In this Java Program, we reverse a number using Recursion.

The process in which the function calls itself directly or indirectly is called recursion and the corresponding function is called as recursive function

Step 1 : Import Scanner Class

Step 2 : Create a method for reverse

Step 3 : Apply if condition in which the number should be smaller than 10

Step 4 : Print the number

Step 5 : Apply else condition in which number should mod by 10 and print the result

Step 6 : Apply reverse method condition in which number should be divided by 10

Step 7 : Create main method

Step 8 : Initialize num to 0

Step 9 : As the user to enter the number and press enter

Step 10 : Create Scanner object

Step 11 : Store the user entered number into num

Step 12 : Print Reverse of the input number is.

Step 13 : Call the reverseMethod method

Program :

import java.util.Scanner;

public 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 sc = new Scanner(System.in);

            num = sc.nextInt();

            System.out.println("Reverse of the input number is:");

            reverseMethod(num);

            System.out.println();

        }        

}

Output :

Input your number and press enter:

5678901

Reverse of the input number is:

1098765

For execution of the same do visit my YouTube Channel : 

https://youtu.be/uu50BfxfhjA



Java Program To Reverse a Number using For Loop

 

Java Program To Reverse a Number using For Loop

In this Java Program, we reverse a number using For loop.

Logic :

   for (;num != 0;)

            {

                reversenum = reversenum * 10;

                reversenum = reversenum + num%10;

                num = num/10;

            }

Step 1 : Import the Scanner Class

Step 2 :  Create the main method

Step 3 : Initialize num and reversenum equals to zero

Step 4 : Ask the user to enter the number by using System.out.println() method

Step 5 : Create Scanner object.

Step 6 : Store the captured input in num

Step 7 : Apply for loop logic to reverse the number

Step 8 : In for loop, num should be not equal to zero

Step 9 : Multiply reversenum with 10 and store its result in reversenum.

Step 10 : Add num%10 to reversenum and store the result into reversenum

Step 11 : Divide num by 10 and store the result in num.

Step 12 : Print the reversenum

Program :

import java.util.Scanner;

public class ForLoopReverseDemo 

    {

        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 sc = new Scanner(System.in);

            //Captured input would be stored in the number num

            num = sc.nextInt();

            //For loop: No initialization part as num is already initialized.

            //No increment/decrement part as logic

            //num = num/10 already decrements the value of num

            for (;num != 0;)

            {

                reversenum = reversenum * 10;

                reversenum = reversenum + num%10;

                num = num/10;

            }

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

        }

}

Output :

Input your number and press enter:
56789111
Reverse of specified number is:11198765

For execution of the same do visit my YouTube Channel :

Java Program To Reverse a Number using While Loop

Java Program To Reverse a Number using While Loop

In this Java Program, we reverse a number using while loop.

Logic :

  while (num != 0)

             {

                reversenum = reversenum * 10;

                reversenum = reversenum + num%10;

                num = num/10;                

             }

Step 1 : Import the Scanner Class

Step 2 :  Create the main method

Step 3 : Initialize num and reversenum equals to zero

Step 4 : Ask the user to enter the number by using System.out.println() method

Step 5 : Create Scanner object.

Step 6 : Store the captured input in num

Step 7 : Apply while loop logic to reverse the number

Step 8 : In while loop, num should be not equal to zero

Step 9 : Multiply reversenum with 10 and store its result in reversenum.

Step 10 : Add num%10 to reversenum and store the result into reversenum

Step 11 : Divide num by 10 and store the result in num.

Step 12 : Print the reversenum

Program :

import java.util.Scanner;

public class ReverseNumberWhile 

    {

        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 sc = new Scanner(System.in);

            //Captured input would be stored in number num

            num = sc.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 :
145689

Reverse of input number is : 
986541

For execution of the same do visit my YouTube Channel :




Java Program to add two given Matrices

 

Java Program to add two given Matrices

In this Java Program, we add two given matrices. 

Array is a data structure consisting of collections of elements each identified by atleast one array index or key. 

E.g. If you want to store 5 numbers in an array,

        datatype arrayname [arraysize];

         int a[5];

A matrix is a multidimensional array which consists of columns and rows.

For the addition of two matrices, we need two matrices of same dimension, which means they should have same number of rows and columns.

For this Program, we declare the matrix as a 2D array.

| 1 1 1 1 |
| 2 3 5 2 |

Declaration of matrix as 2D array :

int [][] MatrixA = { {1, 1, 1, 1}, {2, 3, 5, 2}};

We use for loop for addition of the two matrices and store the addition values into the sum matrix.

For E.g.

sum[0][0] = MatrixA[0][0] + MatrixB[0][0], similarly,

sum[0][1] = MatrixA[0][1] + MatrixB[0][1] and so on.

Program :

public class AddTwoMatrix {
    
    public static void main(String [] args)
    {
        int rows = 2, columns = 4;
        
        //Declaring the two matrices as multi-dimensional arrays
        int[][] MatrixA = { {1, 1 , 1, 1}, {2, 3, 5, 2} };
        int[][] MatrixB = { {2, 3, 4, 5,}, {2, 2, 4, -4} };
        
        //Declaring a matrix sum, that will be the sum of MatrixA and MatrixB
        //The sum matrix will have the same rows and columns as the given matrices.
        
        int[][] sum = new int[rows][columns];
        for(int i = 0; i < rows; i++)
        {
            for(int j = 0; j < columns; j++)
            {
                sum[i][j] = MatrixA[i][j] + MatrixB[i][j];
            }
        }
        //Displaying the sum matrix
        System.out.println("Sum of the given matrices is:");
        for(int i =0; i < rows; i++)
        {
            for(int j =0; j < columns; j++)
            {
                System.out.print(sum[i][j] + "   ");
                
            }
            System.out.println();
        }
    }
    
}

Output :

Sum of the given matrices is:
3   4   5   6   
4   5   9   -2  

For Execution of the same, do visit my YouTube Channel :




ITEngineer8995
India

SEND ME A MESSAGE

Search This Blog

Powered by Blogger.

Pages