0% found this document useful (0 votes)
15 views32 pages

CS Programming Record

This document is computer science program coding

Uploaded by

hemnathramesh4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views32 pages

CS Programming Record

This document is computer science program coding

Uploaded by

hemnathramesh4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 32

ANNAMALAI UNIVERSITY

FACULTY OF ENGINEERING AND TECHNOLOGY

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

B.E. [ ]

II – SEMESTER

ETSP207: Computer Programming Laboratory

Name :

Reg. No. :
ANNAMALAI UNIVERSITY

FACULTY OF ENGINEERING AND TECHNOLOGY

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

B.E. [ ]

II – SEMESTER

ETSP207 : Computer Programming Laboratory

Certified that this is a bona fide record of work done by


Mr./Ms.

Reg. No.
of B.E. ( ) in the
ETSP207: Computer Programming Laboratory during the even semester of the
academic year 2022 – 2023.

Staff in-charge Internal Examiner

Annamalai nagar External Examiner


Date: … /…. / 2023.
Vision-Mission of Faculty of Engineering and Technology

Vision
Providing world class quality education with strong ethical values to nurture and develop
outstanding professionals fit for globally competitive environment.

Mission

 Provide quality technical education with a sound footing on basic engineering principles,
technical and managerial skills, and innovative research capabilities.
 Transform the students into outstanding professionals and technocrats with strong ethical values
capable of creating, developing and managing global engineering enterprises.
 Develop a Global Knowledge Hub, striving continuously in pursuit of excellence in Education,
Research, Entrepreneurship and Technological services to the Industry and Society.
 Inculcate the importance and methodology of life-long learning to move forward with updated
knowledge to face the challenges of tomorrow.

Vision-Mission of the Department of Computer Science and Engineering

Vision
To provide a congenial ambience for individuals to develop and blossom as academically
superior, socially conscious and nationally responsible citizens.

Mission

 Impart high quality computer knowledge to the students through a dynamic scholastic
environment wherein they learn to develop technical, communication and leadership skills to
bloom as a versatile professional.
 Develop life-long learning ability that allows them to be adaptive and responsive to the changes
in career, society, technology, and environment.
 Build student community with high ethical standards to undertake innovative research and
development in thrust areas of national and international needs.
 Expose the students to the emerging technological advancements for meeting the demands of the
industry.
Program Educational Objectives (PEOs)

PEO PEO Statements


To prepare the graduates with the potential to get employed in the right role and/or become
PEO1
entrepreneurs to contribute to the society.
To provide the graduates with the requisite knowledge to pursue higher education and carry
PEO2
out research in the field of Computer Science.
To equip the graduates with the skills required to stay motivated and adapt to the dynamically
PEO3
changing world so as to remain successful in their career.
To train the graduates to communicate effectively, work collaboratively and exhibit high
PEO4
levels of professionalism and ethical responsibility.
Program Outcomes (POs)

S. NO. Program Outcomes


PO1 Engineering Knowledge: Apply the knowledge of mathematics, science, engineering
fundamentals, and an engineering specialization to the solution of complex engineering
problems.
PO2 Problem Analysis: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of mathematics,
natural sciences and engineering sciences.
PO3 Design/Development of Solutions: Design solutions for complex engineering problems and
design system components or processes that meet the specified needs with appropriate
consideration for the public health and safety, and the cultural, societal, and environmental
considerations.
PO4 Conduct Investigations of Complex Problems: Use research-based knowledge and
research methods including design of experiments, analysis and interpretation of data, and
synthesis of the information to provide valid conclusions.
PO5 Modern Tool Usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modeling to complex engineering activities
with an understanding of the limitations.
PO6 The Engineer and Society: Apply reasoning informed by the contextual knowledge to assess
societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to
the professional engineering practice.
PO7 Environment and Sustainability: Understand the impact of the professional
engineering solutions in societal and environmental contexts, and demonstrate the knowledge
of, and need for sustainable development.
PO8 Ethics: Apply ethical principles and commit to professional ethics and responsibilities and
norms of the engineering practice.
PO9 Individual and Team Work: Function effectively as an individual, and as a member or leader
in diverse teams, and in multidisciplinary settings.
PO10 Communication: Communicate effectively on complex engineering activities with the
engineering community and with society at large, such as, being able to comprehend and write
effective reports and design documentation, make effective presentations, and give and receive
clear instructions.
PO11 Project Management and Finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one’s own work, as a member and
leader in a team, to manage projects and in multidisciplinary environments.
PO12 Life-long Learning: Recognize the need for, and have the preparation and ability to engage in
independent and lifelong learning in the broadest context of technological change.
Program Specific Outcomes (PSOs)

