The length and breadth / width of a rectangle are entered through the keyboard. Write a program to calculate the area of the rectangle.
Rectangle is a quadrilateral(i.e., it has 4 sides). And all the 4 angles of the rectangle are of 90 degree. To calculate area of a rectangle we need its length and width/breadth.
Temperature of a city in Fahrenheit degree is input through the keyboard. Write a program to convert this temperature into Degree Celsius / Centigrade.
From our previous video tutorial Convert Degree Celsius To Fahrenheit: C Program we already know the formula to convert temperature from Celsius to Fahrenheit. We will use the same formula and convert it to calculate Degree celsius from Fahrenheit.
Temperature of a city in Degree Celsius / Centigrade degree is input through the keyboard. Write a program to convert this temperature into Fahrenheit.
Fahrenheit = (Centigrade * (9/5)) + 32; But in C programming, any number divided by an integer number will return integer value. So 9/5 will give 1 and not 1.8
So we need to be careful while doing division operation in C. To solve this issue we can write following formula in C program to convert degree celsius to Fahrenheit.
Ramesh’s basic salary is input through the keyboard. His dearness allowance is 40% of basic salary, and house rent allowance is 20% of basic salary. Write a program to calculate his gross salary.
#include < stdio.h >
int main()
{
float bs, hra, da, gs;
printf("Enter basic salary\n");
scanf("%f", &bs);
hra = bs * (20/100);
da = bs * (40/100);
gs = bs + hra + da;
printf("Gross Salary = %f\n", gs);
return 0;
}
Output 1: Enter basic salary 10000 Gross Salary = 10000.000000
Output 2: Enter basic salary 20000 Gross Salary = 20000.000000
You can see that the answer is wrong. This is because we are dividing 20 and 40 by an integer number i.e., 100, so it returns only the integer part of the result which is 0. So 0 multiplied by anything will be 0. So HRA and DA will be 0. So basic salary and gross salary will be shown equal in that case, which is wrong result.
Formula to calculate Gross Salary is: Gross Salary = Basic Salary + Dearness allowance + House Rent Allowance.
expression_1 is a comparison/conditional argument. expression_2 is executed/returned if expression_1 results in true, expression_3 gets executed/returned if expression_1 is false.
We are illustrating the use of multiple nested ternary operator in this program. We have a nested ternary operator in place of expression2 as well as expression3.
Leap Year Logic
Leap Year 1. If a year is a century year(years ending with 00) and if it’s perfectly divisible by 400, then it’s a leap year. 2. If the given year is not a century year and it’s perfectly divisible by 4, then it’s a leap year.
Not Leap Year 1. If the year entered is a century year(perfectly divisible by 100), but not perfectly divisible by 400. Then it’s not a leap year. 2. A year which is not a century year and is not perfectly divisible by 4 is not a leap year.
C Program To Check Leap Year Using Nested Ternary Operator
In this video tutorial we illustrate the use of multiple nested ternary operator / conditional operator inside a ternary operator.
First we check if given year is perfectly divisible by 100. If its true, then the entered year is a century year(year ending with 00). If the year is also perfectly divisible by 400, then its leap year, if not, its not a leap year.
If the user entered year is not a century year, then we check if the year is perfectly divisible by 4. If yes, then its a leap year, if not, then its not a leap year.