Day 4 of DSA Daily: Exploring Switch Statements and Functions

Photo by Chris Ried on Unsplash

Day 4 of DSA Daily: Exploring Switch Statements and Functions

·

2 min read

Today I completed the eighth video in Babar's DSA Playlist. You can find the video here: Babar's DSA Playlist - Video 8.

This video was fantastic! It covered switch statements and functions in a way that made it so much easier for me to understand. Specifically, the problems related to switch statements were really helpful. I managed to solve both of them by myself:

  1. Creating a simple calculator.

     #include<iostream>
    
     using namespace std;
    
     int main(){
         int a , b;
         char c;
    
         cout<<"Enter first Number: ";
         cin>>a;
         cout<<"Enter Second Number: ";
         cin>>b;
         cout<<"Enter operator: ";
         cin>>c;
    
         switch (c){
             case '+':
                 cout<<(a+b)<<endl;
                 break;
             case '-':
                 cout<<(a-b)<<endl;
                 break;
             case '/':
                 cout<<(a/b)<<endl;
                 break;
             case '*':
                 cout<<(a*b)<<endl;
                 break;
             case '%':
                 cout<<(a%b)<<endl;
                 break;
             default:
                 cout<<"Enter a valid operator"<<endl;
         }
    
     }
    
  2. Determining the number of notes in a given amount.

     #include<iostream>
    
     using namespace std;
    
     int main(){
         int amount;
         cout<<"Enter the amount";
         cin>>amount;
         int rs100,rs50,rs20,rs1;
    
         switch(1){
             case 1 :{
                 rs100=amount/100;
                 amount=amount%100;
                 cout<<"Number of 100 rs notes "<<rs100<<endl;
             }
             case 2 :{
                 rs50=amount/50;
                 amount=amount%50;
                 cout<<"Number of 50 rs notes "<<rs50<<endl;
             }
             case 3 :{
                 rs20=amount/20;
                 amount=amount%20;
                 cout<<"Number of 20 rs notes "<<rs20<<endl;
             }
             case 4 :{
                 rs1=amount/1;
                 amount=amount%1;
                 cout<<"Number of 1 rs notes "<<rs1<<endl;
             }
         }
     }
    

After that, I started with functions. Alongside the video, I solved questions related to nCr and even or odd number, which helped to understand and implement functions

//nCr

#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 ncr(int n, int r){
    int num = factorial(n);
    int denom = factorial(r)*factorial(n-r);

    return num/denom;
}

int main(){
    int n,r;
    cin>>n>>r;
    cout<<"Anuswer "<<ncr(n,r)<<endl;
}
//Even or Odd Number
#include<iostream>

using namespace std;
bool evenodd(int n){
    if(n%2==0){
        return true;
    }else{
        return false;
    }
}
int main(){
    int num;
    cin>>num;

    if(evenodd(num)==true){
        cout<<"It is a even number";
    }else{
        cout<<"it is a Odd number";
    }
}

The video also had some homework exercises, which I plan to do tomorrow. They seem like a great way to reinforce what I've learned today.

Overall, it was a highly productive day! I'm looking forward to solving more problems and learning new concepts tomorrow.

For those interested, I share my daily DSA blog posts on Hashnode: Daily DSA Blog.

Additionally, I also share updates on my DSA journey on Twitter and LinkedIn. Here are the links if you'd like to follow along:

Twitter: Jaychav37720654 LinkedIn: Jay Chavan

Feel free to check them out.

Until next time,
Jay Chavan