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

16. Program to find largest of three numbers:

import java.util.Scanner;

public class Largest01 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
        System.out.println("Enter three numbers: ");
        int one = sc.nextInt();
        int two = sc.nextInt();
        int three = sc.nextInt();
        
        int largest = (one > two)&&(one > three) ? one : (two > three) ? two : three;
        
        System.out.println("Largest of the three is "+largest);

	}

}

17. Program for Multiplication Table:

import java.util.Scanner;

public class MultiplicationTable {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Scanner sc = new Scanner(System.in);
        System.out.println("Enter the Number : ");
        int num = sc.nextInt();
        
        System.out.println("The Multiplication Table of "+num+" is :");
        for(int i=1; i<=10; i++){
            int ans = num * i;
            System.out.println(""+num+" X "+i+" = "+ans);
        }

	}

}

18. Program to Check if number is Palindrome or not.

import java.util.Scanner;

public class Pailndrome {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number : ");
        int number = sc.nextInt();
        
        
        
        int num = number; // preserving the original number
        
       // int sum = 0;
        int rev = 0;
        
        while(num > 0){
            int rem = num % 10;
            //sum += Math.pow(digit,3);
            //sum = sum + (digit * digit * digit);
            rev = (rev * 10)+ rem;
            num = num / 10;
        }
        if(rev == number){
            System.out.println(""+number+" is an Palindrome Number !");
        }else{
            System.out.println(""+number+" is NOT an Palindrome Number !");
        }


	}

}

19. Program to check number is Positive or Negative:

import java.util.Scanner;

/*Java Program to Check if a Number is Positive or Negative
 * By Rahul Kundu
 */
public class PositiveNegative {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter the value of n: ");
		int n = sc.nextInt();
		
		
		if (n == 0) {
			System.out.println("Number is 0 which is neither Even nor Odd.");
		}else if(n < 0) {
            System.out.println("The given number "+n+" is a Negative Number");
        } else{
            System.out.println("The given number "+n+" is a Positive Number");
        }

	}

}

20. Program to Calculate Potential Energy.

/*-------Program to calculate Potential Energy-----------
 * By Rahul Kundu
 */
/*Program to find Potential Energy
 * By Rahul Kundu
 */
import java.util.Scanner;

public class PotentialEnergy {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        
        System.out.println("Enter the mass (in kg): ");
        float m = sc.nextFloat();
        
        float g = 9.8f;
        
        System.out.println("Enter the height (in meters): ");
        float h = sc.nextFloat();
        
        float PE = m * g * h;
        
        System.out.printf("The Potential Energy is %.2f",PE);


	}

}
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 – 268
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