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

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

	}

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