Welcome to the first day of our DSA journey! In this article, we’ll dive into some core programming concepts that will lay the foundation for the more complex algorithms and data structures we'll explore later. By solving a few simple problems, you'll strengthen your understanding of loops, conditions, and functions—essential skills for anyone learning programming. We’ll be working in C++ to solve these fundamental problems.
1. Check if a Number is Even or Odd
This is one of the simplest problems to start with! We need to determine whether a number is even (divisible by 2) or odd (not divisible by 2). In C++, we can use the modulus operator (%
) to check the remainder when a number is divided by 2. If the remainder is 0, the number is even; otherwise, it’s odd.
The program prompts the user to enter a number and checks if the number is divisible by 2 using the modulus operator. If the result is 0, it prints that the number is even; otherwise, it prints that it is odd.
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (num % 2 == 0) {
cout << num << " is even." << endl;
} else {
cout << num << " is odd." << endl;
}
return 0;
}
2. Find the Factorial of a Number
The factorial of a number is the product of all positive integers up to that number. For example, the factorial of 5 (5!
) is 5 * 4 * 3 * 2 * 1 = 120
.
We can compute the factorial using a loop or using recursion in C++.
The program uses a for
loop to multiply each number from 1 up to the given number to find the factorial. If the user inputs a negative number, it displays a message saying the factorial isn’t defined for negative numbers.
#include <iostream>
using namespace std;
int main() {
int num;
long long factorial = 1;
cout << "Enter a number: ";
cin >> num;
if (num < 0) {
cout << "Factorial is not defined for negative numbers." << endl;
} else {
for (int i = 1; i <= num; i++) {
factorial *= i; // Multiply factorial by each number
}
cout << "Factorial of " << num << " is " << factorial << endl;
}
return 0;
}
3. Print the First n Fibonacci Numbers
The Fibonacci sequence starts with the numbers 0 and 1, and each subsequent number is the sum of the previous two. For example, the first 7 Fibonacci numbers are: 0, 1, 1, 2, 3, 5, 8
.
Let’s create a program that prints the first n
Fibonacci numbers.
This program uses two variables (a
and b
) to store the first two Fibonacci numbers, 0
and 1
. The loop calculates the next Fibonacci number by adding the previous two numbers and prints them until it reaches the desired number of terms.
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of Fibonacci numbers to print: ";
cin >> n;
int a = 0, b = 1, next;
if (n <= 0) {
cout << "Please enter a positive integer." << endl;
} else if (n == 1) {
cout << "Fibonacci series: " << a << endl;
} else {
cout << "Fibonacci series: " << a << ", " << b;
for (int i = 3; i <= n; i++) {
next = a + b;
cout << ", " << next;
a = b;
b = next;
}
cout << endl;
}
return 0;
}
4. Check if a Given Number is Prime
A prime number is a number greater than 1 that has no divisors other than 1 and itself. For example, 2, 3, 5, and 7 are prime numbers.
To check if a number is prime, we need to check whether it’s divisible by any number other than 1 and itself.
This program checks if a number is divisible by any number from 2 to the square root of the given number. If a divisor is found, the number is not prime. Otherwise, it’s prime.
#include <iostream>
#include <cmath>
using namespace std;
bool isPrime(int num) {
if (num <= 1) return false; // Numbers less than or equal to 1 are not prime
for (int i = 2; i <= sqrt(num); i++) {
if (num % i == 0) {
return false; // Number is divisible by i, hence not prime
}
}
return true; // If no divisors are found, the number is prime
}
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (isPrime(num)) {
cout << num << " is a prime number." << endl;
} else {
cout << num << " is not a prime number." << endl;
}
return 0;
}
5. Reverse a Number
Reversing a number means reversing the order of its digits. For example, if the input is 123
, the output should be 321
.
In this program, we repeatedly extract the last digit of the number using the modulus operator (% 10
). This digit is added to a new number (reversed
), and the original number is reduced by dividing it by 10 until all digits have been reversed.
#include <iostream>
using namespace std;
int main() {
int num, reversed = 0;
cout << "Enter a number: ";
cin >> num;
while (num != 0) {
int digit = num % 10; // Extract the last digit
reversed = reversed * 10 + digit; // Add it to the reversed number
num /= 10; // Remove the last digit from the original number
}
cout << "Reversed number: " << reversed << endl;
return 0;
}
That’s it for Day 1 of my DSA journey! Today, we’ve covered some key programming fundamentals, including loops, conditionals, and functions. These concepts will serve as a solid foundation as we move forward with Data Structures and Algorithms.