Day 1: Programming Fundamentals

Day 1: Programming Fundamentals

Welcome to the beginning of my 100 Days of DSA journey! Over the next 100 days, I’ll be diving deep into the world of Data Structures and Algorithms, working on problems, exploring concepts, and sharing my learnings every step of the way.

My goal with this challenge is not only to improve my problem-solving skills but also to document the process for others who might be on a similar path. Through these posts, I’ll share the problems I solve, the lessons I learn, and the strategies I use to overcome challenges.


Today, I started with the basics, focusing on fundamental programming concepts and algorithms. Using C++, I worked through a few simple yet essential problems to establish a strong foundation. Here’s a breakdown of what I tackled and what I discovered along the way. Let’s dive in! 🚀

Check out the GitHub repository for the same at: 100 Days of DSA


1. Check if a Number is Even or Odd

This program checks whether an entered integer is even or odd. It takes an input number from the user, determines whether it is divisible by 2 (using the modulus operator %), and then outputs the result.

The modulus operator calculates the remainder when one number is divided by another. If the remainder is 0 when dividing by 2, the number is even; otherwise, it is odd.

Code:

#include <iostream>
using namespace std;
int main() {
    int n;
    cout << "Enter an integer: ";
    cin >> n;

    // Check if the number is divisible by 2
    if (n % 2 == 0) {
        // If the remainder is 0, the number is even
        cout << "The number entered, " << n << ", is even.";
    }
    else {
        // Otherwise, the number is odd
        cout << "The number entered, " << n << ", is odd.";
    }
    return 0;
}

Output:


2. Factorial of a Number

This program calculates the factorial of a number using a loop. The factorial of a number n (denoted as n!) is the product of all positive integers from 1 to n. For example, the factorial of 5 is 5 * 4 * 3 * 2 * 1 = 120.

The program defines a function fact(int n) that calculates the factorial by iterating through numbers from 1 to n and multiplying them together. The result is returned to the main() function, where it is displayed to the user.

Code:

#include <iostream> 
using namespace std;

// Function to calculate factorial using a loop
int fact(int n) {
    int f = 1; // Initialize factorial result to 1
    for (int i = 1; i <= n; i++) {
        f = f * i; // Multiply f with the current value of i
    }
    return f; 
}

int main() {
    int f; 
    cout << "Enter an integer: ";
    cin >> f; 
    int factorial = fact(f);
    cout << "The factorial of given number, " << f << ", is " << factorial << ".";
    return 0;
}

Output:


3. First ‘n’ Fibonacci Numbers

This program prints the first n Fibonacci numbers. The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, starting from 0 and 1. For example, the first few Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8....

The program defines a function fib(int n) that uses a loop to calculate and print each Fibonacci number in the sequence, up to the nth number. The sequence is displayed directly to the user.

Code:

#include <iostream> 
using namespace std;

// Function to print the first n Fibonacci numbers
void fib(int n) {
    int a = 0; // First Fibonacci number
    int b = 1; // Second Fibonacci number
    int c;     // Variable to store the next Fibonacci number

    // Print the first two Fibonacci numbers
    cout << a << " " << b << " ";

    // Generate and print the next Fibonacci numbers using a loop
    for (int i = 2; i < n; i++) {
        c = a + b;        // Calculate the next number in the sequence
        cout << c << " "; // Print the current Fibonacci number
        a = b;            // Update 'a' to the previous 'b'
        b = c;            // Update 'b' to the current number
    }
}

int main() {
    int n; 
    cout << "Enter an integer: ";
    cin >> n; 
    fib(n);
    return 0;
}

Output:


4. Check if a Given Number is Prime

This program checks if a given number is prime. A prime number is a natural number greater than 1 that is divisible only by 1 and itself. For example, 2, 3, 5, 7, and 11 are prime numbers.

The program uses a function checkprime(int n) to determine if a number is prime. It iterates from 2 to n/2 and checks if the number is divisible by any number in this range. If it is, the number is composite (not prime); otherwise, it is prime.

Code:

#include <iostream> 
using namespace std;

// Function to check if a number is prime
void checkprime(int n) {
    int flag = 0; // Flag to indicate if the number is composite

    // Special cases: 0 and 1 are neither prime nor composite
    if (n == 0 || n == 1) {
        cout << "Entered number, " << n << ", is neither prime, nor composite.";
        return; 
    }

    // Check divisibility from 2 to n/2
    for (int i = 2; i <= n / 2; i++) {
        if (n % i == 0) {   // If divisible, it's not a prime number
            flag = 1;       // Set flag to indicate a composite number
            break;          // Exit the loop as we found a divisor
        }
    }

    // Determine and display the result based on the flag
    if (flag == 0) {
        cout << "Entered number, " << n << ", is prime." << endl;
    }
    else {
        cout << "Entered number, " << n << ", is composite (not prime)." << endl;
    }
}

int main() {
    int n; 
    cout << "Enter an integer: ";
    cin >> n; 
    checkprime(n);
    return 0; 
}

Output:


5. Reverse a Number

This program reverses a given integer. For example, if the input is 123, the output will be 321. The logic involves extracting each digit of the number using the modulus operator (%) and then rebuilding the number in reverse order.

The revn function performs the reversal using a while loop to process each digit. The reversed number is then displayed to the user.

Code:

// Implement a program to reverse a number (e.g., input: 123, output: 321).

#include <iostream> 
using namespace std;

// Function to reverse the digits of a number
void revn(int n) {
    int orig = n; // Store the original number for reference
    int rem = 0;  // Variable to hold the remainder (last digit of n)
    int rev = 0;  // Variable to store the reversed number

    // Process each digit of the number
    while (n != 0) {
        rem = n % 10;         // Extract the last digit of the number
        rev = rev * 10 + rem; // Append the digit to the reversed number
        n = n / 10;           // Remove the last digit from the original number
    }

    cout << "The reversed number is: " << rev;
}

int main() {
    int n; 
    cout << "Enter an integer: ";
    cin >> n; 
    revn(n);
    return 0; 
}

Output:


Wrapping up Day 1, I’ve reinforced my understanding of programming fundamentals through these exercises. By focusing on logic, loops, and conditionals, I’ve set a solid groundwork for diving into more advanced challenges. Every step in this journey brings me closer to mastering Data Structures and Algorithms.

Excited for what’s to come in Day 2! Stay tuned! 🚀✨