Computer Science Project || ISC || Sunrise Days

Problem 1: Write a program to check a number is Smith or not. [Digits of Smith number when added is equal to sum of digits of prime factors of the number. 27 is Smith number because 2+7=9 and 27=333]

Solution

import java.util.Scanner;

class Smith {
    public static boolean isPrime(int n) {
        if (n <= 1)
            return false;
        for (int i = 2; i <= Math.sqrt(n); i++) {
            if (n % i == 0)
                return false;
        }
        return true;
    }

    public static int sumOfDigit(int n) {
        int sum = 0;
        while (n > 0) {
            sum = sum + n % 10;
            n = n / 10;
        }
        return sum;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a number: ");
        int n = sc.nextInt();
        int k=n, s=0;
        for (int i = 2; i <= n; i++) {
            if (isPrime(i)) {
                while (k % i == 0) {
                    k = k / i;
                    s = s + sumOfDigit(i);
                }
            }
        }
        if (sumOfDigit(n) == s)
            System.out.println(n + " is Smith number");
        else
            System.out.println(n + " is not Smith number");
        sc.close();
    }
}

Output

Enter a number:
27
27 is Smith number

Enter a number:
12
12 is not Smith number

Problem 2: Write a program to check whether a number is Kaprekar. [A Kaprekar number's square can be split into two parts that add up to the original number. 45 is Kaprekar number because 45^2 = 2025 and 20+25=45].

Solution

import java.util.Scanner;

class Kaprekar {
    public static int noOfDigits(int n) {
        int count = 0;
        while (n > 0) {
            count++;
            n = n / 10;
        }
        return count;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a number: ");
        int n = sc.nextInt();
        int c = noOfDigits(n);
        int sq = n * n;
        int l = sq / (int) Math.pow(10, c);
        int r = sq % (int) Math.pow(10, c);
        if (l + r == n)
            System.out.println(n + " is Kaprekar number");
        else
            System.out.println(n + " is not Kaprekar number");
        sc.close();
    }
}

Output

Enter a number:
45
45 is Kaprekar number

Enter a number:
10
10 is not Kaprekar number

Problem 3: Write a program to find out the given number is a ISBN number or not. [ISBN is a 10 digit number where the last digit is a check digit. The check digit is calculated as (10a1+9a2+8a3+7a4+6a5+5a6+4a7+3a8+2*a9)%11 and if the check digit is 10 then it is represented as X]

Solution

import java.util.Scanner;

class ISBN {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a number: ");
        int n = sc.nextInt();
        int sum = 0;
        int a = n;
        int i = 10;
        while (a > 0) {
            int r = a % 10;
            sum = sum + r * i;
            a = a / 10;
            i--;
        }
        int checkDigit = sum % 11;
        if (checkDigit == 10)
            System.out.println(n + " is ISBN number");
        else
            System.out.println(n + " is not ISBN number");
        sc.close();
    }
}

Output

Enter a number:
950126105537
950126105537 is ISBN number

Enter a number:
950126105538
950126105538 is not ISBN number

Problem 4: Write a program to check whether a number is Disarium. [A Disarium number is a number where the sum of its digits powered with their respective positions is equal to the number itself. 175 is Disarium number because 1^1+7^2+5^3=175]

Solution

import java.util.Scanner;

class Disarium {
    public static int noOfDigits(int n) {
        int count = 0;
        while (n > 0) {
            count++;
            n = n / 10;
        }
        return count;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a number: ");
        int n = sc.nextInt();
        int c = noOfDigits(n);
        int sum = 0;
        int a = n;
        int i = 1;
        while (a > 0) {
            int r = a % 10;
            sum = sum + (int) Math.pow(r, i);
            a = a / 10;
            i++;
        }
        if (sum == n)
            System.out.println(n + " is Disarium number");
        else
            System.out.println(n + " is not Disarium number");
        sc.close();
    }
}

Output

Enter a number:
175
175 is Disarium number

Enter a number:
176
176 is not Disarium number

Problem 5: Write a program to check whether a number is Keith. [A Keith number isis a number that appears in a sequence generated from its own digits, where the sequence starts with the digits of the number, and each new term is the sum of the previous k terms (k being the number of digits). For example, 197 (3 digits) starts a sequence: 1, 9, 7, (1+9+7)=17, (9+7+17)=33, (7+17+33)=57, (17+33+57)=107, (33+57+107)=197; since 197 appears, it's a Keith number.]

Solution

import java.util.Scanner;

class Keith {
    public static int noOfDigits(int n) {
        int count = 0;
        while (n > 0) {
            count++;
            n = n / 10;
        }
        return count;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a number: ");
        int n = sc.nextInt();
        int c = noOfDigits(n);
        int[] arr = new int[c];
        int a = n;
        int i = 0;
        while (a > 0) {
            arr[i] = a % 10;
            a = a / 10;
            i++;
        }
        int sum = 0;
        for (int j = 0; j < c; j++) {
            sum = sum + arr[j];
        }
        while (sum != n) {
            for (int j = 0; j < c - 1; j++) {
                arr[j] = arr[j + 1];
            }
            arr[c - 1] = sum;
            sum = 0;
            for (int j = 0; j < c; j++) {
                sum = sum + arr[j];
            }
        }
        if (sum == n)
            System.out.println(n + " is Keith number");
        else
            System.out.println(n + " is not Keith number");
        sc.close();
    }
}

Output

Enter a number:
197
197 is Keith number

Enter a number:
198
198 is not Keith number

Problem 6: Write a program to generate a Pascal Triangle for n rows. [Pascal's Triangle is a triangular array of the binomial coefficients. The rows of Pascal's Triangle are conventionally enumerated starting with row 0 at the top.]

Solution

import java.util.Scanner;

class PascalTriangle {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of rows: ");
        int n = sc.nextInt();
        int[][] arr = new int[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j <= i; j++) {
                if (j == 0 || j == i)
                    arr[i][j] = 1;
                else
                    arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j];
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                System.out.print(" ");
            }
            for (int j = 0; j <= i; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
        sc.close();
    }
}

Output

Enter the number of rows:
5
    1
   1 1
  1 2 1
 1 3 3 1
1 4 6 4 1

Problem 7: Write a program to find GCD using recursive function. [GCD is the greatest common divisor of two numbers. It is the largest number that divides both numbers without leaving a remainder.]

Solution

import java.util.Scanner;

class GCD {
    public static int gcd(int a, int b) {
        if (b == 0)
            return a;
        else
            return gcd(b, a % b);
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter two numbers: ");
        int a = sc.nextInt();
        int b = sc.nextInt();
        System.out.println("GCD of " + a + " and " + b + " is " + gcd(a, b));
        sc.close();
    }
}

Output

Enter two numbers:
48 18
GCD of 48 and 18 is 6

Problem 8: Write a program to perform Permutation for a given limit and a common factor. [Permutation is an arrangement of all the members of a set into some sequence or order.]

Solution

import java.util.Scanner;

class Permutation {
    public static long fact(int n) {
        if (n == 0)
            return 1;
        else
            return n * fact(n - 1);
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the limit: ");
        int n = sc.nextInt();
        System.out.println("Enter the common factor: ");
        int r = sc.nextInt();
        long count = fact(n) / fact(n - r); // n! / (n-k)!)
        System.out.println("Number of permutations where product is divisible by " + r + " is " + count);
        sc.close();
    }
}

Output

Enter the limit:
12
Enter the common factor:
4
Number of permutations where product is divisible by 4 is 11880

Problem 9: Write a program to perform combination for a given limit and a common factor. [Combination is a selection of items from a larger set, where the order of selection does not matter.]

Solution

import java.util.Scanner;

class Combination {
    public static long fact(int n) {
        if (n == 0)
            return 1;
        else
            return n * fact(n - 1);
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the limit: ");
        int n = sc.nextInt();
        System.out.println("Enter the common factor: ");
        int r = sc.nextInt();
        long count = fact(n) / (fact(r) * fact(n - r)); // n! / (r! * (n-r)!)
        System.out.println("Number of combinations where product is divisible by " + r + " is " + count);
        sc.close();
    }
}

