C Exercise 1

Mannan Ul Haq
0

Objective:

  1. Introduction to C
  2. How to compile C code
  3. Simple arithmetic problems solving


Activity 1:

Take two inputs from user (keyboard) length and breadth of rectangle. Compute the area of rectangle given by: area = length*breadth


Solution:

#include <stdio.h>

int main()
{
    int length = 0;
    int breadth = 0;
    int area = 0;

    printf("Enter Length and Breadth of Rectangle: ");
    scanf("%d %d", &length, &breadth);

    area = length * breadth;

    printf("The Area is = %d", area);

    return 0;
}


Activity 2:

Write C code to solve the second-degree equation aX^2 + bX + c= 0 for any real numbers a, b and c. The program takes the values of a, b and c as input from the user, and calculates X.

Note: real numbers are taken in float.

Solution:

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

int main()
{
    float a = 0;
    float b = 0;
    float c = 0;
    float X = 0;

    printf("Enter three Real Numbers a, b, and c: ");
    scanf("%f %f %f", &a, &b, &c);

    float eq1;
    float sq;
    float eq2;
    float eq3;

    eq1 = (b * b) - (4 * a * c);
    sq = sqrtf(eq1);

    eq2 = (-b - sq) / (2 * a);
    printf("The First value of X is = %.2f\n", eq2);

    eq3 = (-b + sq) / (2 * a);
    printf("The Second value of X is = %.2f\n", eq3);

    return 0;
}


Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !