Java

Sample Java Programs

21. Program to find Reverse of String.

/*Java Program to Reverse a String Using reverse() method
 *  By Rahul Kundu
 */
import java.util.Scanner;

public class ReverseString {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
        System.out.println("Enter a String: ");
        String str = sc.next();
        
        StringBuilder sb = new StringBuilder(str);
        String revString = sb.reverse().toString();
        
        System.out.println("Reversed String : "+revString);

	}

}

22. Program to convert Doller into Rupees:

/*Java Program to Convert Dollars into Rupees
 * By Rahul Kundu
 */

import java.util.Scanner;

public class Rupees {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
        
        System.out.println("Enter the Amount in USD: ");
        double USD = sc.nextDouble();
        
        double INR = 81.63 * USD;  //Dollar price as on 28/09/2022
        
        System.out.println(""+USD+" USD in INR is equal to ₹"+INR);
        

	}

}

23. Program to find square of a number.

import java.util.Scanner;

/*Java Program to Find Squre of a Number
 * By Rahul Kundu
 */
public class Square {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter value of n: ");
		int n = sc.nextInt();
		
		int square = (int) Math.pow(n, 2);
		System.out.println("Cube of the number "+n+" is "+square+" .");

	}

}

24. Program to Print Right Triangle:

/* Java Program to Print Right Triangle Star Pattern
 * By Rahul Kundu
 */

import java.util.Scanner;

public class Triangle {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter a Value of n: ");
		int n = sc.nextInt();
		
		for(int i=0; i<n; i++) {
			for(int j=0; j<=i;j++) {
				System.out.print("* ");
			}
			System.out.println();
		}

	}

}

25. Program to find addition of two matrix:

package sam2;

import java.util.Scanner;

public class AdditionMatrix {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Scanner sc = new Scanner(System.in);
        
        System.out.println("Enter the number of rows of Matrices: ");
        int row = sc.nextInt();
        
        System.out.println("Enter the number of columns of Matrices: ");
        int col = sc.nextInt();
        
        int[][] matA = new int[row][col];
        int[][] matB = new int[row][col];
        
        System.out.println("Enter the values of Matrix A:");
        for(int i=0;i<row;i++){
            for(int j=0; j<col ; j++){
                matA[i][j] = sc.nextInt();
            }
        }
        
        System.out.println("Enter the values of Matrix B:");
        for(int i=0;i<row;i++){
            for(int j=0; j<col ; j++){
                matB[i][j] = sc.nextInt();
            }
        }
        
        // Adding Matrix A and Matrix B
        
        int[][] resultantMatrix = new int[row][col];
        
        for(int i=0;i<row;i++){
            for(int j=0; j<col ; j++){
                resultantMatrix[i][j] = matA[i][j] + matB[i][j];
            }
        }
        
        //Displaying the resultant matrix
        System.out.println("Sum of Matrix A and Matrix B is ");
        for(int i=0;i<row;i++){
            for(int j=0; j<col ; j++){
                System.out.print(resultantMatrix[i][j]+" ");
            }
            System.out.println();
        }

	}

}