Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

C Program Assignments

Uploaded by

tasmiul02
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

C Program Assignments

Uploaded by

tasmiul02
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

C Program Assignments Solutions

2nd Assignment: Quadratic Equation Roots

C program to find the roots of a quadratic equation ax^2 + bx + c = 0 using nested if-else

statements.

1. Input: Take coefficients a, b, c from the user.

2. Discriminant: Calculate discriminant as b^2 - 4ac.

3. Conditions:

a. If the discriminant is positive, there are two real and distinct roots.

b. If the discriminant is zero, the roots are real and equal.

c. If the discriminant is negative, the roots are complex.

4. Output: Print the roots based on the discriminant.

C Code:

#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 distinct: %.2f and %.2f\n", root1, root2);

} else if (discriminant == 0) {

root1 = root2 = -b / (2 * a);

printf("Roots are real and equal: %.2f and %.2f\n", root1, root2);

} else {

realPart = -b / (2 * a);

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

printf("Roots are complex: %.2f + %.2fi and %.2f - %.2fi\n", realPart, imaginaryPart, realPart,

imaginaryPart);

return 0;

3rd Assignment: Leap Year Checker

C program to check whether a given year is a leap year using nested if-else statements.

1. Input: The user enters a year.

2. Leap Year Conditions:

a. If the year is divisible by 4, it may be a leap year.

b. If the year is divisible by 100, check for divisibility by 400:

i. If divisible by 400, it is a leap year.

ii. Otherwise, it is not a leap year.

3. Output: The program prints whether the given year is a leap year.

C Code:
#include <stdio.h>

int main() {

int year;

printf("Enter a year: ");

scanf("%d", &year);

if (year % 4 == 0) {

if (year % 100 == 0) {

if (year % 400 == 0) {

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

} else {

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

} else {

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

} else {

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

return 0;

You might also like