C Programming Lab Manual – 36

Write a C program to calculate a Quadratic equation.

Function Main
    Declare Integer A
    Declare Integer B
    Declare Integer C
    Declare Integer Delta
    Declare Real X1
    Declare Real X2
    Output "Enter coefficient of X^2:"
    Input A
    Output "Enter coefficient of X:"
    Input B
    Output "Enter known term:"
    Input C
    If A = 0
        Output "Equarion:"
        Assign X1 = -C / B
        Output "The solution is X = " & X1
    Else
        Assign Delta = B^2 - 4*A*C
        If Delta < 0
            Output "There are no real solutions"
        Else
            If Delta = 0
                Assign X1 = -B / (2*A)
                Output "Two real and coincident solutions"
                Output "X1 = " & X1
                Output "X2 = " & X1
            Else
                Assign X1 = -B - SQRT(Delta) / (2*A)
                Assign X2 = -B + SQRT(Delta) / (2*A)
                Output "Two real and distinct solutions"
                Output "X1 = " & X1
                Output "X2 = " & X2
            End
        End
    End
End

#include <stdio.h>
#include <math.h>

int main() {
    int A;
    int B;
    int C;
    int Delta;
    double X1;
    double X2;
    printf("Enter coefficient of X^2:\n");
    scanf("%d", &A);
    printf("Enter coefficient of X:\n");
    scanf("%d", &B);
    printf("Enter known term:\n");
    scanf("%d", &C);
    if (A== 0) {
        printf("Equation:\n");
        X1 = -C / (double)B;
        printf("The solution is X = %lf\n", X1);
    } else {
        Delta = (int)(pow(B, 2) - 4 * A * C);
        if (Delta < 0) {
            printf("There are no real solutions\n");
        } else {
            if (Delta == 0) {
                X1 = -B / (2 * (double)A);
                printf("Two real and coincident solutions\n");
                printf("X1 = %lf\n", X1);
                printf("X2 = %lf\n", X1);
            } else {
                X1 = (-B - sqrt(Delta)) / (2 * (double)A);
                X2 = (-B + sqrt(Delta)) / (2 * (double)A);
                printf("Two real and distinct solutions\n");
                printf("X1 = %lf\n", X1);
                printf("X2 = %lf\n", X2);
            }
        }
    }
    return 0;
}

Enter coefficient of X^2:
5
Enter coefficient of X:
10
Enter known term:
5
Two real and coincident solutions
X1 = -1.000000
X2 = -1.000000

C Programming Lab Manual – 35

Write a C program to convert a given number of days into years, weeks, and days.

Function Main
    Declare Integer y
    Declare Integer w
    Declare Integer d
    Declare Integer a
    Output "Enter total number of days:"
    Input d
    Assign y = d / 365
    Assign a = d MOD 365
    Assign w = a / 7
    Assign d = a MOD 7
    Output "Years:" & ToString(y) & ToChar(10) & ToChar(13) & "Weeks: " & ToString(w) & ToChar(10) & ToChar(13) & "Days: " & ToString(d)
End

#include <stdio.h>

int main() {
    int y;
    int w;
    int d;
    int a;
    printf("Enter total number of days:\n");
    scanf("%d", &d);
    y = (int) ((double) d / 365);
    a = d % 365;
    w = (int) ((double) a / 7);
    d = a % 7;
    printf("Years: %d\nWeeks: %d\nDays: %d\n", y, w, d);
    return 0;
}

Enter total number of days:
400
Years: 1                                                                        
Weeks: 5                                                                        
Days: 0