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


	}

}