S. No. Program Specific Outcomes


PSO1 Acquire the ability to understand basic sciences, humanity sciences, basic engineering
sciences and fundamental core courses in Computer Science and Engineering to realize
and appreciate real life problems in diverse fields for proficient design of computer-
based systems of varying complexity.
PSO2 Learn specialized courses in Computer Science and Engineering to build up the aptitude
for applying typical practices and approaches to deliver quality products intended for
business and industry requirements.
PSO3 Apply technical and programming skills in Computer Science and Engineering essential
for employing current techniques in software development crucial in industries, to create
pioneering career paths for pursuing higher studies, research and to be an entrepreneur.

Rubric for CO3 in Laboratory Courses


Distribution of 10 Marks for CIE/SEE Evaluation Out of 40/60 Marks
Rubric
UpTo2.5 Marks Up To 5 Marks Up To 7.5 Marks Up To 10 Marks
Demonstrate Poor listening Showed better Demonstrated Demonstrated
an ability to and communication good excellent
Listen and communication Skill by relating Communication communication
answer the skills. Failed to The problem skills by relating skills by relating
viva questions relate the with The the problem with the problem with
related to programming programming the programming the programming
programming Skills needed for skills acquired skills acquired skills acquired
Skills needed Solving the but the with few errors. and have been
for solving problem. description successful in
real-world showed serious tailoring the
problems in errors. description.
Computer
Science and
Engineering.
Contents

S. Page
Date List of Experiments Remarks Signature
No. No.
1. Arithmetic Operations 1

2. Calculate Percentage of Marks in Five Subjects 2

3. Odd or Even using if-else Statement 3


Convert any number from 0 to 9 into word using
4. 4
switch…case
5. Sum of Natural Numbers using for Statement 6

6. Sum of Even Numbers using while Looping Statement 8

7. Searching an Element in the List of Numbers 10

8. Sorting the List of Numbers using Bubble Sort 12

9. Matrix Addition using 2-D array 14

10 String Length with and without using Library Functions 16

11 Swapping of Numbers using Function Call by Value 17

12 Swapping of Numbers using Function Call by Reference 19

13 Factorial of a Number using Recursive Functions 21

14 Structures 22

15 Creation of a Simple Text File 24


Ex. No. : 01 Arithmetic Operations
Date :

Aim:
To perform the arithmetic operations using a C program.

Algorithm:

1. Start
2. Get the data assigned to the respective variables.
3. Add the numbers and print the result
4. Multiply the numbers and print the result
5. Divide the numbers and print the result
6. Get the remainder and print the result
7. Pre Increment a and print the result
8. Post Increment b and print the result
9. Stop

Program:

#include <stdio.h>
int main()
{
int a = 21; int b = 10; int c
;
c = a + b; printf(" Value of c = a+b is %d\n", c );
c = a - b; printf(" Value of c = a-b is %d\n", c );
c = a * b; printf(" Value of c = a*b is %d\n", c );
c = a / b; printf(" Value of c = a/b is %d\n", c );
c = a % b; printf(" Value of c = a%%b is %d\n", c );
c = ++a; printf(" Value of c = ++a is %d\n", c ); /* pre-incrementation */
c = b++; printf(" Value of c = b++ is %d\n", c ); /* post-incrementation */
}

Sample Input / Output:

Value of c = a+b is 31
Value of c = a-b is 11
Value of c = a*b is 210
Value of c = a/b is 2
Value of c = a%b is 1
Value of c = ++a is 22
Value of c = b++is 10

