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

6. Program to calculate BMI.

import java.util.Scanner;
/*Java Program to Calculate BMI (Body Mass Index)
 * By Rahul Kundu
 */
public class BMIcalculator {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
        
        System.out.println("Enter weight in kg : ");
        float weight = sc.nextFloat();
        
        System.out.println("Enter height in meters : ");
        float height = sc.nextFloat();
        
        float BMI = weight / (height * height);
        
        if(BMI < 18.5){
            System.out.println("The BMI score is "+BMI+" .The person is Under Weight.");
        }else if(BMI >= 18.5 && BMI < 25){
            System.out.println("The BMI score is "+BMI+" .The person is Normal.");
        }else if(BMI >= 25 && BMI < 30){
            System.out.println("The BMI score is "+BMI+" .The person is Over Weight.");
        }else if(BMI >= 30){
            System.out.println("The BMI score is "+BMI+" .The person is Obese.");
        }

	}

}

7. Program for Arithmetic Calculator.

import java.util.Scanner;

public class Calculator {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		 Scanner sc = new Scanner(System.in);
	        
	        System.out.println("Enter the Operator (+,-,*,/) : ");
	        char operator = sc.next().charAt(0);
	        
	        System.out.println("Enter the First Operand : ");
	        double first = sc.nextDouble();
	        
	        System.out.println("Enter the Second Operand : ");
	        double second = sc.nextDouble();
	        
	        double result = 0;
	        
	        switch(operator){
	        case '+':
	            result = first + second;
	            break;
	        case '-':
	            result = first - second;
	            break;
	        case '*':
	            result = first * second;
	            break;
	        case '/':
	            result = first / second;
	            break;
	        }
	        
	        System.out.println("The Result is : \n "+first+" "+operator+" "+second+" = "+result);
	        

	}

}

8. program to convert first letter of word to capital

import java.util.Scanner;

public class CapFirstLetter {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter a letter in small case: ");
        String word = sc.next();
        
        //getting the firstLetter
        String firstLetter = word.substring(0,1);
        
        //word after removing first letter
        String substr = word.substring(1);
        
        // Capitalizing the first letter and attaching it back to the substring 
        String capWord = firstLetter.toUpperCase() + substr;
        
        System.out.println("Result for "+word+ "is " +capWord);
    }

	}


9. Program to calculate Compound Intrest.

/*Java Program to Calculate Compound Interest
 * By Rahul Kundu
 * CI = P * ( 1 + r/100 )t - P
 */
import java.util.Scanner;

public class CompoundIntrest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		 	Scanner sc = new Scanner(System.in);
	        
	        System.out.println("Enter the Principal : ");
	        double P = sc.nextDouble();
	        
	        System.out.println("Enter the Rate : ");
	        double R = sc.nextDouble();
	        
	        System.out.println("Enter the Time : ");
	        double T = sc.nextDouble();
	        
	        //Calculating Amount
	        double A = P * Math.pow(( 1 + R/100 ),T);
	        
	        //Calculating the Compount Interest
	        double CI = A - P;
	        
	        //displaying the Result
	        System.out.printf("The Compound Interest is %.2f",CI);

	}

}

10. Program to find Cube of a number.

/*Java Program to Find Cube of a Number
 * By Rahul Kundu
 */
import java.util.Scanner;

public class Cube {

	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();
        
        //Calculating the cube of the number
        int cube = (int) Math.pow(num,3);
        
        System.out.println("Cube of the number "+num+" is "+cube+" .");
     

	}

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