Microcontroller Embedded C Programming Lecture 96| switch case exercise solution contd.

  • Post author:
  • Post category:Blog

 

switch case exercise solution

 

 

In this post, let’s continue our previous exercise.

Let’s modify the code to handle the negative numbers.

First, let’s check the case c, which is a circle. Here, after getting the radius(r) value, if(r < 0), then you have to print “radius cannot be negative” and make the area -1. So, calculate the area only if the radius is positive. That’s why I use the else statement here. 

area=3.1415*r*r is calculated only when the radius is positive; otherwise, the area will be concluded as negative.

switch(code){
case 'c':
       printf("Circle Area calculation\n");
       printf("Enter radius(r) value:");
       scanf("%f",&r);
       if( r < 0){
              printf("radius cannot be -ve\n");
              area = -1;
       }else{
              area = 3.1415 * r * r;
       }
       break;

Handle negative numbers code

Here in the case ‘t’, we can consider two values: base and height. So, after reading the values b and h, let’s add the code. if((b<0)|| (h<0)), then print “base or height cannot be -ve”; else, it calculates the area.

case 't':
        printf("Triangle Area calculation\n");
        printf("Enter base(b) value:");
        scanf("%f",&b);
        printf("Enter height(h) value:");
        scanf("%f",&h);
        if( (b < 0) || ( h < 0)){
                    printf("base or height cannot be -ve\n");
                    area = -1;
       }else{
                     area = ( b * h)/2;
       }
       break;

 

Here in case ‘z’, we use 3 variables: a, b and h. So, if(((a < 0) || (b < 0) || (h < 0))), if one among these become negative, this expression will be true, then you can print “base or height cannot be negative”. That’s correct.

case 'z':
       printf("Trapezoid Area calculation\n");
       printf("Enter base1(a) value:");
       scanf("%f",&a);
       printf("Enter base2(b) value:");
       scanf("%f",&b);
       printf("Enter height(h) value:");
       scanf("%f",&h);
       if( ( (a < 0) || ( b < 0) || (h < 0) ) ){
               printf("base or height cannot be -ve\n");
               area = -1;
       }else{
               area = (( a + b)/2) * h;
       }
       break;

 

In case ‘s’,  if(a  <  0), then print “side cannot be negative”; Otherwise, calculate the area. 

case 's':
      printf("Square Area calculation\n");
      printf("Enter side(a) value:");
      scanf("%f",&a);
      if( a < 0){
            printf("side cannot be -ve\n");
            area = -1;
      }else{
            area = a * a;
      }
      break;

 

And also do the same as a rectangle. 

Let’s execute the code and see the output. First, let me try a rectangle. Enter width value, I type 4, and length value I enter negative number -5. So, it shows width or length cannot be negative. That’s correct.

switch case exercise solution
Figure 1. Output

 

Let’s try once again.  Look at Figure 2, it shows the correct output.

switch case exercise solution
Figure 2. Output

 

We completed the discussion on switch/case statements, and so we are going to use switch case statements in our embedded programming as well. That’s all about a switch/case statement.

 

Get the Microcontroller Embedded C Programming Full Course on Here.

FastBit Embedded Brain Academy Courses

Click here: https://fastbitlab.com/course1

 

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.