Write a C program to accept the car value and category of insurance from the user and calculate premium to be paid.
1. Category A, here the basic premium is calculated as 2% of the car’s value.
2. Category B, here the basic premium is calculated as 3% of the car’s value.
3. Category C, here the basic premium is calculated as 5% of the car’s value.
If you are looking for a problem which you can solve with the help of decision statements then this is a perfect example.
Tutorial 📺
Watch tutorial and learn how to solve this problem. 😉
Steps 🪜
To calculate premium of car
- First of all, display all 3 car insurance categories to the user. Later, accept two values from the user, car value (floating point) and insurance category (character).
- Evaluate the insurance category entered by the user using switch. Since, you have accepted the car category in the form of character. There will be 2 cases for each category in uppercase and lowercase character.
- Now, whatever the user will enter, the matching case will be executed otherwise the default case will be executed.
Code 💻
Here, we will solve this problem using switch-case statement but you can also do it using if-else conditional statement.
#include<stdio.h>
#include<conio.h>
void main()
{
float carval=0;
char catg;
clrscr();
flushall();
printf("Category A, the basic premium is 2%c of the car's value.\n",37);
printf("Category B, the basic premium is 3%c of the car's value.\n",37);
printf("Category C, the basic premium is 5%c of the car's value.\n",37);
printf("\nPlease enter the category of Insurance Policy: ");
scanf("%c", &catg);
printf("\nPlease enter the car's value: ");
scanf("%f", &carval);
switch(catg)
{
case 'a': case 'A':
printf("\nPremium of car is: %0.2f", (carval*2)/100);
break;
case 'b': case 'B':
printf("\nPremium of car is: %0.2f", (carval*3)/100);
break;
case 'c': case 'C':
printf("\nPremium of car is: %0.2f", (carval*5)/100);
break;
default: printf("\nInvalid Category Selected.");
}
getch();
}
Example 😍
Let’s assume, the user enters 60,000 as car value and B as car insurance category.
After accepting the values from the user, the control will execute the switch statement, in which it will evaluate the insurance category.
Now, there exists a matching case, so it will execute the statements of that matching case i.e. case ‘B’ (60,000*3)/100.
Finally, it will display the message:
Premium of car is: 200.00
Let’s take another example where the user enters 100,000, a as car value and insurance category, respectively.
First of all, it will evaluate the insurance category. Since, there exists a matching case ‘a’, so it will calculate the car premium by executing its statements. Thus, the car premium will be (100,000*2)/100 = 2000.
Finally, it will display the message:
Premium of car is: 2000.00
This is how we write C program to calculate premium of a car based on accepted car value and insurance category. For more C programs, click here.
Comments