0% found this document useful (0 votes)
4 views

arth java programs

The document contains ten Java class examples demonstrating basic programming concepts such as addition, checking even or odd numbers, simple calculations, finding maximum values, calculating the area of a circle, computing factorials, checking positive or negative numbers, reversing a number, checking for prime numbers, and swapping two numbers. Each class includes a main method that tests its functionality. These examples serve as fundamental exercises for learning Java programming.

Uploaded by

Janhavi More
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

arth java programs

The document contains ten Java class examples demonstrating basic programming concepts such as addition, checking even or odd numbers, simple calculations, finding maximum values, calculating the area of a circle, computing factorials, checking positive or negative numbers, reversing a number, checking for prime numbers, and swapping two numbers. Each class includes a main method that tests its functionality. These examples serve as fundamental exercises for learning Java programming.

Uploaded by

Janhavi More
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

🔹 1.

Addition of Two Numbers


class Addition {
int add(int a, int b) {
return a + b; // Using '+' operator
}

public static void main(String[] args) {


Addition obj = new Addition();
int result = obj.add(10, 20);
System.out.println("Sum: " + result);
}
}

🔹 2. Check Even or Odd


class EvenOddChecker {
String checkEvenOdd(int number) {
if (number % 2 == 0) // Using '%' and '==' operators
return "Even";
else
return "Odd";
}

public static void main(String[] args) {


EvenOddChecker checker = new EvenOddChecker();
System.out.println("Number is: " + checker.checkEvenOdd(7));
}
}

🔹 3. Simple Calculator (Add, Subtract, Multiply, Divide)


class Calculator {
int add(int a, int b) { return a + b; }
int subtract(int a, int b) { return a - b; }
int multiply(int a, int b) { return a * b; }
double divide(int a, int b) { return (double) a / b; }

public static void main(String[] args) {


Calculator calc = new Calculator();
System.out.println("Add: " + calc.add(10, 5));
System.out.println("Subtract: " + calc.subtract(10, 5));
System.out.println("Multiply: " + calc.multiply(10, 5));
System.out.println("Divide: " + calc.divide(10, 5));
}
}

🔹 4. Find Maximum of Two Numbers


class MaxFinder {
int findMax(int a, int b) {
return (a > b) ? a : b; // Using ternary operator
}

public static void main(String[] args) {


MaxFinder max = new MaxFinder();
System.out.println("Maximum: " + max.findMax(8, 12));
}
}

🔹 5. Area of a Circle
class CircleArea {
double calculateArea(double radius) {
return 3.1416 * radius * radius; // Using '*' operator
}

public static void main(String[] args) {


CircleArea area = new CircleArea();
System.out.println("Area: " + area.calculateArea(5));
}
}

🔹 6. Factorial of a Number
class Factorial {
int computeFactorial(int n) {
int fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i; // Using '*=' operator
}
return fact;
}

public static void main(String[] args) {


Factorial f = new Factorial();
System.out.println("Factorial of 5: " + f.computeFactorial(5));
}
}

🔹 7. Check if Number is Positive or Negative


class PositiveNegative {
String check(int num) {
if (num > 0)
return "Positive";
else if (num < 0)
return "Negative";
else
return "Zero";
}

public static void main(String[] args) {


PositiveNegative pn = new PositiveNegative();
System.out.println("Result: " + pn.check(-10));
}
}

🔹 8. Reverse a Number
class ReverseNumber {
int reverse(int num) {
int rev = 0;
while (num != 0) {
int digit = num % 10;
rev = rev * 10 + digit;
num /= 10;
}
return rev;
}

public static void main(String[] args) {


ReverseNumber rn = new ReverseNumber();
System.out.println("Reversed Number: " + rn.reverse(1234));
}
}

🔹 9. Check if a Number is Prime


class PrimeCheck {
boolean isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0)
return false;
}
return true;
}

public static void main(String[] args) {


PrimeCheck pc = new PrimeCheck();
System.out.println("Is 7 Prime? " + pc.isPrime(7));
}
}

🔹 10. Swapping Two Numbers (Using Temporary Variable)


class SwapNumbers {
void swap(int a, int b) {
System.out.println("Before Swap: a = " + a + ", b = " + b);
int temp = a;
a = b;
b = temp;
System.out.println("After Swap: a = " + a + ", b = " + b);
}

public static void main(String[] args) {


SwapNumbers s = new SwapNumbers();
s.swap(3, 7);
}
}

You might also like