0% found this document useful (0 votes)
5 views5 pages

C Programs Solutions

Uploaded by

keshavjhapranshu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

C Programs Solutions

Uploaded by

keshavjhapranshu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

Check if a character is a vowel or consonant

#include <stdio.h>

#include <ctype.h>

int main() {

char ch;

printf("Enter a character: ");

scanf("%c", &ch);

ch = tolower(ch);

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {

printf("%c is a vowel.\n", ch);

} else if ((ch >= 'a' && ch <= 'z')) {

printf("%c is a consonant.\n", ch);

} else {

printf("Invalid input. Please enter an alphabet character.\n");

return 0;

2. Check if a triangle is valid and determine its type

#include <stdio.h>

int main() {

float a, b, c;

printf("Enter the three sides of the triangle: ");


scanf("%f %f %f", &a, &b, &c);

if (a + b > c && b + c > a && c + a > b) {

printf("The triangle is valid.\n");

if (a == b && b == c) {

printf("It is an equilateral triangle.\n");

} else if (a == b || b == c || c == a) {

printf("It is an isosceles triangle.\n");

} else if ((a * a + b * b == c * c) || (b * b + c * c == a * a) || (c * c + a *

a == b * b)) {

printf("It is a right-angled triangle.\n");

} else {

printf("It is a scalene triangle.\n");

} else {

printf("The triangle is not valid.\n");

return 0;

3. Find the largest among three numbers

#include <stdio.h>

int main() {

float num1, num2, num3;

printf("Enter three numbers: ");

scanf("%f %f %f", &num1, &num2, &num3);


if (num1 >= num2 && num1 >= num3) {

printf("The largest number is %.2f\n", num1);

} else if (num2 >= num1 && num2 >= num3) {

printf("The largest number is %.2f\n", num2);

} else {

printf("The largest number is %.2f\n", num3);

return 0;

4. Calculate roots of a quadratic equation

#include <stdio.h>

#include <math.h>

int main() {

float a, b, c, discriminant, root1, root2, realPart, imaginaryPart;

printf("Enter coefficients a, b, and c: ");

scanf("%f %f %f", &a, &b, &c);

discriminant = b * b - 4 * a * c;

if (discriminant > 0) {

root1 = (-b + sqrt(discriminant)) / (2 * a);

root2 = (-b - sqrt(discriminant)) / (2 * a);

printf("Roots are real and different.\n");

printf("Root 1 = %.2f\nRoot 2 = %.2f\n", root1, root2);


} else if (discriminant == 0) {

root1 = -b / (2 * a);

printf("Roots are real and the same.\n");

printf("Root 1 = Root 2 = %.2f\n", root1);

} else {

realPart = -b / (2 * a);

imaginaryPart = sqrt(-discriminant) / (2 * a);

printf("Roots are complex.\n");

printf("Root 1 = %.2f + %.2fi\n", realPart, imaginaryPart);

printf("Root 2 = %.2f - %.2fi\n", realPart, imaginaryPart);

return 0;

5. Check if a year is a leap year

#include <stdio.h>

int main() {

int year;

printf("Enter a year: ");

scanf("%d", &year);

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {

printf("%d is a leap year.\n", year);

} else {

printf("%d is not a leap year.\n", year);

}
return 0;

6. Check if a character is an alphabet, digit, or special character

#include <stdio.h>

#include <ctype.h>

int main() {

char ch;

printf("Enter a character: ");

scanf("%c", &ch);

if (isalpha(ch)) {

printf("%c is an alphabet.\n", ch);

} else if (isdigit(ch)) {

printf("%c is a digit.\n", ch);

} else {

printf("%c is a special character.\n", ch);

return 0;

You might also like