Objective:
- Introduction to C
- How to compile C code
- 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;
}