Result:
Thus, the C program for arithmetic operations has been successfully executed.

1
Ex. No. 02 Calculate Percentage of Marks in Five Subjects
Date:

Aim:
To calculate percentage of marks obtained in five subjects using a C program.

Algorithm:

1. Start
2. Assign total as 500
3. Get the marks obtained for 5 subjects
4. Add the marks of 5 subjects and get the sum
5. Calculate percentage = (sum / total ) * 100
6. Print the percentage
7. Stop

Program:

#include<stdio.h>
int main()
{
int s1,s2,s3,s4,s5,sum=0,total=500;
float percent;
printf("Enter Marks of 5 Subjects: \n");
scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5);
sum=s1+s2+s3+s4+s5;
printf("\n Sum of 5 Subjects is %d",sum);
percent=(float)sum/(float)total * 100;
printf("\n Percentage is %f", percent);
}

Sample Input / Output:

Enter Marks of 5 Subjects:


98
78
67
95
83
Sum of 5 subjects is 421
Percentage is 84.200000

Result:
Thus, the C program for calculating percentage of marks obtained in five subjects has
been successfully executed.

2
Ex. No. 03 Odd or Even using if-else Statement
Date:

Aim:
To find whether a number is odd or even using if-else statement in C language

Algorithm:

1. Start
2. Input number
3. Remainder = number%2
4. if(Remainder=0) then print “Number is even”
otherwise print “Number is odd”
5. Stop

Program:

#include<stdio.h>
int main()
{
int n;
printf("\n Enter a value to find odd or even:");
scanf("%d",&n);
if(n%2==0)
printf("\n Number is even");
else
printf("\n Number is odd");
}

Sample Input / Output:

Enter a value to find odd or even: 5


Number is odd

Enter a value to find odd or even: 8


Number is even

Result:
Thus, the C program for finding odd or even number has been successfully executed.

3
Ex. No. : 04 Convert any number from 0 to 9 into word using switch … case
Date :

Aim:
To convert any number from 0 to 9 into word using switch … case statement in C
language.

Algorithm:

1. Get the value for n from the user.


2. Check the entered value of n with the cases listed below. If the value of n matches
with the case value, then execute corresponding statement only,
a. n=0 : display “Entered number is Zero”
b. n=1 : display “Entered number is One”
c. n=2 : display “Entered number is Two”
d. n=3 : display “Entered number is Three”
e. n=4 : display “Entered number is Four”
f. n=5 : display “Entered number is Five”
g. n=6 : display “Entered number is Six”
h. n=7 : display “Entered number is Seven”
i. n=8 : display “Entered number is Eight”
j. n=9 : display “Entered number is Nine”
otherwise display “Enter the number only between 0 and 9”.
3. Stop

Program:

#include<stdio.h>
int main()
{
int num;
printf("\n\nEnter a number from 0 to 9 : ");
scanf("%d",&num);
switch(num)
{
case 0:
printf("\nEntered number is Zero \n");
break;
case 1:
printf("\nEntered number is One \n");
break;
case 2:
printf("\nEntered number is Two \n");
break;
case 3:
printf("\nEntered number is Three \n");
break;
case 4:

4
printf("\nEntered number is Four \n");
break;
case 5:
printf("\nEntered number is Five \n");
break;
case 6:
printf("\nEntered number is Six \n");
break;
case 7:
printf("\nEntered number is Seven \n");
break;
case 8:
printf("\nEntered number is Eight \n");
break;
case 9:
printf("\nEntered number is nine \n");
break;
default:
printf("\n Enter the number only from 0 to 9 \n");
break;
}
}

Sample Input / Output:

Enter a number from 0 to 9 : 0


Entered number is Zero

Enter a number from 0 to 9 : 1


Entered number is One

Enter a number from 0 to 9 : 9


Entered number is Nine

Enter a number from 0 to 9 :10


Enter the number only from 0 to 9

Result:
Thus, the C program to convert the given number into word using switch … case
statement is executed successfully.

