aSSIGMNT OOP

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 27

NAME :ABDUL MALIK

ROLL NO: BSAI-2024-078

SUBJECT:OOP

50 JAVA PROGRAMS
50 Java Programs Code
1. Odd or Even Check

java
Copy code
import java.util.Scanner;

public class OddEven {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();

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;

public class ReverseNumber {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int reversed = 0;

while (num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}

System.out.println("Reversed Number: " + reversed);


}
}
3. Largest of Three Numbers

java
Copy code
import java.util.Scanner;

public class LargestOfThree {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = sc.nextInt();
System.out.print("Enter second number: ");
int num2 = sc.nextInt();
System.out.print("Enter third number: ");
int num3 = sc.nextInt();

int largest = (num1 > num2) ? (num1 > num3 ? num1 : num3) : (num2 > num3 ? num2 : num3);
System.out.println("Largest number: " + largest);
}
}

4. Fahrenheit to Celsius Conversion


java
Copy code
import java.util.Scanner;

public class FahrenheitToCelsius {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter temperature in Fahrenheit: ");
double fahrenheit = sc.nextDouble();
double celsius = (fahrenheit - 32) * 5 / 9;
System.out.println("Temperature in Celsius: " + celsius);
}
}
5. Armstrong Number Check

java
Copy code
import java.util.Scanner;

public class ArmstrongNumber {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int originalNum = num, remainder, result = 0;

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;

public class BubbleSort {


public static void main(String[] args) {
int[] arr = {5, 3, 8, 6, 2};
System.out.println("Original array: " + Arrays.toString(arr));

for (int i = 0; i < arr.length - 1; i++) {


for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}

System.out.println("Sorted array: " + Arrays.toString(arr));


7. Binary Search

java
Copy code
import java.util.Arrays;

public class BinarySearch {


public static void main(String[] args) {
int[] arr = {1, 3, 5, 7, 9, 11};
int key = 7;
int index = Arrays.binarySearch(arr, key);

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;

public class MultiplicationTable {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();

for (int i = 1; i <= 10; i++) {


System.out.println(num + " x " + i + " = " + (num * i));
}
}
}
9. Palindrome Check

();
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;

public class PalindromeCheck {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt
}

10. Find Factorial

java
Copy code
import java.util.Scanner;

public class Factorial {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
long factorial = 1;

for (int i = 1; i <= num; i++) {


factorial *= i;
}

System.out.println("Factorial of " + num + " is: " + factorial);


}
}
11. Find Sum of Digits
java
Copy code
import java.util.Scanner;

public class SumOfDigits {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int sum = 0;

while (num != 0) {
sum += num % 10;
num /= 10;
}

System.out.println("Sum of digits: " + sum);


}
}

12. Check Leap Year

java
Copy code
import java.util.Scanner;

public class LeapYearCheck {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a year: ");
int year = sc.nextInt();

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))


System.out.println(year + " is a leap year.");
else
System.out.println(year + " is not a leap year.");
}
}
13. Simple Interest Calculator

java
Copy code
import java.util.Scanner;

public class SimpleInterest {


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();

double simpleInterest = (principal * rate * time) / 100;


System.out.println("Simple Interest: " + simpleInterest);
}
}

14. Check Armstrong Numbers in a Range

java
Copy code
public class ArmstrongInRange {
public static void main(String[] args) {
int start = 1, end = 500;

for (int num = start; num <= end; num++) {


int sum = 0, temp = num;
while (temp != 0) {
int digit = temp % 10;
sum += Math.pow(digit, 3);
temp /= 10;
}
if (sum == num) {
System.out.println(num + " is an Armstrong number.");
}
}
}
}
15. Sum of First N Natural Numbers

java
Copy code
import java.util.Scanner;

public class SumOfNaturalNumbers {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter N: ");
int n = sc.nextInt();

int sum = (n * (n + 1)) / 2;


System.out.println("Sum of first " + n + " natural numbers: " + sum);
}
}

16. Transpose of a Matrix

java
Copy code
public class TransposeMatrix {
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

int[][] transpose = new int[3][3];


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
transpose[i][j] = matrix[j][i];
}
}

System.out.println("Transpose of the matrix:");


for (int[] row : transpose) {
for (int col : row) {
System.out.print(col + " ");
}
System.out.println();
}
}
}
17. Check Perfect Number

java
Copy code
import java.util.Scanner;

public class PerfectNumber {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int sum = 0;

for (int i = 1; i <= num / 2; i++) {


if (num % i == 0) {
sum += i;
}
}

if (sum == num)
System.out.println(num + " is a perfect number.");
else
System.out.println(num + " is not a perfect number.");
}
}

18. Calculate Compound Interest

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;

