Microcontroller Embedded C Programming Lecture 87| if-else exercise implementation part-2

  • Post author:
  • Post category:Blog

 

if-else exercise implementation part-2

 

 

if-else exercise

  • Write a program which receives 2 numbers(integers) from the user and prints the biggest of two.  
  • If n1 == n2, then print “both numbers are equal”. 

 

We have completed this exercise in a previous article. But, we had the below problem. Now let’s solve this

if and else exercise
Figure 1. problem

 

Let’s modify the previous article code.  

Here our goal is to compare 2 integers. So, let’s stick to that goal. Let’s accept user input as real numbers. For that, I want to change int32_t to float. So, there is nothing wrong about that. Now you are ready to accept both integers as well as real numbers. Let’s change this format specifier %d to %f. 

But, let’s compare only integer parts. So, I would create two more variables here. int32_t n1, n2

And I will copy the num1 value to n1 and num2 value to n2. 

n1 = num1; 

n2 = num2;

What does it?

You are copying from a float variable into an integer variable. So, this will automatically remove the decimal part. Only integer parts will be stored in the variables n1 and n2. So, we are storing only the integer part of the real number. 

 

Give a warning here for the user. If we enter a real number, let’s give a warning.

So, if n1 is not equal to num1(if( n1 != num1)), that means that the user has entered a real number. That’s why let’s print a warning here. Printf(“warning! comparing only integer part\n”). Not equal to(!=) is a relational operator.

Now let’s do the same for num 2. Now let me keep this if(n1 != num1) inside the parentheses and let’s use the logical-OR (||) operator, if(n2 != num2). 

if( (n1 != num1) || (n2 != num2) )

So, if one of these expressions is true, then the user has entered a real number, and the warning will be printed.

 

And after that, let’s use n1 and n2 for the comparisons. So, let me change all num1 to n1 and num2 to n2.

The code is shown below.

int main(void)
{

float num1 , num2;
printf("Enter the first number(integer):");
scanf("%f", &num1);
printf("Enter the second number(integer):");
scanf("%f", &num2);

int32_t n1, n2;

/* we are storing only integer part of the real numbers*/
n1 = num1;
n2 = num2;

if( (n1 != num1) || (n2 != num2) ){
    printf("Warning ! comparing only integer part\n");
}

if(n1 == n2){
    printf("Numbers are equal\n");
}else{

if(n1 < n2){
    printf("%d is bigger\n",n2);
}else{
    printf("%d is bigger\n",n1);
}
}

 

Let’s execute the application. First, let’s check with integers. Enter the first number and the second number, I entered 45 and  -78. It printed 45 is bigger. That’s correct.

if and else exercise
Figure 2. Output

 

Let’s try to enter some real numbers. I enter the first number as 4.5. You can see that your application is not breaking; it is behaving properly. Then, I enter the second number as 0, and here it is now giving us a warning. Warning! Comparing only integer part. That means only 4 and 0 are compared. So, 4 is bigger.

if and else exercise
Figure 3. Output

 

Let’s try with another number like the first number is 4.0 and the second number is 4.1. So, again it gives you a warning saying only integer parts are compared, and it prints, “Numbers are equal”. That’s correct.

Output
Figure 4. Output

 

Let’s try once again. I enter the first number as 4.0 and the second number as 4.0. Here it prints, “Numbers are equal”. 

Output
Figure 5. Output

 

Let’s try with a negative real number; It shows the correct output. So, our application is behaving properly.

Output
Figure 6. Output

 

Let’s try to break this application once again by entering a character. Let’s type ‘a’. Here, we can see that your application breaks. 

Issue in output
Figure 7. Issue in output

 

Your next job is to solve this issue. So, try to modify the code so that it also responds properly for the characters. I will implement this issue in the following article.

 

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.

Leave a Reply