Skip to content
testingbuddy
  • Home
  • Manual Testing
    • Test Cases
  • Mobile Testing
  • Automation Testing
    • Selenium
    • Assignments
    • Appium
    • Frameworks
  • API Testing
    • Postman
    • Rest Assured
  • ChatGPT
  • Java
    • Sample Java Programs
  • Python
    • Sample Python Program
  • Downloads
  • About Us

Sample Java Programs

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();
        }

	}

}
Pages: 1 2 3 4 5 6 7 8

Recent Posts

  • Mobile application testing and ADB commands
  • Delete Method in Rest Assured
  • Put and Patch method in Rest Assured
  • Post Method in Rest Assured
  • Get Request in Rest Assured

Recent Comments

  • yourtestingbuddy@gmail.com on Prototype Model
  • Открыть учетную запись в binance on Clear text from text area using selenium WebDriver.
  • James_onern on Spiral Model
  • odpri racun na binance on How to obtain the tagname of the parent element in Selenium webdriver?
  • binance koda on How to scroll down a webpage in selenium using Java?

Archives

  • November 2023
  • July 2023
  • June 2023
  • May 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • August 2022
  • April 2022
  • January 2022
  • April 2021
  • March 2021
  • January 2021

Find Us

Address
Hyderabad, 500084, India

Hours
Monday–Friday: 9:00AM–5:00PM
Saturday & Sunday: 11:00AM–3:00PM

Visitors online – 270
The maximum number of visits was – 2025-03-16
Visitors Count – 0

Copyright 2020 - 2025 || www.testingbuddy.co.in || Rahul Kundu
Theme by Colorlib Powered by WordPress