Programming - jiquest

add

#

Programming

 Basic Logic and Control Structures

  1. Write a program to check if a number is even or odd.
  2. Write a program to find the largest of three numbers.
  3. Write a program to check if a given year is a leap year.
  4. Write a program to calculate the factorial of a number.
  5. Write a program to generate the Fibonacci series up to a given number.
  6. Write a program to reverse a given integer number.
  7. Write a program to check if a number is a palindrome.
  8. Write a program to find the greatest common divisor (GCD) of two numbers.
  9. Write a program to find the least common multiple (LCM) of two numbers.
  10. Write a program to print the prime numbers up to a given number.
  11. Write a program to check if a number is prime.
  12. Write a program to calculate the sum of digits of a number.
  13. Write a program to calculate the power of a number using recursion.
  14. Write a program to swap two numbers without using a third variable.
  15. Write a program to find the sum of the first N natural numbers.
  16. Write a program to check if a number is an Armstrong number.
  17. Write a program to check if a number is a perfect number.
  18. Write a program to find the average of N numbers.
  19. Write a program to find the second largest number in an array.
  20. Write a program to find the sum of all prime numbers up to a given number.

Array and String Manipulation

  1. Write a program to reverse an array.
  2. Write a program to find the maximum and minimum elements in an array.
  3. Write a program to sort an array using bubble sort.
  4. Write a program to sort an array using insertion sort.
  5. Write a program to find the missing number in an array of integers.
  6. Write a program to remove duplicates from an array.
  7. Write a program to find the common elements between two arrays.
  8. Write a program to merge two sorted arrays.
  9. Write a program to rotate an array by K positions.
  10. Write a program to find the frequency of each element in an array.
  11. Write a program to check if a string is a palindrome.
  12. Write a program to count the number of vowels and consonants in a string.
  13. Write a program to reverse a string.
  14. Write a program to find the first non-repeated character in a string.
  15. Write a program to check if two strings are anagrams of each other.
  16. Write a program to find all permutations of a given string.
  17. Write a program to remove all white spaces from a string.
  18. Write a program to check if a string contains only digits.
  19. Write a program to find the longest substring without repeating characters.
  20. Write a program to count the occurrence of each character in a string.

Searching and Sorting Algorithms

  1. Write a program to implement binary search.
  2. Write a program to implement linear search.
  3. Write a program to sort an array using selection sort.
  4. Write a program to sort an array using merge sort.
  5. Write a program to sort an array using quick sort.
  6. Write a program to sort an array using heap sort.
  7. Write a program to find the kth smallest/largest element in an array.
  8. Write a program to search an element in a rotated sorted array.
  9. Write a program to implement the Dutch National Flag problem (sort an array of 0s, 1s, and 2s).
  10. Write a program to find the intersection of two sorted arrays.

Mathematical and Number-Based Problems

  1. Write a program to check if a number is a power of two.
  2. Write a program to find the square root of a number without using a built-in function.
  3. Write a program to find the nth Fibonacci number using dynamic programming.
  4. Write a program to generate all prime numbers less than N using the Sieve of Eratosthenes.
  5. Write a program to calculate the binomial coefficient.
  6. Write a program to find the sum of all digits until the sum becomes a single digit.
  7. Write a program to find the sum of all even numbers up to a given number.
  8. Write a program to print the pascal triangle.
  9. Write a program to find the sum of the first N Fibonacci numbers.
  10. Write a program to check if a number is a perfect square.


1. Write a program to check if a number is even or odd.


import java.util.Scanner;

public class EvenOddCheck {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    System.out.print("Enter a number: ");
    int number = scanner.nextInt();

    if (number % 2 == 0) {
      System.out.println(number + " is even.");
    } else {
      System.out.println(number + " is odd.");
    }

    scanner.close();
  }
}


2. Write a program to find the largest of three numbers.

import java.util.Scanner;

public class LargestOfThree {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    // Input three numbers
    System.out.print("Enter the first number: ");
    int num1 = scanner.nextInt();

    System.out.print("Enter the second number: ");
    int num2 = scanner.nextInt();