public class SwapNumbers {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = sc.nextInt();
System.out.print("Enter second number: ");
int num2 = sc.nextInt();

System.out.println("Before swap: num1 = " + num1 + ", num2 = " + num2);


int temp = num1;
num1 = num2;
num2 = temp;
System.out.println("After swap: num1 = " + num1 + ", num2 = " + num2);
}
}

20. Sum of Elements in an Array

java
Copy code
public class SumOfArray {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int sum = 0;

for (int num : arr) {


sum += num;
}

System.out.println("Sum of array elements: " + sum);


}
}
21. Linear Search in an Array
java
Copy code
import java.util.Scanner;

public class LinearSearch {


public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
Scanner sc = new Scanner(System.in);
System.out.print("Enter the element to search: ");
int key = sc.nextInt();
boolean found = false;

for (int i = 0; i < arr.length; i++) {


if (arr[i] == key) {
System.out.println("Element found at index: " + i);
found = true;
break;
}
}

if (!found) {
System.out.println("Element not found in the array.");
}
}
}

22. Factorial Using Recursion

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));
}

public static int factorial(int n) {


if (n == 0)
return 1;
return n * factorial(n - 1);
}
}
23. Check Palindrome String
java
Copy code
import java.util.Scanner;

public class PalindromeString {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine();
String reversed = new StringBuilder(str).reverse().toString();

if (str.equalsIgnoreCase(reversed))
System.out.println("The string is a palindrome.");
else
System.out.println("The string is not a palindrome.");
}
}

24. GCD of Two Numbers

java
Copy code
import java.util.Scanner;

public class GCD {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = sc.nextInt();
System.out.print("Enter second number: ");
int num2 = sc.nextInt();

while (num2 != 0) {
int temp = num2;
num2 = num1 % num2;
num1 = temp;
}

System.out.println("GCD is: " + num1);


}
}
25. LCM of Two Numbers
java
Copy code
import java.util.Scanner;

public class LCM {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = sc.nextInt();
System.out.print("Enter second number: ");
int num2 = sc.nextInt();

int gcd = num1, temp = num2;


while (temp != 0) {
int swap = temp;
temp = gcd % temp;
gcd = swap;
}
int lcm = (num1 * num2) / gcd;
System.out.println("LCM is: " + lcm);
}
}

26. Display Current Date and Time


java
Copy code
import java.util.Date;

public class CurrentDateTime {


public static void main(String[] args) {
Date date = new Date();
System.out.println("Current date and time: " + date.toString());
}
}
27. Check Prime Numbers in a Range

java
Copy code
public class PrimeNumbersInRange {
public static void main(String[] args) {
int start = 10, end = 50;

for (int num = start; num <= end; num++) {


if (isPrime(num)) {
System.out.println(num + " is a prime number.");
}
}
}

public static boolean isPrime(int num) {


if (num < 2) return false;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) return false;
}
return true;
}
}

28. Merge Two Arrays


java
Copy code
import java.util.Arrays;

public class MergeArrays {


public static void main(String[] args) {
int[] arr1 = {1, 3, 5};
int[] arr2 = {2, 4, 6};
int[] merged = new int[arr1.length + arr2.length];

System.arraycopy(arr1, 0, merged, 0, arr1.length);


System.arraycopy(arr2, 0, merged, arr1.length, arr2.length);

System.out.println("Merged array: " + Arrays.toString(merged));


}
29. Check Strong Number

java
Copy code
import java.util.Scanner;

public class StrongNumber {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int originalNum = num, sum = 0;

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);
}

30. Calculate Power of a Number


java
Copy code
import java.util.Scanner;

public class PowerOfNumber {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter base: ");
int base = sc.nextInt();
System.out.print("Enter exponent: ");
int exponent = sc.nextInt();
long result = 1;

for (int i = 0; i < exponent; i++) {


result *= base;
}
System.out.println(base + " raised to the power of " + exponent + " is: " + result);
31. Floyd’s Triangle
java
Copy code
import java.util.Scanner;

public class FloydsTriangle {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = sc.nextInt();
int num = 1;

for (int i = 1; i <= rows; i++) {


for (int j = 1; j <= i; j++) {
System.out.print(num + " ");
num++;
}
System.out.println();
}

32. Check Perfect Square


java
Copy code
import java.util.Scanner;

public class PerfectSquare {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
double sqrt = Math.sqrt(num);

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;

public class RandomNumbers {


public static void main(String[] args) {
Random random = new Random();
System.out.println("Random numbers:");

for (int i = 0; i < 5; i++) {


System.out.println(random.nextInt(100)); // Generate random numbers between 0-99
}
}
}

34. Check Neon Number

java
Copy code
import java.util.Scanner;

public class NeonNumber {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int square = num * num;
int sum = 0;

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;

for (int i = 1; i <= rows; i++) {


for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}

36. Swap Two Numbers Without Temporary Variable

java
Copy code
import java.util.Scanner;

public class SwapWithoutTemp {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = sc.nextInt();
System.out.print("Enter second number: ");
int num2 = sc.nextInt();

System.out.println("Before swap: num1 = " + num1 + ", num2 = " + num2);


num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;
System.out.println("After swap: num1 = " + num1 + ", num2 = " + num2);
}
}
37. Check Automorphic Number

java
Copy code
import java.util.Scanner;

public class AutomorphicNumber {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int square = num * num;

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;

public class HappyNumber {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();

if (isHappy(num))
System.out.println(num + " is a Happy number.");
else
System.out.println(num + " is not a Happy number.");
}

public static boolean isHappy(int num) {


int slow = num, fast = num;
do {
slow = digitSquareSum(slow);
fast = digitSquareSum(digitSquareSum(fast));
} while (slow != fast);

return slow == 1;
}

public static int digitSquareSum(int n) {


int sum = 0;
while (n > 0) {
int digit = n % 10;
sum += digit * digit;
n /= 10;
}
return sum;
}
}
39. Insertion Sort

java
Copy code
import java.util.Arrays;

public class InsertionSort {


public static void main(String[] args) {
int[] arr = {8, 4, 6, 2, 9};
System.out.println("Original Array: " + Arrays.toString(arr));

for (int i = 1; i < arr.length; i++) {


int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}

System.out.println("Sorted Array: " + Arrays.toString(arr));


}
}

40. Convert Binary to Decimal

java
Copy code
import java.util.Scanner;

public class BinaryToDecimal {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a binary number: ");
String binary = sc.nextLine();

int decimal = Integer.parseInt(binary, 2);


System.out.println("Decimal equivalent: " + decimal);
}
}
41. Decimal to Binary Conversion

java
Copy code
import java.util.Scanner;

public class DecimalToBinary {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a decimal number: ");
int decimal = sc.nextInt();

String binary = Integer.toBinaryString(decimal);


System.out.println("Binary equivalent: " + binary);
}
}

42. Check Magic Number

java
Copy code
import java.util.Scanner;

public class MagicNumber {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();

while (num >= 10) {


int sum = 0;
while (num != 0) {
sum += num % 10;
num /= 10;
}
num = sum;
}

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;

public class SumOfSquares {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int sum = 0;

while (num != 0) {
int digit = num % 10;
sum += digit * digit;
num /= 10;
}

System.out.println("Sum of squares of digits: " + sum);


}
}

44. Reverse a String

java
Copy code
import java.util.Scanner;

public class ReverseString {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine();

String reversed = new StringBuilder(str).reverse().toString();


System.out.println("Reversed string: " + reversed);
}
}
45. Count Vowels and Consonants

java
Copy code
import java.util.Scanner;

public class VowelConsonantCount {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine().toLowerCase();

int vowels = 0, consonants = 0;


for (char c : str.toCharArray()) {
if ("aeiou".indexOf(c) != -1)
vowels++;
else if (Character.isLetter(c))
consonants++;
}

System.out.println("Vowels: " + vowels);


System.out.println("Consonants: " + consonants);
}
}

46. Check Palindrome Using Recursion

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));
}

public static boolean isPalindrome(String str, int start, int end) {


if (start >= end)
return true;
if (str.charAt(start) != str.charAt(end))
return false;
return isPalindrome(str, start + 1, end - 1);
}
}
47. Count Words in a String

java
Copy code
import java.util.Scanner;

public class WordCount {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine();

String[] words = str.trim().split("\\s+");


System.out.println("Number of words: " + words.length);
}
}

48. Check Anagram

java
Copy code
import java.util.Arrays;
import java.util.Scanner;

public class AnagramCheck {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first string: ");
String str1 = sc.nextLine();
System.out.print("Enter second string: ");
String str2 = sc.nextLine();

char[] arr1 = str1.replaceAll("\\s", "").toLowerCase().toCharArray();


char[] arr2 = str2.replaceAll("\\s", "").toLowerCase().toCharArray();
Arrays.sort(arr1);
Arrays.sort(arr2);

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));
}

public static boolean isArmstrong(int num, int digits) {


return num == armstrongHelper(num, digits);
}

private static int armstrongHelper(int num, int digits) {


if (num == 0)
return 0;
int digit = num % 10;
return (int) Math.pow(digit, digits) + armstrongHelper(num / 10, digits);
}
}

50. Find Smallest and Largest Element in Array

java
Copy code
import java.util.Arrays;

public class MinMaxInArray {


public static void main(String[] args) {
int[] arr = {4, 7, 1, 9, 3, 6};
Arrays.sort(arr);
System.out.println("Smallest element: " + arr[0]);
System.out.println("Largest element: " + arr[arr.length - 1]);
}
}

You might also like