5
Ex. No. : 05 Sum of Natural Numbers using for Statement
Date :

Aim:

To compute the sum of first n natural numbers using for looping statement in C
language

Algorithm:

1. Start
2. Get the value of n.
3. Assign the initial value 0 to sum.
4. Assign the first natural number 1 to the variable i.
5. Repeat the following steps for n number of times
a. Accumulate the present value of i along with the present value of sum in
the variable sum. [i.e. sum = sum + i.]
b. Increment the value of i by one. [i.e. i = i + 1]
6. Display the result available in the variable sum.
7. Stop

Program:

#include<stdio.h>
#include<conio.h>
int main()
{
int n, i, sum =
0; clrscr();

printf("Enter a Positive Integer Number: ");


scanf("%d", &n);
printf("\nThe Sum of the Series ");

for(i=1; i<= n; i++)


{
printf("%d + ",i);
sum += i;
}

printf("\nSum = %d", sum);


}

6
Sample Input / Output:

Enter a Positive Integer Number: 5

The Sum of the Series 1 + 2 + 3 + 4 +5 +


Sum = 15

Enter a Positive Integer Number: 1

The Sum of the Series 1 +


Sum = 1

Result:
Thus, the C program to find the sum of first n natural number using for looping
statement is executed successfully.

7
Ex. No. 06 Sum of Even Numbers using while Looping Statement
Date:

Aim:
To compute the sum of first n even numbers using while looping statement in C
language.

Algorithm:

1. Start
2. Get the value of n.
3. Assign the initial value 0 to sum.
4. Assign the first Even number 2 to the variable even.
5. Repeat the following steps for n number of times
a. Accumulate the present value of even along with the present value of sum
in the variable sum. [i.e. sum=sum +even.]
b. Increment the value of even by two. [i.e. even = even + 2]
6. Display the result available in the variable sum.
7. Stop

Program:

#include <stdio.h>

int main()
{
int n, i, sum = 0,even=2;

printf("Enter a Positive Integer Number: ");


scanf("%d", &n);
i = 0;
printf("\nThe Even Number(s) are ");

while ( i<n )
{
printf("%d ",even);
sum += even;
even +=2;
i++;
}

printf("\nSum of above Even Number Series = %d", sum);


}

8
Sample Input / Output:

Enter a Positive Integer Number: 5

The Even Number(s) are 2 4 6 8 10


Sum of above Even Number Series = 30

Enter a Positive Integer Number: 1

The Even Number(s) are 2


Sum of above Even Number Series = 2

Enter a Positive Integer Number: 0

The Even Number(s) are


Sum of above Even Number Series = 0

Result:
Thus, the C program to find the sum of first n even numbers using while looping
statement is executed successfully.

9
Ex. No. : 07 Searching an Element in the List of Numbers
Date :

Aim:
To search an element in the list using one dimensional array in C language

Algorithm:

1. Start
2. Get the number of elements in the list in variable n.
3. Assign the list of n numbers in the array.
4. Get the element e to be searched in the list.
5. Compare the element e with each element present in the array.
a. If it is present then display “Element Present” along with its location value and
go to step 7.
6. display “Element not found in the list”
7. Stop

Program:

#include<stdio.h>

int main()
{
int a[99], i, search, n;
int pos=-1;
printf("Enter the number of elements in the list : ");
scanf("%d", &n);
printf("Enter the List of Elements : ");
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);

printf("Enter the value of element to be searched : ");


scanf("%d", &search);

