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.

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:
Read an integer input from the user and store it in the variable
year.Check if the
yearis divisible by 4 (using the modulus operator%).If it is divisible by 4, proceed to step 4. Otherwise, print the message:
Year is not a Leap Year.Check if the 'year' is divisible by 100
anddivisible by 400.If it is divisible by both 100
and400 (eg. 2000), print the message:Year is a Leap Year.If it is not divisible by 400 but divisible by 100 (eg. 2200), print the message:
Year is not a Leap Year.If it is not divisible by both 100
and400, 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:
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:
Read an integer input from the user and store it in the variable
n,(eg. n=5).Read an integer input from the user and store it in the variable
a,(eg. a=1).Read an integer input from the user and store it in the variable
d,(eg. d=3).Initialize the variable
sumto0.Initialize the variable '
nextTerm'toa.Start a loop from 0 to n-1 (inclusive) with a variable
i.Calculate the value of
nextTermusing the formula:nextTerm = a + i * d.Add the value of
nextTermto the variablesum.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:
Read a long integer input from the user and store it in the variable
a,(eg. a=12).Read a long integer input from the user and store it in the variable
b,(eg. b=4).Calculate the product of
aandband store it in the variableproduct.Initialize the variable 'count' to 0.
Start a loop that continues until
productis not equal to 0.Divide
productby 10 and update its value.Increment the value of
countby 1.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:
Read an integer input from the user and store it in the variable
n,(eg. n=5).Initialize the variable
sumto0.Start a loop that continues until
nis not equal to0.Calculate the cube of
nand add it to the variablesum.Decrement the value of
nby1.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:
Read an integer input from the user and store it in the variable
num.Initialize the variable
temptonum.Initialize the variable
reverseto0.Start a loop that continues until
tempis not equal to0.Extract the last digit of
tempand store it in the variabledigit.Multiply the value of
reverseby10and add the value ofdigitto it.Divide the value of
tempby 10 and update its value.End the loop.
Check if
reverseis equal tonum.If they are equal, print the message:
Entered Number is Palindrome.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:
π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:


