Java

Sample Java Programs

11. Program to demonstrate Electricity bill:

import java.util.Scanner;

public class ElectricityBill {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Scanner sc = new Scanner(System.in);
        
        System.out.println("Enter the units of electricity consumed : ");
        int units = sc.nextInt(); 
        
        int bill = 0;
        
        if(units <= 100){
            bill = units * 10;
        }
        
        else if(units <= 200){
            bill = 100 * 10 + (units - 100) * 15;
        }
        
        else if(units <= 300){
            bill = 100 * 10 + 100 * 15 + (units - 200) * 20;
        }
        else{
            bill = 100 * 10 + 100 * 15 + 100 * 20 + (units - 300) * 25;
        }
        
        System.out.println("The Electricity Bill is "+bill);
        

	}

}

12. Program to find even number from 1 to 100:

import java.util.Scanner;

public class Even1_100 {

	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();
		
		for (int i = 1; i <= n ; i++) {
			if (i % 2 == 0) {
				System.out.print(" "+i+" ");
			}
		}

	}

}

13. Program to find number is even or odd:

import java.util.Scanner;

public class EvenOdd {

	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 % 2 != 0){
            System.out.println(""+n+" is an ODD Number");
        }else{
            System.out.println(""+n+" is an EVEN Number");
        }
		

	}

}

14. Program to find the Factor of given number:

import java.util.Scanner;

/*Java Program to Display Factors of a Number
 * By Rahul Kundu
 */
public class Factor {

	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();
        
        System.out.println("Factors of "+number+" are :");
        
        // Finding the Factors of the Number
        for(int i=1; i<=number; i++){
            if(number % i == 0){
                System.out.print(i+", ");
            }
        }
        System.out.println(".");

	}

}

15. Program to find GCD of two number:

/*Java Program to Find GCD of Two Numbers
 *  By Rahul Kundu
 */
import java.util.Scanner;

public class GCD {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Scanner sc = new Scanner(System.in);
	     System.out.println("Enter first number: ");
	     int first = sc.nextInt();
	     
	     System.out.println("Enter second number: ");
	     int second = sc.nextInt();
	     
	     int gcd=0;
	     
	    // Calculating GCD of two numbers
	    for (int i = 1; i <= first && i <= second; i++){
	        if (first % i == 0 && second % i == 0){
	            gcd = i;
	        }
	    }
	    
	    System.out.println("GCD of "+first+" and "+second+" is "+gcd);
	        
	    

	}

}