for(i=0;i<n;i++)
{
if(a[i]==search)
{
pos=i;
break;
}

if(pos==-1)
{
printf("the value is not found in the list");
}
else
10
{
printf("the element is present at the Location : %d", pos+1);

11
Sample Input / Output:

Enter Number of Elements in the List : 7

Enter the List of Elements :


54 23 45 19 1 49 46

Enter the Value of Element to be Searched : 45

The Element is Present at the Location 3

Enter Number of Elements in the List : 8

Enter the List of Elements :


54 23 45 19 1 49 46 22

Enter the Value of Element to be Searched : 145

Element Not found in the List

Result:
Thus, the C program to search an element in the list using one dimensional array is
executed successfully.

12
Ex. No. 08 Sorting the List of Numbers using Bubble Sort
Date:

Aim:
To sort the list of numbers in ascending order using Bubble sort algorithm.

Algorithm:

1. Start
2. Get the number of elements in the list in variable n.
3. Get the values of n elements in the array.
4. If the current element is greater than the next element of the array then interchange its
positions.
5. Repeat the Step 4 for n-1 number of times.
6. Repeat Step 5 for (n-1-number of times step 5 executed) number of times.
7. Display the resultant array which is in ascending order.
8. Stop

Program:

#include <stdio.h>
int main()
{
int array[100], n, i, j, temp;
printf("\nEnter the Number of Elements in the list to be Sorted:");
scanf("%d", &n);

printf("\nEnter %d Integers\n\n", n);

for (i = 0; i < n; i++)


scanf("%d", &array[i]);

for (i = 0 ; i < n - 1; i++)


{
for (j = 0 ; j < n - i - 1; j++)
{
if (array[j] > array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}

printf("\nSorted list of Numbers in ascending order … \n");


for (i = 0; i < n; i++)
printf(" %d ", array[i]);
}

13
Sample Input / Output:

Enter the Number of Elements in the list to be sorted: 5

Enter 5 Integers

14 33 27 35 10

Sorted list of Numbers in ascending order …


10 14 27 33 35

Result:
Thus, the C program to arrange the list of numbers in ascending order using Bubble
sort algorithm is executed successfully.

14
Ex. No. 09 Matrix Addition using 2-D array
Date:

Aim:
To perform addition of two matrixes using two dimensional arrays in C language

Algorithm:

1. Start
2. Get the dimensions of the matrix A and B in row and col.
3. Get the elements of the matrices A
4. Get the elements of the matrices B
5. Perform addition of each value present in Matrix A at the location (i, j) with
corresponding locations of Matrix B and store it in Matrix C.
That is C[i, j] = A[i, j] + B[i, j] for all i, j such that 0 ≤ i < row and 0 ≤ j < col.
6. Display the resultant matrix C in the matrix format.
7. Stop

Program:

#include <stdio.h>
int main()
{
int row, col, a[10][10], b[10][10], c[10][10], i, j;

printf("Enter the Dimension of the Matrix\n"); printf("\


nEnter the number of rows (between 1 and10): ");
scanf("%d",&row);
printf("\nEnter the number of columns (between 1and 10): ");
scanf("%d",&col);

printf("\nEnter the Elements of 1st Matrix in Matrix Format : \n");


for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
scanf("%d",&a[i][j]);
}
}

printf("\nEnter the Elements of 2nd matrix in Matrix Format : \n");


for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
scanf("%d", &b[i][j]);
}
}

15
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}

printf("\n Addition of two Matrices is: \n\n");


for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d ",c[i][j]);
}
printf("\n");
}

Sample Input / Output:

Enter the Dimension of the Matrix

Enter the number of rows (between 1 and 10) : 3

Enter the number of columns (between 1 and 10) : 3

Enter the Elements of the 1st Matrix in Matrix Format :


123
456
781

Enter the Elements of the 1st Matrix in Matrix Format :


763
321
205

Addition of two Matrices is :

886
777
986

Result:
Thus, the C program to perform matrix addition using two dimensional arrays is
executed successfully.

16
Ex. No. 10 String Length with and without using Library Functions
Date:

Aim:
To find length of the given string using and without using library function in C
language

Algorithm:
1. Start
2. Get the string in the variable str.
3. Call strlen( ) to find the length of the string and display it.
4. Start counting from first character to last character.
5. Display the count as length.
6. Stop

Program:
#include <stdio.h>
#include<string.h>
int main()
{
char str[100];
int i;
int length = 0;

fflush(stdin);
printf("\nSTRING OPERATIONS \n");
printf("\nString Length \n"); printf("\
nEnter a string: "); scanf("%s",str);
printf("\n\nLength of the string (using Library Function): %d", strlen(str));

for(i = 0; str[i] != '\0'; ++i);


length=i;
printf("\n\nLength of the string (Without using Library function) : %d", length);
}