Output

Enter the limit:
12
Enter the common factor:
4
Number of combinations where product is divisible by 4 is 495

Problem 10: Write a program to generate AP series and print its sum by getting first term, common ratio and no of terms as input.

Solution

import java.util.Scanner;

class APSeries {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the first term: ");
        int a = sc.nextInt();
        System.out.println("Enter the common difference: ");
        int d = sc.nextInt();
        System.out.println("Enter the number of terms: ");
        int n = sc.nextInt();
        int sum = 0;

        System.out.println("AP series is: ");
        for (int i = 0; i < n; i++) {
            int term = a + i * d;
            System.out.print(term + " ");
            sum += term;
        }
        System.out.println("\nSum of AP series is: " + sum);
        sc.close();
    }
}

Output

Enter the first term:
2
Enter the common difference:
3
Enter the number of terms:
5
AP series is: 2 5 8 11 14 
Sum of AP series is: 40

Problem 11: Write a program to generate GP series and print its sum by getting first term, common ratio and no of terms as input.

Solution

import java.util.Scanner;

class GPSeries {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the first term: ");
        int a = sc.nextInt();
        System.out.println("Enter the common ratio: ");
        int r = sc.nextInt();
        System.out.println("Enter the number of terms: ");
        int n = sc.nextInt();
        int sum = 0;
        double product = 1;
        System.out.println("GP series is: ");
        for (int i = 0; i < n; i++) {
            int term = (int) (a * Math.pow(r, i));
            System.out.print(term + " ");
            sum += term;
            product *= term;
        }
        System.out.println("\nSum of GP series is: " + sum);
        System.out.println("Product of GP series is: " + product);
        sc.close();
    }
}

Output

Enter the first term:
2
Enter the common ratio:
3
Enter the number of terms:
5
GP series is: 2 6 18 54 162 
Sum of GP series is: 242
Product of GP series is: 1458

Problem 12: Write a program to change a number into a Roman number. [Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.]

Solution

import java.util.Scanner;

class RomanNumber {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a number: ");
        int n = sc.nextInt();
        String[] roman = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
        int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
        String result = "";
        for (int i = 0; i < values.length; i++) {
            while (n >= values[i]) {
                result += roman[i];
                n -= values[i];
            }
        }
        System.out.println("Roman number is: " + result);
        sc.close();
    }
}

Output

Enter a number:
1987
Roman number is: MCMLXXXVII

Problem 13: Write a program to find denomination of a given amount.

Solution

import java.util.Scanner;

class Denomination {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the amount: ");
        int amount = sc.nextInt();
        int[] denominations = {1000, 500, 100, 50, 20, 10, 5, 2, 1};
        int[] count = new int[denominations.length];
        for (int i = 0; i < denominations.length; i++) {
            count[i] = amount / denominations[i];
            amount = amount % denominations[i];
        }
        System.out.println("Denomination of " + amount + " is: ");
        for (int i = 0; i < denominations.length; i++) {
            if (count[i] > 0)
                System.out.println(denominations[i] + " x " + count[i]);
        }
        sc.close();
    }
}

Output

Enter the amount:
1234
Denomination of 1234 is: 
1000 x 1
200 x 1
100 x 2
20 x 1
10 x 3
4 x 2

Problem 14: Write a program to swap two strings without using third variable.

Solution

import java.util.Scanner;

class SwapStrings {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the first string: ");
        String str = sc.nextLine();
        System.out.println("Enter the second string: ");
        String str1 = sc.nextLine();
        str = str + str1;
        str1 = str.substring(0, str.length() - str1.length());
        str = str.substring(str1.length());
        System.out.println("After swapping: ");
        System.out.println("First string: " + str);
        System.out.println("Second string: " + str1);
        sc.close();
    }
}

Output

Enter the first string:
Hello
Enter the second string:
World
After swapping: 
First string: World
Second string: Hello

