Chapter 4 - Let Us C
If the lengths of three sides of a triangle are entered through the keyboard, write a program to check whether the triangle is an isosceles, an equilateral, a scalene or a right-angled triangle.
#include <stdio.h>
int main(){
int a, b, c;
printf("Enter the sides of the triangle : ");
scanf("%d%d%d", &a, &b, &c);
(a == b && b == c) ? printf("It is an equilateral triangle") :
((a * a + b * b == c * c) || (b * b + c * c == a * a) || (c * c + a * a == b * b)) ?
printf("The triangle is right-angled") :
(a == b || b == c || c == a) ? printf("The triangle is isosceles") :
printf("The triangle is scalene");
return 0;
}
Step 2: Prompt the user to enter the lengths of the three sides and store them in `a`, `b`, and `c` using `scanf`.
Step 3: Use the ternary operator (`condition ? value_if_true : value_if_false`) to determine the type of triangle.
Step 4: Check if all three sides are equal (`a == b && b == c`). If true, print "It is an equilateral triangle".
Step 5: If the triangle is not equilateral, check if it is a right-angled triangle. This is done by checking if the sum of the squares of any two sides is equal to the square of the third side: `(a * a + b * b == c * c) || (b * b + c * c == a * a) || (c * c + a * a == b * b)`. If true, print "The triangle is right-angled".
Step 6: If the triangle is neither equilateral nor right-angled, check if any two sides are equal: `(a == b || b == c || c == a)`. If true, print "The triangle is isosceles".
Step 7: If none of the above conditions are met, the triangle must be scalene. Print "The triangle is scalene".
Step 8: The program returns 0, indicating successful execution.
In digital world colors are specified in Red-Green-Blue (RGB) format, with values of R, G, B varying on an integer scale from 0 to 255. In print publishing the colors are mentioned in Cyan-Magenta-Yellow-Black (CMYK) format, with values of C, M, Y, and K varying on a real scale from 0.0 to 1.0. Write a program that converts RGB color to CMYK color.
#include <stdio.h>
int main()
{
int r, g, b;
float w, c, y, m, k;
printf("Enter the values of RGB : ");
scanf("%d%d%d", &r, &g, &b);
if (r >= 0 && r <= 255 && g >= 0 && g <= 255 && b >= 0 && b <= 255)
{
w = (r > g) ? ((r > b) ? r : b) : ((g > b) ? g : b);
w /= 255;
if (w == 0)
{
c = m = y = 0;
k = 1;
}
else
{
c = (w - (float)r / 255) / w;
m = (w - (float)g / 255) / w;
y = (w - (float)b / 255) / w;
k = 1 - w;
}
printf("(CYMK) = (%.2f,%.2f,%.2f,%.2f)", c, m, y, k);
}
else
{
printf("Enter the RGB Values between 0 to 255");
}
return 0;
}
Step 2: Prompt the user to enter the RGB values and store them in `r`, `g`, and `b` using `scanf`.
Step 3: Check if the entered RGB values are within the valid range (0 to 255) using the condition `r >= 0 && r <= 255 && g >= 0 && g <= 255 && b >= 0 && b <= 255`.
Step 4: If the RGB values are valid:
- Determine the maximum of the RGB values and store it in `w`. This is done using nested ternary operators: `w = (r > g) ? ((r > b) ? r : b) : ((g > b) ? g : b);`
- Normalize `w` by dividing it by 255: `w /= 255;`
- Check if `w` is 0.
- If `w` is 0, it means all RGB values are 0, so set `c`, `m`, and `y` to 0 and `k` to 1.
- Otherwise (if `w` is not 0):
- Calculate cyan (c): `c = (w - (float)r / 255) / w;`
- Calculate magenta (m): `m = (w - (float)g / 255) / w;`
- Calculate yellow (y): `y = (w - (float)b / 255) / w;`
- Calculate black (k): `k = 1 - w;`
- Print the calculated CMYK values using `printf` with two decimal places for each value: `printf("(CYMK) = (%.2f,%.2f,%.2f,%.2f)", c, m, y, k);`
Step 6: The program returns 0, indicating successful execution.
A certain grade of steel is graded according to the following conditions:
(i) Hardness must be greater than 50
(ii) Carbon content must be less than 0.7
(iii) Tensile strength must be greater than 5600
The grades are as follows:
Grade is 10 if all three conditions are met
Grade is 9 if conditions (i) and (ii) are met
Grade is 8 if conditions (ii) and (iii) are met
Grade is 7 if conditions (i) and (iii) are met
Grade is 6 if only one condition is met
Grade is 5 if none of the conditions are met
Write a program, which will require the user to give values of hardness, carbon content and tensile strength of the steel under consideration and output the grade of the steel.
#include <stdio.h>
int main(){
float hardness,carbon,tensile;
printf("Enter hardness of steel : ");
scanf("%f",&hardness);
printf("Enter carbon content of steel : ");
scanf("%f",&carbon);
printf("Enter tensile strength of steel : ");
scanf("%f",&tensile);
int conditions_met = (hardness > 50) + (carbon < 0.7) + (tensile > 5600);
printf("The Grade of the steel is %d",
(conditions_met == 3) ? 10 :
(conditions_met == 2) ? ((hardness > 50 && carbon < 0.7) ? 9 :
(carbon < 0.7 && tensile > 5600) ? 8 : 7) :
(conditions_met == 1) ? 6 : 5);
return 0;
}
Step 2: Prompt the user to enter the hardness, carbon content, and tensile strength of the steel and store them in the respective variables using `scanf`.
Step 3: Calculate the number of conditions met. The expression `(hardness > 50) + (carbon < 0.7) + (tensile > 5600)` cleverly uses the fact that boolean true evaluates to 1 and false to 0 in C. So, `conditions_met` will be the sum of how many of the three conditions are true (0, 1, 2, or 3).
Step 4: Use nested ternary operators to determine the grade of the steel based on the number of conditions met:
- If all three conditions are met (`conditions_met == 3`), the grade is 10.
- Otherwise, if two conditions are met (`conditions_met == 2`):
- If hardness is greater than 50 and carbon content is less than 0.7, the grade is 9.
- Otherwise, if carbon content is less than 0.7 and tensile strength is greater than 5600, the grade is 8.
- Otherwise (if hardness > 50 and tensile > 5600), the grade is 7.
- Otherwise, if only one condition is met (`conditions_met == 1`), the grade is 6.
- Otherwise (no conditions met), the grade is 5.
Step 6: The program returns 0, indicating successful execution.
The Body Mass Index (BMI) is defined as ratio of the weight of a person (in kilograms) to the square of the height (in meters). Write a program that receives weight and height, calculates the BMI, and reports the BMI category as per the following table:
BMI Status | Range |
---|---|
Starvation | <15 |
Anorexic | 15.1 to 17.5 |
Underweight | 17.6 to 18.5 |
Ideal | 18.6 to 24.9 |
Overweight | 25 to 25.9 |
Extreme Obesity | 30 to 39.9 |
Morbidly Obese | >=40 |
#include <stdio.h>
int main(){
float w, h, bmi;
printf("Enter your weight in kg and height in m: ");
scanf("%f%f", &w, &h);
bmi = w / (h * h);
(bmi > 0 && bmi <= 15) ? printf("BMI = %.1f and Status = Starvation", bmi) :
(bmi > 15 && bmi <= 17.5) ? printf("BMI = %.1f and Status = Anorexic", bmi) :
(bmi > 17.5 && bmi <= 18.5) ? printf("BMI = %.1f and Status = Underweight", bmi) :
(bmi > 18.5 && bmi <= 24.9) ? printf("BMI = %.1f and Status = Ideal", bmi) :
(bmi > 24.9 && bmi <= 29.9) ? printf("BMI = %.1f and Status = Overweight", bmi) :
(bmi > 29.9 && bmi <= 39.9) ? printf("BMI = %.1f and Status = Obese", bmi) :
(bmi > 39.9) ? printf("BMI = %.1f and Status = Morbidly Obese", bmi) :
printf("You have entered wrong values");
return 0;
}
Step 2: Prompt the user to enter their weight in kilograms and height in meters, and store these values in `w` and `h` using `scanf`.
Step 3: Calculate the BMI using the formula: `bmi = w / (h * h);`.
Step 4: Use a series of nested ternary operators to determine the BMI status and print the result. The conditions are checked sequentially:
- If BMI is between 0 and 15 (inclusive), print "BMI = %.1f and Status = Starvation".
- Else if BMI is between 15 and 17.5 (inclusive), print "BMI = %.1f and Status = Anorexic".
- Else if BMI is between 17.5 and 18.5 (inclusive), print "BMI = %.1f and Status = Underweight".
- Else if BMI is between 18.5 and 24.9 (inclusive), print "BMI = %.1f and Status = Ideal".
- Else if BMI is between 24.9 and 29.9 (inclusive), print "BMI = %.1f and Status = Overweight".
- Else if BMI is between 29.9 and 39.9 (inclusive), print "BMI = %.1f and Status = Obese".
- Else if BMI is greater than 39.9, print "BMI = %.1f and Status = Morbidly Obese".
- Otherwise (if none of the above conditions are met, which would likely mean invalid input), print "You have entered wrong values".
Using conditional operators determine whether the character entered through the keyboard is a lower case alphabet or not.
#include <stdio.h>
int main(){
char ch;
printf("Enter your character : ");
scanf("%c",&ch);
(ch>=97 && ch<= 122)?printf("Lowercase\n"):printf("Not Lowercase");
return 0;
}
Step 2: Prompt the user to enter a character and store it in the `ch` variable using `scanf`.
Step 3: Use a ternary operator to check if the entered character is a lowercase letter. The ASCII values for lowercase letters range from 97 ('a') to 122 ('z'). The condition `ch >= 97 && ch <= 122` checks if `ch` falls within this range.
Step 4: If the condition is true (the character is lowercase), print "Lowercase".
Step 5: Otherwise (if the condition is false), print "Not Lowercase".
Step 6: The program returns 0, indicating successful execution.
Using conditional operators determine whether the character entered through the keyboard is a upper case alphabet or not.
#include <stdio.h>
int main(){
char ch;
printf("Enter your character : ");
scanf("%c",&ch);
(ch>=65 && ch<= 90)?printf("Uppercase\n"):printf("Not Uppercase");
return 0;
}
Step 2: Prompt the user to enter a character and store it in the `ch` variable using `scanf`.
Step 3: Use a ternary operator to check if the entered character is an uppercase letter. The ASCII values for uppercase letters range from 65 ('A') to 90 ('Z'). The condition `ch >= 65 && ch <= 90` checks if `ch` falls within this range.
Step 4: If the condition is true (the character is uppercase), print "Uppercase".
Step 5: Otherwise (if the condition is false), print "Not Uppercase".
Step 6: The program returns 0, indicating successful execution.
Write a program using conditional operators to determine whether a year entered through the keyboard is a leap year or not.
#include <stdio.h>
int main(){
int year;
printf("Enter a year: ");
scanf("%d", &year);
((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))?
printf("It is a leap year\n"):printf("It is not a leap year\n");
return 0;
}
Step 2: Prompt the user to enter a year and store it in the `year` variable using `scanf`.
Step 3: Use a ternary operator to determine if the year is a leap year. The leap year condition is `(year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)`. This condition checks if the year is divisible by 400 (e.g., 2000), or if it's divisible by 4 but not by 100 (e.g., 2024, but not 1900).
Step 4: If the condition is true (it's a leap year), print "It is a leap year".
Step 5: Otherwise (if the condition is false), print "It is not a leap year".
Step 6: The program returns 0, indicating successful execution.
Write a program to find the greatest of the three numbers entered through the keyboard. Use conditional operators.
#include <stdio.h>
int main(){
int a,b,c;
printf("Enter 3 numbers : ");
scanf("%d%d%d",&a,&b,&c);
printf("The greatest number is %d",(a>b)?((a>c)?a:c):((b>c)?b:c));
return 0;
}
Step 2: Prompt the user to enter three numbers and store them in `a`, `b`, and `c` using `scanf`.
Step 3: Use nested ternary operators to find the greatest number:
- The outer ternary operator `(a > b) ? ... : ...` checks if `a` is greater than `b`.
- If `a` is greater than `b`, the inner ternary operator `(a > c) ? a : c` checks if `a` is also greater than `c`. If so, `a` is the greatest; otherwise, `c` is the greatest.
- If `a` is not greater than `b`, the second inner ternary operator `(b > c) ? b : c` checks if `b` is greater than `c`. If so, `b` is the greatest; otherwise, `c` is the greatest.
Step 5: The program returns 0, indicating successful execution.
Write a program to receive value of an angle in degrees and check whether sum of squares of sine and cosine of this angle is equal to 1.
#include <stdio.h>
#include <math.h>
int main(){
float deg;
printf("Enter the degree : ");
scanf("%f",°);
printf("The sum of squares of sine and cosine of given angle is %f",pow(sin(deg),2)+pow(cos(deg),2));
return 0;
}
Step 2: Declare a floating-point variable `deg` to store the angle in degrees.
Step 3: Prompt the user to enter the angle.
Step 4: Calculate the sum of the squares of the sine and cosine of the given angle.The `pow` function is used to calculate the square of the sine and cosine.
Step 5: Print the calculated sum using `printf`.
Step 6: The program returns 0, indicating successful execution.
Comments