Skip to main content

Command Palette

Search for a command to run...

DSA Series: Basic Problems (Part-01)

Hello πŸ‘‹ Reader, In this article, We will learn & solve 5 coding problems: Leap Year, Sum of AP, Product Sum, Sum of first N terms, and Palindrome.

Updated
β€’7 min read
DSA Series: Basic Problems 
(Part-01)
R

Hello! πŸ‘‹ I'm a UI/UX Designer and Software Developer with a strong interest in Open Source contributions and DSA Problem Solving. I am also enthusiastic about DevOps practices and AI/ML.

Problem-01: Leap Year Program❓

πŸ”— Problem Link

πŸ€” Intuition:

A leap year is exactly divisible by 4 except for century years (years ending with 00). The century year is a leap year only if it is perfectly divisible by 400.

For example,

  • 1997 is not a leap year

  • 2000 is a leap year

  • 2016 is a leap year

πŸ—£οΈ Explanation:

Here is the step-by-step explanation of the Leap Year problem:

  1. Read an integer input from the user and store it in the variable year.

  2. Check if the year is divisible by 4 (using the modulus operator %).

  3. If it is divisible by 4, proceed to step 4. Otherwise, print the message: Year is not a Leap Year.

  4. Check if the 'year' is divisible by 100 and divisible by 400.

  5. If it is divisible by both 100 and 400 (eg. 2000), print the message: Year is a Leap Year.

  6. If it is not divisible by 400 but divisible by 100 (eg. 2200), print the message: Year is not a Leap Year .

  7. If it is not divisible by both 100 and 400, print the message: Year is a Leap Year.

πŸ‘¨β€πŸ’» Solution Code:

import java.util.Scanner;

public class LeapYear {
  public static void main(String[] args) {
    System.out.println("Enter the year to check whether it is a leap year or not!");
    Scanner sc = new Scanner(System.in);
    int year = sc.nextInt();
    if (year % 4 == 0) {
      if (year % 100 == 0 && year % 400 == 0) {
        System.out.println(year + " is a Leap Year.");
      } else if (year % 400 != 0 && year % 100 == 0) {
        System.out.println(year + " is not a Leap Year.");
      } else {
        System.out.println(year + " is a Leap Year.");
      }
    } else {
      System.out.println(year + " is not a Leap Year.");
    }
    sc.close();
  }
}

πŸ‘‰ Things to Remember:

πŸ’‘
The % (modulo) operator is used to check whether a number is divisible or not.
πŸ’‘
To check Leap Year, it must be divisible by 4, except for years that are divisible by 100 but not 400.

Problem-02: Sum Of AP Program❓

πŸ”— Problem Link

πŸ€” Intuition:

An arithmetic series refers to a series with a constant difference between consecutive terms. The first term of the series is denoted as 'a' and the common difference as 'd'. The sequence can be written as a, a + d, a + 2d, a + 3d, and so on.

For example,

  • 1, 4, 7, 10, 13, 16, . . . . . . . . . . .

πŸ—£οΈ Explanation:

Here is the step-by-step explanation of the Sum Of AP problem:

  1. Read an integer input from the user and store it in the variable n,(eg. n=5).

  2. Read an integer input from the user and store it in the variable a,(eg. a=1).

  3. Read an integer input from the user and store it in the variable d,(eg. d=3).

  4. Initialize the variable sum to 0.

  5. Initialize the variable 'nextTerm' to a.

  6. Start a loop from 0 to n-1 (inclusive) with a variable i.

  7. Calculate the value of nextTerm using the formula: nextTerm = a + i * d.

  8. Add the value of nextTerm to the variable sum.

  9. Print the message: The Sum of Your AP Series is: 35 .

πŸ‘‰ Here a is the first term, d is the common difference, and n is the number of terms.

πŸ‘¨β€πŸ’» Solution Code:

import java.util.Scanner;

public class SumOfAP {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter Nth Term of AP: ");
    int n = sc.nextInt();
    System.out.print("Enter First Term of AP: ");
    int a = sc.nextInt();
    System.out.print("Enter Common Difference of AP: ");
    int d = sc.nextInt();
    int sum = 0;
    int nextTerm = a;
    for (int i = 0; i < n; i++) {
      nextTerm = a + i * d;
      sum += nextTerm;
    }
    System.out.println("The Sum of Your AP Series is: " + sum);
    sc.close();
  }
}