    System.out.print("Enter the third number: ");
    int num3 = scanner.nextInt();

    // Determine the largest number
    int largest;

    if (num1 >= num2 && num1 >= num3) {
      largest = num1;
    } else if (num2 >= num1 && num2 >= num3) {
      largest = num2;
    } else {
      largest = num3;
    }

    // Output the largest number
    System.out.println("The largest number is: " + largest);

    scanner.close();
  }
}


3. Write a program to check if a given year is a leap year.

import java.util.Scanner;

public class LeapYearCheck {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    // Input the year
    System.out.print("Enter a year: ");
    int year = scanner.nextInt();

    // Check if the year is a leap year
    boolean isLeapYear;

    if (year % 4 == 0) {
      if (year % 100 == 0) {
        if (year % 400 == 0) {
          isLeapYear = true;
        } else {
          isLeapYear = false;
        }
      } else {
        isLeapYear = true;
      }
    } else {
      isLeapYear = false;
    }

    // Output the result
    if (isLeapYear) {
      System.out.println(year + " is a leap year.");
    } else {
      System.out.println(year + " is not a leap year.");
    }

    scanner.close();
  }
}



4. Write a program to calculate the factorial of a number.

import java.util.Scanner;

public class Factorial {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    // Input the number
    System.out.print("Enter a number: ");
    int number = scanner.nextInt();

    // Calculate factorial
    long factorial = 1;

    for (int i = 1; i <= number; i++) {
      factorial *= i;
    }

    // Output the result
    System.out.println("The factorial of " + number + " is: " + factorial);

    scanner.close();
  }
}



5. Write a program to generate the Fibonacci series up to a given number.

import java.util.Scanner;

public class FibonacciSeries {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    // Input the number up to which the Fibonacci series will be generated
    System.out.print("Enter the number up to which the Fibonacci series should be generated: ");
    int n = scanner.nextInt();

    // Initialize the first two numbers in the Fibonacci series
    int first = 0, second = 1;

    System.out.print("Fibonacci Series up to " + n + ": " + first + ", " + second);

    // Generate the Fibonacci series
    int next;
    while (true) {
      next = first + second;
      if (next > n) {
        break;
      }
      System.out.print(", " + next);
      first = second;
      second = next;
    }

    scanner.close();
  }
}


6. Write a program to reverse a given integer number.

import java.util.Scanner;

public class ReverseInteger {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    // Input the number
    System.out.print("Enter an integer number: ");
    int number = scanner.nextInt();

    // Initialize the variable to store the reversed number
    int reversedNumber = 0;

    // Reverse the number
    while (number != 0) {
      int digit = number % 10; // Get the last digit
      reversedNumber = reversedNumber * 10 + digit; // Add the digit to the reversed number
      number = number / 10; // Remove the last digit from the original number
    }

    // Output the reversed number
    System.out.println("Reversed Number: " + reversedNumber);

    scanner.close();
  }
}


7. Write a program to check if a number is a palindrome.

import java.util.Scanner;

public class PalindromeCheck {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    // Input the number
    System.out.print("Enter an integer number: ");
    int originalNumber = scanner.nextInt();

    int reversedNumber = 0;
    int temp = originalNumber;

    // Reverse the number
    while (temp != 0) {
      int digit = temp % 10; // Get the last digit
      reversedNumber = reversedNumber * 10 + digit; // Build the reversed number
      temp = temp / 10; // Remove the last digit from temp
    }

    // Check if the original number and reversed number are the same
    if (originalNumber == reversedNumber) {
      System.out.println(originalNumber + " is a palindrome.");
    } else {
      System.out.println(originalNumber + " is not a palindrome.");
    }

    scanner.close();
  }
}


8. Write a program to find the greatest common divisor (GCD) of two numbers.

import java.util.Scanner;

public class GCD {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    // Input two numbers
    System.out.print("Enter the first number: ");
    int num1 = scanner.nextInt();

    System.out.print("Enter the second number: ");
    int num2 = scanner.nextInt();