Problem 15: Write a program to display the below pattern

1 A A A A A
2 2 B B B B
3 3 3 C C C
4 4 4 4 D D
5 5 5 5 5 E

Solution

class Pattern {
    public static void main(String[] args) {
        char ch = 'A';
        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(i + " ");
            }
            for (int k = 1; k <= 5 - i; k++) {
                System.out.print(ch + " ");
            }
            System.out.println();
            ch++;
        }
    }
}

Problem 16: Write a program to enter a natural number and display all possible combinations of consecutive natural numbers that sum up to that number.

Solution

import java.util.Scanner;

class ConsecutiveSum {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a natural number: ");
        int n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            int sum = 0;
            for (int j = i; j <= n; j++) {
                sum += j;
                if (sum == n) {
                    for (int k = i; k <= j; k++) {
                        System.out.print(k + " ");
                    }
                    System.out.println();
                    break;
                } else if (sum > n) {
                    break;
                }
            }
        }
        sc.close();
    }
}

Output

Enter a natural number: 15
1 2 3 4 5
4 5 6 
7 8 
15

Problem 17: Write a program to display the below pattern

11
12 22
13 23 33
14 24 34 44
15 25 35 45 55

Solution

import java.util.Scanner;

class Pattern {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number of rows: ");
        int n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j + "" + i + " ");
            }
            System.out.println();
        }
        sc.close();
    }
}

Problem 18: Write a program to display the below pattern

*************
*           *
*           *
*           *
*           *
*           *
*           *
*           *
*           *
*************

Solution

class Pattern {
    public static void main(String[] args) {
        int n = 10;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (i == 1 || i == n || j == 1 || j == n)
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
    }
}

Problem 19: Write a program to check whether given pair of numbers are Twin Prime. [Twin primes are pairs of prime numbers that have a difference of 2.]

Solution

import java.util.Scanner;

class TwinPrime {
    public static boolean isPrime(int n) {
        if (n <= 1)
            return false;
        for (int i = 2; i <= Math.sqrt(n); i++) {
            if (n % i == 0)
                return false;
        }
        return true;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the first number: ");
        int n1 = sc.nextInt();
        System.out.print("Enter the second number: ");
        int n2 = sc.nextInt();
        if (isPrime(n1) && isPrime(n2) && Math.abs(n1 - n2) == 2)
            System.out.println(n1 + " and " + n2 + " are Twin Primes.");
        else
            System.out.println(n1 + " and " + n2 + " are not Twin Primes.");
        sc.close();
    }
}

Output

Enter the first number:
3
Enter the second number:
5
3 and 5 are Twin Primes.

Problem 20: Write a program to merge two sorted arrays.

Solution

import java.util.Arrays;
import java.util.Scanner;

class MergeSortedArrays {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the size of the first array: ");
        int n = sc.nextInt();
        int[] arr1 = new int[n];
        System.out.print("Enter the elements of the first array: ");
        for (int i = 0; i < n; i++)
            arr1[i] = sc.nextInt();
        System.out.print("Enter the size of the second array: ");
        int m = sc.nextInt();
        int[] arr2 = new int[m];
        System.out.print("Enter the elements of the second array: ");
        for (int i = 0; i < m; i++)
            arr2[i] = sc.nextInt();
        int[] mergedArray = new int[n + m];
        int i = 0, j = 0, k = 0;
        while (i < n && j < m) {
            if (arr1[i] < arr2[j])
                mergedArray[k++] = arr1[i++];
            else
                mergedArray[k++] = arr2[j++];
        }
        while (i < n)
            mergedArray[k++] = arr1[i++];
        while (j < m)
            mergedArray[k++] = arr2[j++];
        System.out.println("Merged sorted array: " + Arrays.toString(mergedArray));
        sc.close();
    }
}

Output

Enter the size of the first array:
3
Enter the elements of the first array: 1 3 5
Enter the size of the second array:
3
Enter the elements of the second array: 2 4 6
Merged sorted array: [1, 2, 3, 4, 5, 6]
Share:

0 comments:

Post a Comment