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

1. Create a program to add two numbers.

public class Addition {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int a = 10 , b = 20;
		
		int c = a + b;
		
		System.out.println("Addition = "+c);

	}

}

2. Program to calculate Area of circle.

/*Java Program to Calculate the Area of a Circle
 * By Rahul Kundu
 * Area of a Circle, A = π r², where, π (pie) is equal to the fraction 22/7 and r stands for radius.
 * 
 */
import java.util.Scanner;

public class AreaCircle {

	public static void main(String[] args){
        
        Scanner sc = new Scanner(System.in);
        
        System.out.println("Enter the radius of the circle : ");
        float radius = sc.nextFloat();
        
        float area = (22.0f/7.0f) * radius * radius;
        
        System.out.println("Area of the Circle is "+ area +" sq. units");
        
    }

}

3. Program to check Armstrong Number.

/*Java Program to Check Armstrong Number
 * By Rahul Kundu
 * For example, 0, 1, 153, 370, 371, and 407 are some Armstrong numbers.
 */
import java.util.Scanner;

public class Armstrong {

	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();
        
        if(number == 0 || number == 1){
            System.out.println(""+number+" is an Armstrong Number !");
            return;
        }
        
        int num = number; // preserving the original number
        
        int sum = 0;
        
        while(num > 0){
            int digit = num % 10;
            //sum += Math.pow(digit,3);
            sum = sum + (digit * digit * digit);
            num = num / 10;
        }
        if(sum == number){
            System.out.println(""+number+" is an Armstrong Number !");
        }else{
            System.out.println(""+number+" is NOT an Armstrong Number !");
        }

	}

}

4. Program to find Average of total marks.


public class Average {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int p = 75 , c = 80 , m = 78;
		double avg;
		
		double total = p+c+m;
		System.out.println("Total= "+total);
		avg = total / 300;
		
		System.out.println("Average = "+avg);
		
		double percentage = avg * 100;
		System.out.println("Percentage = "+percentage);
		

	}

}

5. Program to calculate Average of N number:

import java.util.Scanner;
/*Java Program to Calculate Average of N Numbers
 * Average = (5 + 6 + 9 + 10) / 4 = 30/4 = 7.5
 */
public class AverageOfN {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
        System.out.println("Total count of number to find the average of: ");
        int n = sc.nextInt();
        
        int sum = 0;
        
        System.out.println("Enter the numbers: ");
        
        for(int i=0;i<n;i++){
            sum += sc.nextInt();
        }
        
        double average = sum/(double)n; // typecasting n from int to double for decimal division
        
        System.out.println("Average of n given numbers is "+average);
        

	}

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