Interactive C Programming Tutorial for B.Tech Students
Triangle Validity: For three sides a, b, c to form a valid triangle:
Triangle Types:
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c;
printf("Enter three sides of triangle: ");
scanf("%f %f %f", &a, &b, &c);
// Check validity
if (a + b > c && b + c > a && a + c > b) {
printf("Triangle is VALID\\n");
// Check type
if (a == b && b == c) {
printf("Triangle is EQUILATERAL\\n");
}
else if (a == b || b == c || a == c) {
printf("Triangle is ISOSCELES\\n");
}
else {
printf("Triangle is SCALENE\\n");
}
// Check for right angle
float max = (a > b) ? (a > c ? a : c) : (b > c ? b : c);
float sum_squares;
if (max == a) sum_squares = b*b + c*c;
else if (max == b) sum_squares = a*a + c*c;
else sum_squares = a*a + b*b;
if (fabs(max*max - sum_squares) < 0.001) {
printf("Triangle is also RIGHT-ANGLED\\n");
}
}
else {
printf("Triangle is INVALID\\n");
}
return 0;
}
BMI (Body Mass Index) is a measure of body fat based on height and weight.
| BMI Range | Category |
|---|---|
| < 15 | Starvation |
| 15.1 - 17.5 | Anorexic |
| 17.6 - 18.5 | Underweight |
| 18.6 - 24.9 | Ideal |
| 25.0 - 29.9 | Overweight |
| 30.0 - 39.9 | Obese |
| >= 40.0 | Morbidly Obese |
#include <stdio.h>
int main() {
float weight, height, bmi;
printf("Enter weight in kg: ");
scanf("%f", &weight);
printf("Enter height in meters: ");
scanf("%f", &height);
bmi = weight / (height * height);
printf("Your BMI is: %.2f\\n", bmi);
if (bmi < 15) {
printf("Category: Starvation\\n");
}
else if (bmi >= 15.1 && bmi <= 17.5) {
printf("Category: Anorexic\\n");
}
else if (bmi >= 17.6 && bmi <= 18.5) {
printf("Category: Underweight\\n");
}
else if (bmi >= 18.6 && bmi <= 24.9) {
printf("Category: Ideal\\n");
}
else if (bmi >= 25.0 && bmi <= 29.9) {
printf("Category: Overweight\\n");
}
else if (bmi >= 30.0 && bmi <= 39.9) {
printf("Category: Obese\\n");
}
else {
printf("Category: Morbidly Obese\\n");
}
return 0;
}
Collinear Points: Three points are collinear if they lie on the same straight line.
Mathematical Condition: Points (x₁,y₁), (x₂,y₂), (x₃,y₃) are collinear if:
#include <stdio.h>
#include <math.h>
int main() {
float x1, y1, x2, y2, x3, y3, area;
printf("Enter coordinates of first point (x1, y1): ");
scanf("%f %f", &x1, &y1);
printf("Enter coordinates of second point (x2, y2): ");
scanf("%f %f", &x2, &y2);
printf("Enter coordinates of third point (x3, y3): ");
scanf("%f %f", &x3, &y3);
// Calculate area of triangle formed by three points
area = fabs(x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2)) / 2.0;
printf("Area of triangle: %.2f\\n", area);
if (area == 0) {
printf("The three points are COLLINEAR\\n");
}
else {
printf("The three points are NOT COLLINEAR\\n");
}
return 0;
}
Problem: Given that January 1, 0001 was a Monday, find what day January 1st falls on for any given year.
Approach:
Leap Year Rule: Divisible by 4, except century years must be divisible by 400
#include <stdio.h>
int isLeapYear(int year) {
return ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0));
}
int main() {
int year, totalDays = 0, dayOfWeek;
char *days[] = {"Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday", "Sunday"};
printf("Enter a year: ");
scanf("%d", &year);
// Calculate total days from year 1 to (year-1)
for (int i = 1; i < year; i++) {
if (isLeapYear(i)) {
totalDays += 366;
} else {
totalDays += 365;
}
}
// Find day of week (0 = Monday, 1 = Tuesday, ..., 6 = Sunday)
dayOfWeek = totalDays % 7;
printf("January 1, %d falls on: %s\\n", year, days[dayOfWeek]);
return 0;
}
Ternary Operator: condition ? value_if_true : value_if_false
Rectangle Perimeter: P = 2 × (length + breadth)
Task: Compare perimeters of at least 3 rectangles using ternary operators.
#include <stdio.h>
int main() {
float l1, b1, l2, b2, l3, b3;
float p1, p2, p3;
printf("Enter length and breadth of Rectangle 1: ");
scanf("%f %f", &l1, &b1);
printf("Enter length and breadth of Rectangle 2: ");
scanf("%f %f", &l2, &b2);
printf("Enter length and breadth of Rectangle 3: ");
scanf("%f %f", &l3, &b3);
// Calculate perimeters
p1 = 2 * (l1 + b1);
p2 = 2 * (l2 + b2);
p3 = 2 * (l3 + b3);
printf("Perimeter 1: %.2f\\n", p1);
printf("Perimeter 2: %.2f\\n", p2);
printf("Perimeter 3: %.2f\\n", p3);
// Using ternary operator to find maximum
float max_perimeter = (p1 > p2) ?
((p1 > p3) ? p1 : p3) :
((p2 > p3) ? p2 : p3);
int max_rectangle = (p1 == max_perimeter) ? 1 :
(p2 == max_perimeter) ? 2 : 3;
printf("Rectangle %d has the highest perimeter: %.2f\\n",
max_rectangle, max_perimeter);
return 0;
}