Day 1 : Programming Fundamentals

Kanchan RaiKanchan Rai
3 min read

I’m kicking off the 100 Days of DSA Challenge to deepen my understanding of Data Structures and Algorithms. Over the next 100 days, I’ll be tackling a variety of problems and sharing daily updates on the questions I worked on, the lessons I learned, and the obstacles I encountered.

This blog will track my progress and serve as a daily reflection on my journey. Whether you’re also working through DSA or just curious about the process, I hope these posts offer insights and inspiration. Let’s get started! 🍀


Today I completed five programming fundamentals problems, below is the implementation and output of each problem. I’ll be using C++.

Check out this github repository: 100 Days of DSA


1. Check if a number is odd or even

To find out if the number entered by the user is odd or even, we check it’s remainder using the “%“ operator by dividing it with 2. If the remainder is found to be zero then it is an even number otherwise it is odd.

Code:

#include<iostream>
using namespace std;
void check_odd_even(int num){
    if(num%2==0){
        cout<<num<<" is even";
    }
    else{
        cout<<num<<" is odd";
    }
}
int main(){
    int num;
    cout<<"Enter a number:";
    cin>>num;
    check_odd_even(num);
}

2. Check if a given number is prime or not

If a number is prime by dividing it by all integers from 2 to n/2. If no divisor is found, the number is prime otherwise, it is not prime. Special cases for 0 and 1 are also handled, as they are neither prime nor composite.

Code:

#include<iostream>
using namespace std;
void prime(int n){
    int flag=0;
    if(n==0 || n==1){
        cout<<"neither prime nor composite";
    }
    for(int i=2; i<= n/2 ;i++){
        if(n%i==0){
           flag=1;
           break;
        }
    }

    if(flag==0){
        cout<<"Prime"<<endl;
    }
    else{
        cout<<"Not Prime"<<endl;
    }
}
int main(){
    prime(7);
    prime(8);
    prime(9);
}

3 . Find the factorial of a number using loop

The factorial of a number ‘n’ is the product of all positive integers from 1 to n. This can be calculated using a for loop as follows,

Code:

#include<iostream>
using namespace std;
int factorial(int n){
    int fact=1;
    for(int i=1;i<=n;i++){
        fact=fact*i;
    }
    return fact;
}

int main(){
    int n;
    cout<<"Enter a number:";
    cin>>n;
    int res=factorial(n);
    cout<<"Factorial is "<<res;
}

4 . Print the fibonacci series upto ‘n’ terms

The fibonacci series is a special series of numbers starting from 0,1,2,3,5,8….n. Here we can notice that the third term is a sum of the first and second term. Similarly the fourth term is a sum of second and third term. We can use this simple logic in a while to implement this,

Code:

#include<iostream>
using namespace std;
//0,1,1,2,3,5,8,......n
void fibonacci(int n){   
    int term1=0;
    int term2=1;
    int next;
    cout<<term1<<" "<<term2<<" ";
    for(int i = 2; i < n; i++){
        next = term1 + term2;
        cout<<next<<" ";
        term1 = term2;
        term2 = next;
    }
}
int main(){
    fibonacci(10);
}

Fibonacci series upto the first 10 terms in the above output.

5. Find the reverse digits of a number.

To find the reverse digits of a number, we use a loop to determine its end digits repeatedly.

Code:

#include<iostream>
using namespace std;
void revnum(int n){
    int org=n;
    int rem=0;
    int rev=0;
    while(n!=0){
        rem=n%10;
        rev=rev*10+rem;
        n=n/10;
    }
    cout<<"Reversed number:"<<rev;
}
int main(){
    // reversing 123 -> 321
    revnum(123);
}

Output after reversing 123 to 321.


Building a strong foundation with programming fundamentals is the key to tackling more complex challenges ahead. These exercises sharpened my skills in logic, loops, and conditionals—essential tools for any programmer. I'm eager to continue leveling up on this journey of mastering Data Structures and Algorithms!

Looking forward to Day 2! ✨🍀

1
Subscribe to my newsletter

Read articles from Kanchan Rai directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Kanchan Rai
Kanchan Rai