0% found this document useful (0 votes)
3 views10 pages

Java Basic Programs Overview

The document contains multiple Java programs that demonstrate basic programming concepts such as comparisons, arithmetic operations, and control structures. Each program includes a description of its functionality and the output it produces when compiled and executed. Examples include checking if a number is positive or negative, calculating simple interest, and determining if a year is a leap year.

Uploaded by

suneo7137
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views10 pages

Java Basic Programs Overview

The document contains multiple Java programs that demonstrate basic programming concepts such as comparisons, arithmetic operations, and control structures. Each program includes a description of its functionality and the output it produces when compiled and executed. Examples include checking if a number is positive or negative, calculating simple interest, and determining if a year is a leap year.

Uploaded by

suneo7137
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CompareTwoNumbers.

java

class CompareTwoNumbers {
public static void main(String[] args) {
int a = 10, b = 20;
if (a > b) {
[Link](a + " is greater");
} else {
[Link](b + " is greater");
}
}
}
Compiling Result:
Output:
20 is greater

[Link]

class PositiveOrNegative {
public static void main(String[] args) {
int num = -5;
if (num >= 0) {
[Link]("Positive");
} else {
[Link]("Negative");
}
}
}
Compiling Result:
Output:
Negative
[Link]

class EvenOrOdd {
public static void main(String[] args) {
int num = 6;
if (num % 2 == 0) {
[Link]("Even");
} else {
[Link]("Odd");
}
}
}
Compiling Result:
Output:
Even

[Link]

class SimpleInterest {
public static void main(String[] args) {
int p = 1000, r = 5, t = 2;
int si = (p * r * t) / 100;
[Link]("Simple Interest: " + si);
}
}
Compiling Result:
Output:
Simple Interest: 100
[Link]

class SumOfNumbers {
public static void main(String[] args) {
int a = 5, b = 10;
int sum = a + b;
[Link]("Sum: " + sum);
}
}
Compiling Result:
Output:
Sum: 15

[Link]

class CheckLeapYear {
public static void main(String[] args) {
int year = 2024;
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
[Link]("Leap Year");
} else {
[Link]("Not a Leap Year");
}
}
}
Compiling Result:
Output:
Leap Year
[Link]

class SquareOfNumber {
public static void main(String[] args) {
int num = 4;
[Link]("Square: " + (num * num));
}
}
Compiling Result:
Output:
Square: 16

[Link]

class CubeOfNumber {
public static void main(String[] args) {
int num = 3;
[Link]("Cube: " + (num * num * num));
}
}
Compiling Result:
Output:
Cube: 27
[Link]

class LargestOfThree {
public static void main(String[] args) {
int a = 10, b = 20, c = 15;
if (a > b && a > c) [Link](a + " is largest");
else if (b > c) [Link](b + " is largest");
else [Link](c + " is largest");
}
}
Compiling Result:
Output:
20 is largest

[Link]

class PrintTable {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= 10; i++) {
[Link](n + " x " + i + " = " + (n * i));
}
}
}
Compiling Result:
Output:
5 x 1 = 5
...
5 x 10 = 50
[Link]

class AreaOfCircle {
public static void main(String[] args) {
double r = 7;
double area = 3.14 * r * r;
[Link]("Area: " + area);
}
}
Compiling Result:
Output:
Area: 153.86

[Link]

class AreaOfRectangle {
public static void main(String[] args) {
int l = 5, b = 4;
[Link]("Area: " + (l * b));
}
}
Compiling Result:
Output:
Area: 20
[Link]

class PrintName {
public static void main(String[] args) {
[Link]("My name is Aman");
}
}
Compiling Result:
Output:
My name is Aman

[Link]

class SwapNumbers {
public static void main(String[] args) {
int a = 5, b = 10, temp;
temp = a;
a = b;
b = temp;
[Link]("a=" + a + ", b=" + b);
}
}
Compiling Result:
Output:
a=10, b=5
[Link]

class CheckDivisibleBy5 {
public static void main(String[] args) {
int n = 15;
if (n % 5 == 0) [Link]("Divisible by 5");
else [Link]("Not Divisible");
}
}
Compiling Result:
Output:
Divisible by 5

[Link]

class Multiplication {
public static void main(String[] args) {
int a = 3, b = 4;
[Link]("Product: " + (a * b));
}
}
Compiling Result:
Output:
Product: 12
[Link]

class DivideNumbers {
public static void main(String[] args) {
int a = 10, b = 2;
[Link]("Quotient: " + (a / b));
}
}
Compiling Result:
Output:
Quotient: 5

[Link]

class Print1To5 {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
[Link](i);
}
}
}
Compiling Result:
Output:
1
2
3
4
5
[Link]

class CheckEqual {
public static void main(String[] args) {
int a = 7, b = 7;
if (a == b) [Link]("Equal");
else [Link]("Not Equal");
}
}
Compiling Result:
Output:
Equal

[Link]

class CheckVotingAge {
public static void main(String[] args) {
int age = 18;
if (age >= 18) [Link]("Eligible to vote");
else [Link]("Not eligible");
}
}
Compiling Result:
Output:
Eligible to vote

You might also like