Problem-03: Product Sum❓

πŸ”— Problem Link

πŸ€” Intuition:

In this problem, we will simply multiply the values of a and b and then count the number of digits of their product.

πŸ—£οΈ Explanation:

Here is the step-by-step explanation of the Product Sum problem:

  1. Read a long integer input from the user and store it in the variable a,(eg. a=12).

  2. Read a long integer input from the user and store it in the variable b,(eg. b=4).

  3. Calculate the product of a and b and store it in the variable product.

  4. Initialize the variable 'count' to 0.

  5. Start a loop that continues until product is not equal to 0.

  6. Divide product by 10 and update its value.

  7. Increment the value of count by 1.

  8. Print the message: The Total Number of Digits in the product of two numbers is: 2'.

πŸ‘¨β€πŸ’» Solution Code:

import java.util.Scanner;

public class ProductSum {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter Number 1: ");
    long a = sc.nextLong();
    System.out.print("Enter Number 2: ");
    long b = sc.nextLong();
    long product = a * b;
    long count = 0;
    while (product != 0) {
      product = product / 10;
      count++;
    }
    System.out.println("The Total Number of Digits in the product of two numbers is: " + count);
    sc.close();
  }
}

Problem-04: Sum of first n terms❓

πŸ”— Problem Link

πŸ€” Intuition:

In this problem, we will simply cube each value of n and add each cubic value into a variable in each iteration.

πŸ—£οΈ Explanation:

Here is the step-by-step explanation of the Sum of first n terms problem:

  1. Read an integer input from the user and store it in the variable n,(eg. n=5).

  2. Initialize the variable sum to 0.

  3. Start a loop that continues until n is not equal to 0.

  4. Calculate the cube of n and add it to the variable sum.

  5. Decrement the value of n by 1 .

  6. Print the message: The Sum of Series: 225.

πŸ‘¨β€πŸ’» Solution Code:

import java.util.Scanner;

public class SumOfNthSeries {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter the Nth term of the series: ");
    int n = sc.nextInt();
    long sum = 0;
    while (n != 0) {
      sum += (n * n * n);
      n--;
    }
    System.out.println("The Sum of Series: " + sum);
    sc.close();
  }
}

Problem-05: Palindrome Number❓

πŸ”— Problem Link

πŸ€” Intuition:

To determine if a number is a palindrome, we can reverse the input number and compare it with the original input. If they are the same, then the number is a palindrome. If not, then it is not a palindrome.

πŸ—£οΈ Explanation:

Here is the step-by-step explanation of the Palindrome Number problem:

  1. Read an integer input from the user and store it in the variable num.

  2. Initialize the variable temp to num.

  3. Initialize the variable reverse to 0.

  4. Start a loop that continues until temp is not equal to 0.

  5. Extract the last digit of temp and store it in the variable digit.

  6. Multiply the value of reverse by 10 and add the value of digit to it.

  7. Divide the value of temp by 10 and update its value.

  8. End the loop.

  9. Check if reverse is equal to num.

  10. If they are equal, print the message: Entered Number is Palindrome.

  11. If they are not equal, print the message: Entered Number is not Palindrome.

πŸ‘¨β€πŸ’» Solution Code:

import java.util.Scanner;

public class PalindromeNumber {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter a Number to check Palindrome: ");
    int num = sc.nextInt();
    int temp = num;
    int reverse = 0;
    while (temp != 0) {
      int digit = temp % 10;
      reverse = reverse * 10 + digit;
      temp /= 10;
    }
    if (reverse == num) {
      System.out.println("Entered Number is Palindrome.");
    } else {
      System.out.println("Entered Number is not Palindrome.");
    }
    sc.close();
  }
}

πŸ‘‰ Things to Remember:

πŸ’‘
To extract the last digit from a number, we take modulo(%) of that number with 10.
πŸ’‘
To check if a number is a palindrome, we need to check if its reverse is equal to the original number.

πŸ‘‡Checkout this Repository to access all the Articles, Problems Links, & Solution Code

❗️OK, If you working with any other language then don't worry simply go to ChatGPT and convert this above Java code to your preferred language.

❗️ If you have any doubts about this article then Ask me your doubt on Discord Server.

🀝 Let's Connect with me: