# Day 1 (Programming Fundamentals)

Welcome to Day 1 of my **100 Days of DSA** journey! Today, I tackled the fundamentals of programming, focusing on understanding key concepts and problem-solving approaches. Here are the problems I worked on and how I solved them.

I have also maintained a GitHub repository for the same so please free to check it out as well: [https://github.com/AayJayTee/100-Days-DSA](https://github.com/AayJayTee/100-Days-DSA)

---

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

```cpp
#include <iostream>
using namespace std;

// Function to check whether a number is even or odd
void odd_even(int n) {
    if (n % 2 == 0) {
        cout << "The number is even";
    } else {
        cout << "The number is odd";
    }
}

int main() {
    int n;
    cout << "Enter a number: ";
    cin >> n;
    odd_even(n); // Call the function to check even/odd
    return 0;
}
```

This program determines if a number is even or odd by checking if it's divisible by 2. It uses the modulus operator (`%`) to compute the remainder when the number is divided by 2. If the remainder is 0, the number is even; otherwise, it’s odd.

---

### Q3. Write a program to find the factorial of a number using a loop.

```cpp
#include <iostream>
using namespace std;

// Function to calculate the factorial of a number
void factorial(int n) {
    int fact = 1; // Initialize factorial to 1
    for (int i = 1; i <= n; i++) {
        fact *= i; // Multiply fact by i in each iteration
    }
    cout << "Factorial is: " << fact;
}

int main() {
    int n;
    cout << "Enter the number: ";
    cin >> n;
    factorial(n); // Call the function to calculate factorial
    return 0;
}
```

This program calculates the factorial of a given number using an iterative approach. A variable `fact` is initialized to 1 to store the result. A `for` loop multiplies `fact` by each number from 1 up to `n`, incrementally building the factorial value. The final factorial is displayed to the user. This solution demonstrates iterative computations and basic arithmetic operations.

---

### Q3. Create a program to print the first n Fibonacci numbers.

```cpp
#include <iostream>
using namespace std;

// Function to calculate Fibonacci numbers using recursion
int fibonacci(int n) {
    if (n == 0 || n == 1) {
        return n; // Base case: Fibonacci of 0 is 0, and of 1 is 1
    } else {
        return fibonacci(n - 1) + fibonacci(n - 2); // Recursive formula
    }
}

int main() {
    int n;
    cout << "Enter the number of Fibonacci numbers you want to print: ";
    cin >> n;
    cout << "Fibonacci sequence: ";
    for (int i = 0; i < n; i++) {
        cout << fibonacci(i) << " "; // Print Fibonacci sequence up to n terms
    }
    return 0;
}
```

This program generates the Fibonacci sequence using recursion. The Fibonacci numbers are calculated such that each term is the sum of the two preceding terms, with the first two numbers being 0 and 1. A recursive function `fibonacci()` is defined to compute the sequence. The program uses a loop to call this function for the first `n` terms and prints the sequence. It demonstrates recursion and handling sequences programmatically.

---

### Q4. Write a program to check if a given number is prime

```cpp
#include <iostream>
using namespace std;

// Function to check if a number is prime
bool isPrime(int n) {
    if (n <= 1) {
        return false; // Numbers 1 and below are not prime
    }
    for (int i = 2; i < n; i++) {
        if (n % i == 0) {
            return false; // If divisible by any number other than 1 and itself, not prime
        }
    }
    return true; // Otherwise, the number is prime
}

int main() {
    int n;
    cout << "Enter a number: ";
    cin >> n;
    if (isPrime(n)) {
        cout << "Number is prime";
    } else {
        cout << "Number is not prime";
    }
    return 0;
}
```

This program determines if a number is prime by checking divisibility. Numbers greater than 1 are checked for factors using a `for` loop starting from 2. If any factor divides the number evenly, it's not prime. Special cases, like 1, are handled separately. A `bool` function `isPrime()` returns `true` or `false`, and the result is displayed as "Number is prime" or "Number is not prime."

---

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

```cpp
#include <iostream>
using namespace std;

// Function to reverse a number
void reverseno(int n) {
    int reversed = 0; // Initialize reversed number
    while (n > 0) {
        int lastdigit = n % 10;       // Extract the last digit
        reversed = reversed * 10 + lastdigit; // Append the digit to reversed
        n /= 10;                      // Remove the last digit from n
    }
    cout << "Reversed number: " << reversed;
}

int main() {
    int n;
    cout << "Enter number: ";
    cin >> n;
    reverseno(n); // Call the function to reverse the number
    return 0;
}
```

This program reverses the digits of a number by repeatedly extracting the last digit. The last digit is obtained using `n % 10` and added to a new variable `a`, which is shifted left by multiplying by 10. The input number is reduced by dividing it by 10 until it becomes 0. The reversed number is then printed. This solution showcases the use of loops and arithmetic for digit manipulation.

---

That concludes Day 1 of my **100 Days of DSA**! Today, I revisited the basics and sharpened my problem-solving skills with these fundamental problems. These exercises reinforced essential concepts like loops, conditionals, recursion, and number manipulation
