Q1. WAP to input the 3 sides of a triangle & print its corresponding type.
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three sides of the triangle: ");
scanf("%d %d %d", &a, &b, &c);
if(a+b>c && a+c>b && b+c>a) {
if(a==b && b==c)
printf("Equilateral Triangle\n");
else if(a==b || b==c || a==c)
printf("Isosceles Triangle\n");
else
printf("Scalene Triangle\n");
} else {
printf("Not a valid triangle\n");
}
return 0;
}
Q2. WAP to calculate the area of a triangle, circle, square or rectangle based on user’s
choice.
#include <stdio.h>
#include <math.h>
int main() {
int choice;
float area, a, b, r;
printf("1. Triangle\n2. Circle\n3. Square\n4. Rectangle\nEnter choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Enter base and height: ");
scanf("%f %f", &a, &b);
area = 0.5 * a * b;
break;
case 2:
printf("Enter radius: ");
scanf("%f", &r);
area = M_PI * r * r;
break;
case 3:
printf("Enter side: ");
scanf("%f", &a);
area = a * a;
break;
case 4:
printf("Enter length and breadth: ");
scanf("%f %f", &a, &b);
area = a * b;
break;
default:
printf("Invalid choice");
return 0;
}
printf("Area = %.2f\n", area);
return 0;
}
Q3. WAP to input a number & print its corresponding table.
#include <stdio.h>
int main() {
int n, i;
printf("Enter a number: ");
scanf("%d", &n);
for(i=1; i<=10; i++) {
printf("%d x %d = %d\n", n, i, n*i);
}
return 0;
}
Q5. Write a C program to calculate generic root of a number.
#include <stdio.h>
int main() {
int n, sum;
printf("Enter a number: ");
scanf("%d", &n);
while(n >= 10) {
sum = 0;
while(n > 0) {
sum += n % 10;
n /= 10;
}
n = sum;
}
printf("Generic root = %d\n", n);
return 0;
}
Q8. Write a function to determine whether a year is a leap year or not.
#include <stdio.h>
int isLeap(int year) {
if((year%400==0) || (year%4==0 && year%100!=0))
return 1;
else
return 0;
}
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if(isLeap(year))
printf("%d is a Leap Year\n", year);
else
printf("%d is not a Leap Year\n", year);
return 0;
}