    // Find GCD using the Euclidean algorithm
    int gcd = findGCD(num1, num2);

    // Output the GCD
    System.out.println("The GCD of " + num1 + " and " + num2 + " is: " + gcd);

    scanner.close();
  }

  // Method to find GCD using Euclidean algorithm
  public static int findGCD(int a, int b) {
    while (b != 0) {
      int temp = b;
      b = a % b;
      a = temp;
    }
    return a;
  }
}


9. Write a program to find the least common multiple (LCM) of two numbers.

import java.util.Scanner;

public class LCM {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    // Input two numbers
    System.out.print("Enter the first number: ");
    int num1 = scanner.nextInt();

    System.out.print("Enter the second number: ");
    int num2 = scanner.nextInt();

    // Calculate LCM
    int lcm = findLCM(num1, num2);

    // Output the LCM
    System.out.println("The LCM of " + num1 + " and " + num2 + " is: " + lcm);

    scanner.close();
  }

  // Method to find LCM using the relationship between GCD and LCM
  public static int findLCM(int a, int b) {
    return (a * b) / findGCD(a, b);
  }

  // Method to find GCD using Euclidean algorithm
  public static int findGCD(int a, int b) {
    while (b != 0) {
      int temp = b;
      b = a % b;
      a = temp;
    }
    return a;
  }
}


10. Write a program to print the prime numbers up to a given number.

import java.util.Scanner;

public class PrimeNumbers {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    // Input the number up to which prime numbers will be printed
    System.out.print("Enter a number: ");
    int n = scanner.nextInt();

    System.out.println("Prime numbers up to " + n + " are:");

    // Print prime numbers from 2 to n
    for (int i = 2; i <= n; i++) {
      if (isPrime(i)) {
        System.out.print(i + " ");
      }
    }

    scanner.close();
  }

  // Method to check if a number is prime
  public static boolean isPrime(int num) {
    if (num <= 1) {
      return false;
    }
    for (int i = 2; i <= Math.sqrt(num); i++) {
      if (num % i == 0) {
        return false;
      }
    }
    return true;
  }
}


11. Write a program to check if a number is prime.

import java.util.Scanner;

public class PrimeCheck {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    // Input the number to check
    System.out.print("Enter an integer number: ");
    int number = scanner.nextInt();

    // Check if the number is prime
    boolean isPrime = isPrime(number);

    // Output the result
    if (isPrime) {
      System.out.println(number + " is a prime number.");
    } else {
      System.out.println(number + " is not a prime number.");
    }

    scanner.close();
  }

  // Method to check if a number is prime
  public static boolean isPrime(int num) {
    if (num <= 1) {
      return false;
    }
    for (int i = 2; i <= Math.sqrt(num); i++) {
      if (num % i == 0) {
        return false;
      }
    }
    return true;
  }
}


12. Write a program to calculate the sum of digits of a number.

import java.util.Scanner;

public class SumOfDigits {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    // Input the number
    System.out.print("Enter an integer number: ");
    int number = scanner.nextInt();

    // Initialize the variable to store the sum of digits
    int sum = 0;

    // Calculate the sum of digits
    while (number != 0) {
      int digit = number % 10; // Get the last digit
      sum += digit; // Add the digit to the sum
      number = number / 10; // Remove the last digit from the number
    }

    // Output the sum of digits
    System.out.println("The sum of the digits is: " + sum);

    scanner.close();
  }
}


13. Write a program to calculate the power of a number using recursion.

import java.util.Scanner;

public class PowerCalculation {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    // Input the base and exponent
    System.out.print("Enter the base number: ");
    int base = scanner.nextInt();

    System.out.print("Enter the exponent: ");
    int exponent = scanner.nextInt();

    // Calculate the power using recursion
    long result = power(base, exponent);

    // Output the result
    System.out.println(base + " raised to the power of " + exponent + " is: " + result);

