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+" .");
     

	}

}