CS Programming Record
CS Programming Record
B.E. [ ]
II – SEMESTER
Name :
Reg. No. :
ANNAMALAI UNIVERSITY
B.E. [ ]
II – SEMESTER
Reg. No.
of B.E. ( ) in the
ETSP207: Computer Programming Laboratory during the even semester of the
academic year 2022 – 2023.
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
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)
S. Page
Date List of Experiments Remarks Signature
No. No.
1. Arithmetic Operations 1
14 Structures 22
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 */
}
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);
}
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");
}
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:
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;
}
}
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();
6
Sample Input / Output:
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;
while ( i<n )
{
printf("%d ",even);
sum += even;
even +=2;
i++;
}
8
Sample Input / Output:
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]);
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:
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);
13
Sample Input / Output:
Enter 5 Integers
14 33 27 35 10
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;
15
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
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));
STRING OPERATIONS
String Length
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
Program:
#include <stdio.h>
int main()
{
int a = 10;
int b =
20;
printf("\n From Main \n After Swapping Values at Function Swap a = %d, b = %d\n",a,b);
}
18
Sample Input / Output:
From Main
Before Swapping the Values
a = 10, b = 20
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
Program:
int main()
{
int a = 10;
int b =
20;
printf("\n From Main \n After Swapping Values at Function Swap a = %d, b = %d\n", a, b);
}
temp = *ptr1;
*ptr1=*ptr2;
*ptr2=temp;
20
Sample Input / Output:
From Main
Before Swapping the Values
a = 10, b = 20
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);
}
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>
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);
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();
}
Person Details
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);
}
}
[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