Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

WEEK 3

Objective: Learn how to define variables with the desired data-type, initialize them
with appropriate values and how arithmetic operators can be used with variables and
constants.
Suggested Experiments/Activities:
Tutorial 3: Variable types and type conversions:
Lab 3: Simple computational problems using arithmetic expressions.
i) Finding the square root of a given number
ii) Finding compound interest
iii) Area of a triangle using heron’s formulae
iv) Distance travelled by an object

i) Finding the square root of a given number


The square root of a number is defined as the value, which gives the number when
it is multiplied by itself i.e half power of the number. The radical symbol √ is used to
indicate the square root.
Examples:
If the number is 25, its square root is 5.
If the number is 10, its square root is 3.162.

SOURCE CODE:
#include <stdio.h>
#include <math.h>
int main()
{
int num;
float root;
printf("Enter a number: ");
scanf("%d", &num);
root = sqrt(num);
printf("The Square Root of %d is %.4lf", num, root);
return 0;
}
OUTPUT:

#include <stdio.h>
#include <math.h>
int main()
{
int num;
float root;
printf("Enter a number: ");
scanf("%d", &num);
root = pow(num,0.5);
printf("The Square Root of %d is %.4lf", num, root);
return 0;
}
OUTPUT:
ii) Finding compound interest
Compound interest is the addition of interest to the principal sum of a loan or
deposit, or in other words, interest on interest. It is the result of reinvesting interest,
rather than paying it out, so that interest in the next period is then earned on the
principal sum plus previously-accumulated interest. Compound interest is standard
in finance and economics. Compound interest may be contrasted with simple
interest, where interest is not added to the principal, so there is no compounding.
Compound Interest formula:
Formula to calculate compound interest annually is given by:
Amount= P(1 + R/100) t
Compound Interest = Amount – P
Where,
P is principal amount
R is the rate and
T is the time span

SOURCE CODE:
#include <stdio.h>
#include <math.h>
int main()
{
float principle,rate,time,amount,CI;
printf("Enter Principle Amount: ");
scanf("%f ", &principle);
printf("Enter time: ");
scanf("%f ", &time);
printf("Enter rate: ");
scanf("%f ", &rate);
amount = principle* (pow((1 + rate / 100), time));
CI = amount - principle;
printf("Compound Interest = %f ", CI);
return 0;
}

OUTPUT:
iii) Area of a triangle using heron’s formulae
Steps to find the area of a triangle using Heron's formula
Let A, B and C be the length of three sides of a triangle.
 Calculate the semi perimeter of the triangle.
Semi-Perimeter of triangle(S) = (A + B + C)/2
 Now, we can calculate the area of triangle using below mentioned formula.
Area of Triangle = √ S(S-A)(S-B)(S-C))
Where, S is the semi-perimeter that we calculated in first step.

Example
Let ABC be a triangle, whose length of sides are 5, 10 and 7 meters. To calculate the
area of this triangle first of all we should calculate it's semi-perimeter.
Semi-Perimeter(S) = (5+10+7)/2 = 11
Now, we can calculate area of triangle ABC using Heron's formula
Area = √ 11(11-5)(11-10)(11-7)) = √ 264 = 16.24 m2

SOURCE CODE:
#include<stdio.h>
#include<math.h>
int main()
{
float a,b,c,Perimeter,s,Area;
printf("Enter three sides of Triangle:");
scanf("%f %f %f",&a,&b,&c);
Perimeter = a+b+c;
s = (a+b+c)/2;
Area = sqrt(s*(s-a)*(s-b)*(s-c));
printf("Perimeter of Triangle = %f\n", Perimeter);
printf("Semi Perimeter of Triangle = %f\n",s);
printf("Area of Triangle = %f\n",Area);
return 0;
}

OUTPUT:
iv) Distance travelled by an object
The total distance travelled by vehicle in 't' seconds is given by distance =
ut+1/2at2 where 'u' and 'a' are the initial velocity (m/sec.) and acceleration
(m/sec2).
#include<stdio.h>
int main()
{
float u,a,t,v,s;
printf("Enter initial velocity: ");
scanf("%f",&u);
printf("\nEnter acceleration: ");
scanf("%f ",&a);
printf("\nEnter time: ");
scanf("%f ",&t);
v=u+a*t;
s=u*t+(1/2)*a*t*t;
printf("\nThe final Velocity is : %f ",v);
printf("\nThe distance travelled by the object is : %f ",s);
}

OUTPUT:

You might also like