Sample Input / Output:

STRING OPERATIONS

String Length

Enter a string: Kanthimathinathan

Length of the string (using Library Function): 17


Length of the string (Without using Library function): 17

Result:
Thus, the C program to find the length of the given string using and without using
library function is executed successfully.

17
Ex. No. 11 Swapping of Numbers using Function Call by Value
Date:

Aim:
To swap the contents available in two variables using function call by value.
Algorithm:
1. Start
2. Assign two different values to two integer variables let a =10 and b = 20.
3. Display the present value of a and b.
4. Call the Function swap by passing the values of a and b
5. Display the present value of a and b.
6. Stop

Function swap( v1, v2)


1. Display the present value of v1 and v2.
2. Using temp variable interchange the contents of the variable v1 and v2.
3. Display the present value of v1 and v2.
4. Return to the called place.

Program:

#include <stdio.h>

void swap(int , int);

int main()
{
int a = 10;
int b =
20;

printf("From Main \n Before Swapping the Values \n a = %d, b = %d\n",a,b);


swap(a,b);

printf("\n From Main \n After Swapping Values at Function Swap a = %d, b = %d\n",a,b);
}

void swap (int v1, int v2)


{
int temp;
printf("\nInside Function Swap");
temp = v1;
v1=v2;
v2=temp;
printf("\nAfter Swapping Values inside Function Swap \n v1 = %d, v2 = %d\n",v1,v2);
printf("Exiting from Function Swap\n");
}

18
Sample Input / Output:

From Main
Before Swapping the Values
a = 10, b = 20

Inside Function Swap


After Swapping Values inside Function Swap
v1 = 20, v2 = 10
Exiting from Function Swap

From Main
After Swapping Values at Function Swap
a= 10, b = 20

Result:
Thus, the C program to swap the contents available in two variables using function
call by value is executed successfully.

19
Ex. No. 12 Swapping of Numbers using Function Call by Reference
Date:

Aim:
To swap the contents available in two variables using function call by reference.

Algorithm:

1. Start
2. Assign two different values to two integer variables a =10 and b = 20.
3. Display the present value of a and b.
4. Call the Function swap by passing the address of the variable a and b
5. Display the present value of a and b.
6. Stop

Function swap( ptr1, ptr2)


1. Display the present value of ptr1 and ptr2.
2. Using temp variable interchange the contents of the pointer variable ptr1 and ptr2.
3. Display the present value of ptr1 and ptr2.
4. Return to called place.

Program:

#include <stdio.h> >


void swap(int *, int *);

int main()
{
int a = 10;
int b =
20;

printf("From Main \n Before Swapping the Values \n a = %d, b = %d\n",a,b);


swap(&a,&b);

printf("\n From Main \n After Swapping Values at Function Swap a = %d, b = %d\n", a, b);
}

void swap (int *ptr1, int *ptr2)


{
int temp;
printf("\nInside Function Swap");

temp = *ptr1;
*ptr1=*ptr2;
*ptr2=temp;

printf("\nAfter swapping values inside Function Swap");


printf("\n *ptr1 = %d, *ptr2 = %d\n",*ptr1,*ptr2);
printf("Exiting from Function Swap\n");

20
Sample Input / Output:

From Main
Before Swapping the Values
a = 10, b = 20

Inside Function Swap


After Swapping Values inside Function Swap
*ptr1 = 20, *ptr2 = 10
Exiting from Function Swap

From Main
After Swapping Values at Function Swap
a= 20, b = 10

Result:
Thus, the C program to swap the contents available in two variables using function
call by reference is executed successfully.

21
Ex. No. : 13 Factorial of a Number using Recursive Functions
Date :

Aim:
To find the factorial of a given number using recursive function in C language

Algorithm:
1. Start
2. Get the integer value for the variable n.
3. Call the function multiply(n) and assign the return value to the integer variable f .
4. Display the factorial of the entered value from the variable f.
5. Stop

Function factorial(n)
1. If (n>=1) then return (n *factorial(n-1))
else return the value 1to called place

Program:
#include<stdio.h>
long int factorial(int n);
int main()
{
int n;
long int f;
printf("Enter a positive integer: ");
scanf("%d",&n);
f=factorial(n);
printf("Factorial of %d = %ld", n, f);
}

long int factorial(int n)


{
if (n>=1)
return n*factorial(n-1);
else
return (1);
}

Sample Input / Output:

Enter a positive integer: 5


Factorial of 5 is 120

Enter a positive integer: 6


Factorial of 5 is 720

Result:
Thus, the C program to find the factorial of a given number using recursive function is
executed successfully.

22
Ex. No. 14 Structures
Date:

Aim:
To calculate the Body Mass Index (BMI) of a person from the details stored in
structure data type in C language

Algorithm:

1. Start
2. Create a structure person with the following members
name[30] of character data type
height and weight of float data type
bmi of double data type
3. Create array of structure p[10] of person data type.
4. Get the number of persons in the variable n.
5. Get the details of n persons in the structure array p[i] for all i such that 0 < = i < n.
6. Calculate the bmi of each person by using the formula bmi = weight/height2 and store
it in the corresponding bmi
7. Display the all the details of each person.
8. Stop

Program:
#include<stdio.h>
#include<conio.h>

int Extern _floatconvert;


#pragma extref _floatconvert

struct person
{
char name[30];
float height;
float weight;
double bmi;
};

void main()
{
int i,n;
struct person p[5];
printf("Enter the number of persons detail to be entered : ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("Enter the name of the Person : ");
scanf("%s", p[i].name);

printf("Enter the height of the Person(in meter) : ");


scanf("%f", &p[i].height);

printf("Enter the weight of the Person(in Kgs) : ");

23
scanf("%f", &p[i].weight);

p[i].bmi=p[i].weight/(p[i].height*p[i].height);
}
printf("\nPerson Details\n");
printf("Name \t\t\t\t Height \t weight \t BMI\n");
for(i=0;i<n;i++)
{
printf("%-30s \t %.2f \t\t %.2f \t\t%.2f \n", p[i].name, p[i].height, p[i].weight, p[i].bmi);
}
getch();
}

Sample Input / Output:

Enter the number of persons detail to be entered : 2

Enter the name of the Person: Kanthimathinathan


Enter the height of the Person (in meter) : 1.73
Enter the weight of the Person(in Kgs) : 66

Enter the name of the Person: Ramachandran


Enter the height of the Person (in meter) : 1.69
Enter the weight of the Person(in Kgs) : 67

Person Details

Name Height Weight BMI


Kanthimathinathan 1.73 66.00 22.05
Ramachandran 1.69 67.00 23.46

Result:
Thus, the C program to calculate the Body Mass Index (BMI) of a person from the
details stored in structure data type is executed successfully.

24
Ex. No. : 15 Creation of a Simple Text File
Date :

Aim:
To create a simple text file with contents in C Language.
Algorithm:
1. Start
2. Create a file and open it in writing mode with the file pointer fp.
3. If the file pointer fp is equal to NULL then display “Permission Denied” and go to
Step 6.
4. Copy the content to the file through pointer fp.
5. Close the file.
6. Stop
Program:
#include <stdio.h>
#include<conio.h>
int main()
{
FILE *fp = fopen("new1.txt", "w");
clrscr();
if(fp==NULL)
{
printf(“\n Permission Denied to Create File \n”);
getch();
// exit(0);
}
else
{
fprintf(fp, "Hello Students.\nHave a NICE DAY ");
printf(“\n File Created Successfully”);
getch();
fclose(fp);
}
}

Sample Input / Output:

File Created Successfully

[Note: After successful execution of this program, go to the dos prompt and execute the
below command to display the contents of the file created]

D:\>type new1.txt
Hello Students.
Have a NICE DAY

Result:
Thus, the C program to create a simple text file is executed successfully.

25

You might also like