    scanner.close();
  }

  // Recursive method to calculate power
  public static long power(int base, int exponent) {
    if (exponent == 0) { // Base case: any number raised to the power of 0 is 1
      return 1;
    } else if (exponent > 0) { // Recursive case: multiply the base with the result of power(base, exponent - 1)
      return base * power(base, exponent - 1);
    } else { // Handle negative exponents if needed
      return 1 / power(base, -exponent); // Convert the problem to positive exponent for simplicity
    }
  }
}


14. Write a program to swap two numbers without using a third variable.

import java.util.Scanner;

public class SwapNumbers {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    // Input the two numbers
    System.out.print("Enter the first number: ");
    int num1 = scanner.nextInt();

    System.out.print("Enter the second number: ");
    int num2 = scanner.nextInt();

    // Print the original numbers
    System.out.println("Before swapping:");
    System.out.println("num1 = " + num1);
    System.out.println("num2 = " + num2);

    // Swap the numbers without using a third variable
    num1 = num1 + num2; // Step 1: Add num1 and num2, store the result in num1
    num2 = num1 - num2; // Step 2: Subtract num2 from num1, store the result in num2
    num1 = num1 - num2; // Step 3: Subtract the new num2 from num1, store the result in num1

    // Print the swapped numbers
    System.out.println("After swapping:");
    System.out.println("num1 = " + num1);
    System.out.println("num2 = " + num2);

    scanner.close();
  }
}


15. Write a program to find the sum of the first N natural numbers.

import java.util.Scanner;

public class SumOfNaturalNumbers {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    // Input the number N
    System.out.print("Enter the value of N: ");
    int N = scanner.nextInt();

    // Calculate the sum of the first N natural numbers
    int sum = calculateSum(N);

    // Output the result
    System.out.println("The sum of the first " + N + " natural numbers is: " + sum);

    scanner.close();
  }

  // Method to calculate the sum of the first N natural numbers
  public static int calculateSum(int N) {
    // Using the formula for the sum of the first N natural numbers: sum = N * (N + 1) / 2
    return N * (N + 1) / 2;
  }
}


16. Write a program to check if a number is an Armstrong number.

import java.util.Scanner;

public class ArmstrongNumberCheck {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    // Input the number
    System.out.print("Enter an integer number: ");
    int number = scanner.nextInt();

    // Check if the number is an Armstrong number
    boolean isArmstrong = isArmstrongNumber(number);

    // Output the result
    if (isArmstrong) {
      System.out.println(number + " is an Armstrong number.");
    } else {
      System.out.println(number + " is not an Armstrong number.");
    }

    scanner.close();
  }

  // Method to check if a number is an Armstrong number
  public static boolean isArmstrongNumber(int num) {
    int originalNumber = num;
    int numberOfDigits = String.valueOf(num).length();
    int sum = 0;

    // Calculate the sum of each digit raised to the power of the number of digits
    while (num != 0) {
      int digit = num % 10;
      sum += Math.pow(digit, numberOfDigits);
      num /= 10;
    }

    // Check if the sum is equal to the original number
    return sum == originalNumber;
  }
}


17. Write a program to check if a number is a perfect number.

import java.util.Scanner;

public class PerfectNumberCheck {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    // Input the number
    System.out.print("Enter an integer number: ");
    int number = scanner.nextInt();

    // Check if the number is a perfect number
    boolean isPerfect = isPerfectNumber(number);

    // Output the result
    if (isPerfect) {
      System.out.println(number + " is a perfect number.");
    } else {
      System.out.println(number + " is not a perfect number.");
    }

    scanner.close();
  }

  // Method to check if a number is a perfect number
  public static boolean isPerfectNumber(int num) {
    if (num <= 1) {
      return false;
    }

    int sum = 0;

    // Find the sum of the proper divisors
    for (int i = 1; i <= num / 2; i++) {
      if (num % i == 0) {
        sum += i;
      }
    }

    // Check if the sum of the proper divisors is equal to the original number
    return sum == num;
  }
}


18. Write a program to find the average of N numbers.

import java.util.Scanner;

public class AverageOfNumbers {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    // Input the number of elements
    System.out.print("Enter the number of elements: ");
    int N = scanner.nextInt();

