5 Simple Java Programs for Beginners with Output
21 days ago
5 Simple Java Programs for Beginners with Output
Introduction to Basic Java Programming
Java remains one of the most popular programming languages for beginners due to its clear syntax and object-oriented structure. In this post, we'll explore five fundamental Java programs that demonstrate basic input/output operations, arithmetic calculations, and simple problem-solving approaches. Each program includes complete code and sample output to help you understand the concepts better.
1. Displaying "Hello BCA"
Our first program is the classic "Hello World" variation, modified to greet BCA students specifically.
public class HelloBCA {
public static void main(String[] args) {
System.out.println("Hello BCA");
}
}
Output:
Hello BCA
2. Calculating the Square of a Number
This program demonstrates how to take user input and perform a basic mathematical operation.
import java.util.Scanner;
public class SquareCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
double number = scanner.nextDouble();
double square = number * number;
System.out.println("Square of " + number + " is: " + square);
scanner.close();
}
}
Sample Output:
Enter a number: 5 Square of 5.0 is: 25.0
3. Arithmetic Operations with Two Numbers
This program shows how to perform multiple operations with user-provided numbers.
import java.util.Scanner;
public class ArithmeticOperations {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first number: ");
double num1 = scanner.nextDouble();
System.out.print("Enter second number: ");
double num2 = scanner.nextDouble();
double sum = num1 + num2;
double difference = num1 - num2;
double product = num1 * num2;
System.out.println("Sum: " + sum);
System.out.println("Difference: " + difference);
System.out.println("Product: " + product);
scanner.close();
}
}
Sample Output:
Enter first number: 8 Enter second number: 3 Sum: 11.0 Difference: 5.0 Product: 24.0
4. Circle Area and Circumference Calculator
This program demonstrates mathematical calculations using the Math class constants.
import java.util.Scanner;
public class CircleCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the radius of the circle: ");
double radius = scanner.nextDouble();
double area = Math.PI * radius * radius;
double circumference = 2 * Math.PI * radius;
System.out.printf("Area: %.2f%n", area);
System.out.printf("Circumference: %.2f%n", circumference);
scanner.close();
}
}
Sample Output:
Enter the radius of the circle: 5 Area: 78.54 Circumference: 31.42
5. Area Between Two Concentric Circles
This slightly more advanced program calculates the area between two circles sharing the same center.
import java.util.Scanner;
public class ConcentricCircles {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter radius of the larger circle: ");
double r1 = scanner.nextDouble();
System.out.print("Enter radius of the smaller circle: ");
double r2 = scanner.nextDouble();
if (r1 <= r2) {
System.out.println("Error: Larger radius must be greater than smaller radius");
} else {
double area = Math.PI * (r1 * r1 - r2 * r2);
System.out.printf("Area between circles: %.2f%n", area);
}
scanner.close();
}
}
Sample Output:
Enter radius of the larger circle: 7 Enter radius of the smaller circle: 4 Area between circles: 103.67
Conclusion
These five programs demonstrate fundamental Java concepts including:
- Basic output with System.out.println()
- User input using the Scanner class
- Mathematical operations and expressions
- Conditional statements for input validation
- Using Math class constants and methods
By understanding and practicing these examples, you'll build a solid foundation for more complex Java programming. Try modifying these programs - perhaps by adding more operations or input validation - to deepen your understanding of the concepts.