aSSIGMNT OOP
aSSIGMNT OOP
aSSIGMNT OOP
SUBJECT:OOP
50 JAVA PROGRAMS
50 Java Programs Code
1. Odd or Even Check
java
Copy code
import java.util.Scanner;
if (num % 2 == 0)
System.out.println(num + " is even.");
else
System.out.println(num + " is odd.");
}
}
2. Reverse a Number
java
Copy code
import java.util.Scanner;
while (num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
java
Copy code
import java.util.Scanner;
int largest = (num1 > num2) ? (num1 > num3 ? num1 : num3) : (num2 > num3 ? num2 : num3);
System.out.println("Largest number: " + largest);
}
}
java
Copy code
import java.util.Scanner;
while (originalNum != 0) {
remainder = originalNum % 10;
result += Math.pow(remainder, 3);
originalNum /= 10;
}
if (result == num)
System.out.println(num + " is an Armstrong number.");
else
System.out.println(num + " is not an Armstrong number.");
}
}
6. Bubble Sort
java
Copy code
import java.util.Arrays;
java
Copy code
import java.util.Arrays;
if (index >= 0)
System.out.println("Element found at index: " + index);
else
System.out.println("Element not found");
}
}
8. Multiplication Table
java
Copy code
import java.util.Scanner;
();
int originalNum = num, reversed = 0;
while (num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
if (originalNum == reversed)
System.out.println(originalNum + " is a palindrome.");
else
System.out.println(originalNum + " is not a palindrome.");
}java
Copy code
import java.util.Scanner;
java
Copy code
import java.util.Scanner;
while (num != 0) {
sum += num % 10;
num /= 10;
}
java
Copy code
import java.util.Scanner;
java
Copy code
import java.util.Scanner;
java
Copy code
public class ArmstrongInRange {
public static void main(String[] args) {
int start = 1, end = 500;
java
Copy code
import java.util.Scanner;
java
Copy code
public class TransposeMatrix {
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
java
Copy code
import java.util.Scanner;
if (sum == num)
System.out.println(num + " is a perfect number.");
else
System.out.println(num + " is not a perfect number.");
}
}
java
Copy code
import java.util.Scanner;
public class CompoundInterest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter principal amount: ");
double principal = sc.nextDouble();
System.out.print("Enter rate of interest: ");
double rate = sc.nextDouble();
System.out.print("Enter time in years: ");
double time = sc.nextDouble();
System.out.print("Enter number of times interest is compounded per year: ");
int n = sc.nextInt();
double compoundInterest = principal * Math.pow((1 + rate / 100 / n), n * time);
System.out.println("Compound Interest: " + compoundInterest);
}
}
19. Swap Two Numbers
java
Copy code
import java.util.Scanner;
java
Copy code
public class SumOfArray {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int sum = 0;
if (!found) {
System.out.println("Element not found in the array.");
}
}
}
java
Copy code
public class FactorialRecursion {
public static void main(String[] args) {
int num = 5; // Example number
System.out.println("Factorial of " + num + " is " + factorial(num));
}
if (str.equalsIgnoreCase(reversed))
System.out.println("The string is a palindrome.");
else
System.out.println("The string is not a palindrome.");
}
}
java
Copy code
import java.util.Scanner;
while (num2 != 0) {
int temp = num2;
num2 = num1 % num2;
num1 = temp;
}
java
Copy code
public class PrimeNumbersInRange {
public static void main(String[] args) {
int start = 10, end = 50;
java
Copy code
import java.util.Scanner;
while (num != 0) {
int digit = num % 10;
sum += factorial(digit);
num /= 10;
}
if (sum == originalNum)
System.out.println(originalNum + " is a strong number.");
else
System.out.println(originalNum + " is not a strong number.");
}
public static int factorial(int n) {
if (n == 0)
return 1;
return n * factorial(n - 1);
}
if (sqrt == Math.floor(sqrt))
System.out.println(num + " is a perfect square.");
else
System.out.println(num + " is not a perfect square.");
}
}
33. Generate Random Numbers
java
Copy code
import java.util.Random;
java
Copy code
import java.util.Scanner;
while (square != 0) {
sum += square % 10;
square /= 10;
}
if (sum == num)
System.out.println(num + " is a Neon number.");
else
System.out.println(num + " is not a Neon number.");
}
}
35. Display Star Pattern
java
Copy code
public class StarPattern {
public static void main(String[] args) {
int rows = 5;
java
Copy code
import java.util.Scanner;
java
Copy code
import java.util.Scanner;
if (String.valueOf(square).endsWith(String.valueOf(num)))
System.out.println(num + " is an Automorphic number.");
else
System.out.println(num + " is not an Automorphic number.");
}
}
38. Check Happy Number
java
Copy code
import java.util.Scanner;
if (isHappy(num))
System.out.println(num + " is a Happy number.");
else
System.out.println(num + " is not a Happy number.");
}
return slow == 1;
}
java
Copy code
import java.util.Arrays;
java
Copy code
import java.util.Scanner;
java
Copy code
import java.util.Scanner;
java
Copy code
import java.util.Scanner;
if (num == 1)
System.out.println("The number is a magic number.");
else
System.out.println("The number is not a magic number.");
}
}
43. Calculate Sum of Squares of Digits
java
Copy code
import java.util.Scanner;
while (num != 0) {
int digit = num % 10;
sum += digit * digit;
num /= 10;
}
java
Copy code
import java.util.Scanner;
java
Copy code
import java.util.Scanner;
java
Copy code
public class PalindromeRecursion {
public static void main(String[] args) {
String str = "radar";
System.out.println("Is the string a palindrome? " + isPalindrome(str, 0, str.length() - 1));
}
java
Copy code
import java.util.Scanner;
java
Copy code
import java.util.Arrays;
import java.util.Scanner;
if (Arrays.equals(arr1, arr2))
System.out.println("The strings are anagrams.");
else
System.out.println("The strings are not anagrams.");
}
}
49. Check Armstrong Number Using Recursion
java
Copy code
public class ArmstrongRecursion {
public static void main(String[] args) {
int num = 153; // Example number
int digits = String.valueOf(num).length();
System.out.println("Is " + num + " an Armstrong number? " + isArmstrong(num, digits));
}
java
Copy code
import java.util.Arrays;