    // Validate input
    if (N <= 0) {
      System.out.println("The number of elements must be greater than zero.");
      scanner.close();
      return;
    }

    // Input the numbers and calculate the sum
    double sum = 0;
    for (int i = 1; i <= N; i++) {
      System.out.print("Enter number " + i + ": ");
      double number = scanner.nextDouble();
      sum += number;
    }

    // Calculate the average
    double average = sum / N;

    // Output the result
    System.out.println("The average of the " + N + " numbers is: " + average);

    scanner.close();
  }
}


19. Write a program to find the second largest number in an array.

import java.util.Scanner;

public class SecondLargestNumber {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    // Input the number of elements in the array
    System.out.print("Enter the number of elements in the array: ");
    int n = scanner.nextInt();

    // Validate input
    if (n < 2) {
      System.out.println("Array must have at least two elements.");
      scanner.close();
      return;
    }

    // Initialize the array
    int[] numbers = new int[n];

    // Input the elements of the array
    System.out.println("Enter the elements of the array:");
    for (int i = 0; i < n; i++) {
      numbers[i] = scanner.nextInt();
    }

    // Find the second largest number
    int firstLargest = Integer.MIN_VALUE;
    int secondLargest = Integer.MIN_VALUE;

    for (int num: numbers) {
      if (num > firstLargest) {
        secondLargest = firstLargest;
        firstLargest = num;
      } else if (num > secondLargest && num < firstLargest) {
        secondLargest = num;
      }
    }

    // Output the result
    if (secondLargest == Integer.MIN_VALUE) {
      System.out.println("There is no second largest number.");
    } else {
      System.out.println("The second largest number is: " + secondLargest);
    }

    scanner.close();
  }
}


20. Write a program to find the sum of all prime numbers up to a given number.

import java.util.Scanner;

public class SumOfPrimes {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    // Input the upper limit
    System.out.print("Enter an upper limit: ");
    int upperLimit = scanner.nextInt();

    // Validate input
    if (upperLimit < 2) {
      System.out.println("There are no prime numbers less than 2.");
      scanner.close();
      return;
    }

    // Calculate the sum of prime numbers up to the upper limit
    int sum = 0;
    for (int i = 2; i <= upperLimit; i++) {
      if (isPrime(i)) {
        sum += i;
      }
    }

    // Output the result
    System.out.println("The sum of all prime numbers up to " + upperLimit + " is: " + sum);

    scanner.close();
  }

  // Method to check if a number is prime
  public static boolean isPrime(int num) {
    if (num <= 1) {
      return false;
    }
    if (num == 2) {
      return true; // 2 is the only even prime number
    }
    if (num % 2 == 0) {
      return false; // Exclude all other even numbers
    }
    for (int i = 3; i <= Math.sqrt(num); i += 2) {
      if (num % i == 0) {
        return false;
      }
    }
    return true;
  }
}


21. Write a program to reverse an array.




22. Write a program to find the maximum and minimum elements in an array.




20. Write a program to find the sum of all prime numbers up to a given number.




20. Write a program to find the sum of all prime numbers up to a given number.




20. Write a program to find the sum of all prime numbers up to a given number.




20. Write a program to find the sum of all prime numbers up to a given number.




20. Write a program to find the sum of all prime numbers up to a given number.




20. Write a program to find the sum of all prime numbers up to a given number.




20. Write a program to find the sum of all prime numbers up to a given number.




20. Write a program to find the sum of all prime numbers up to a given number.




20. Write a program to find the sum of all prime numbers up to a given number.




20. Write a program to find the sum of all prime numbers up to a given number.




20. Write a program to find the sum of all prime numbers up to a given number.




20. Write a program to find the sum of all prime numbers up to a given number.




20. Write a program to find the sum of all prime numbers up to a given number.




20. Write a program to find the sum of all prime numbers up to a given number.




20. Write a program to find the sum of all prime numbers up to a given number.




20. Write a program to find the sum of all prime numbers up to a given number.




20. Write a program to find the sum of all prime numbers up to a given number.




20. Write a program to find the sum of all prime numbers up to a given number.