1 C Lab Part
1 C Lab Part
C LANGUAGE
Introduction
C is a programming language developed at AT&T’s BELL Laboratory of
USA in 1972.
Dennis Ritchie designed it. Because of its reliability.
C is very popular.
C is highly portable & it is well suited for structured programming.
C program consists of collection of functions.
Turbo C/C++
Open Turbo C from your Desktop or Programs menu. Select “File” from
Menu bar and select option “New” and Save C program in filename .C
extension.
To do compiling – Select -> Compile from menu and click-> compile.
If the compilation is success – you will see a “success” message. Else you
will see the number of errors.
To RUN the program – you may select ->Run from menu and click -> Run
Now you will see the output screen.
1
Programming in C Laboratory Department of Computer Science and Engineering, SMVEC
AIM:
To write a C program to find the area of a triangle having 3 sides.
DESCRIPTION:
A method for calculating the area of a triangle having 3 sides using Heron’s formula
Let a,b,c be the length of the sides of a triangle. Then
Area = √ (s*(s-a)*(s-b)*(s-c))
Where s=(a+b+c)/2
ALGORITHM:
Step 1: start
Step 2: Declare the variable a, b, c, s, area
Step 3: Get the value of a, b, c
Step 4: Find s value s=(a+b+c)/2
Step 5: Find area = sqrt(s*(s-a)*(s-b)*(s-c))
Step 6: Print the value of area
Step 7: Stop
FLOW CHART:
START
Read a,b,c
s=(a+b+c)/2
area = sqrt(s*(s-a)*(s-b)*(s-c))
PRINT area
STOP 2
Programming in C Laboratory Department of Computer Science and Engineering, SMVEC
SOURCE CODE:
#in clude <stdio .h>
#in clude < ma th.h>
#in clud e <conio .h>
void ma in( )
{
float a ,b,c;
float s ,ar ea;
p rint f(" Ent er si ze of ea ch s ides o f t ri angle" );
s can f( " %f %f %f " ,& a,&b ,&c );
s = ( a+b+ c) /2;
ar ea = sq rt (s*(s -a )*(s -b)* (s - c) );
p rint f(" Ar e a of t ria ngle is: %.3f " ,a re a);
}
3
Programming in C Laboratory Department of Computer Science and Engineering, SMVEC
SCREEN SHOT:
RESULT:
Thus the given program to calculate area of a triangle has been executed
and verified successfully.
4
Programming in C Laboratory Department of Computer Science and Engineering, SMVEC
PRACTICE EXERCISE:
1.(b) To write a C program to find the area of a Cube.
Result
5
Programming in C Laboratory Department of Computer Science and Engineering, SMVEC
2.(a) Write a simple C program to find the total and Average obtained by a
student for 5 subjects.
AIM :
To write a C program to find the total and average obtained by a student for 5
subjects.
ALGORITHM:
Step 1 : Start
Step 2 : Declare the variable m1, m2, m3, m4, m5, total, avg
Step 3 : Get the values of m1, m2, m3, m4, m5
Step 4 : Calculate the total and average values
Step 5 : Print the total and average values
Step 6 : Stop
FLOW CHART:
START
Read m1,m2,m3,
m4, m5
tot=m1+m2+m3+m4+m5
avg=tot/5
STOP
6
Programming in C Laboratory Department of Computer Science and Engineering, SMVEC
SOURCE CODE:
#in clude <s tdio.h>
#in clude <conio .h>
void ma in( )
{
int m1 ,m2 ,m3 ,m4 ,m5 , tot;
f loat avg ;
c lrs cr ();
p rintf ("en te r th e ma r k o f M ATHS = ");
s can f(" %d" ,& m1 );
p rintf (" en te r th e ma r k o f S OE = " );
s can f(" %d" ,& m2 );
p rintf ("en te r th e ma r k o f P C= ") ;
s can f(" %d" ,& m3 );
p rintf ("en te r th e ma r k o f EC = " );
s can f(" %d" ,& m4 );
p rintf ("en te r th e ma r k o f B asic M & C = ");
s can f(" %d" ,& m5 );
tot = m1 + m2+ m3 +m4 + m5 ;
avg =tot/5 ;
p rintf (" \n\n TOTAL M ARKS= % d ",tot );
p rintf (" \n\n Ave rag e M arks = %.2 f",avg );
ge tch ();
}
7
Programming in C Laboratory Department of Computer Science and Engineering, SMVEC
SCREENSHOT:
RESULT:
Thus the given program to calculate the total and average percentage has
been executed and verified successfully.
8
Programming in C Laboratory Department of Computer Science and Engineering, SMVEC
PRACTICE EXERCISE:
9
Programming in C Laboratory Department of Computer Science and Engineering, SMVEC
Result
10