T.J.
S ENGINEERING COLLEGE
(Approved by AICTE &Affiliated to Anna University, Chennai)
Peruvoyal, (Near Kavarapettai),
Gummidipoondi Taluk,
Thiruvallur District-601206
NAME : ………………………………………………….
REG NO : …………………………………………….
DEPARTMENT : ..……………………………………………….
SUBJECT CODE : ………………………………………………….
YEAR/SEM : …………………………………………………..
T.J.S ENGINEERING COLLEGE
(Approved by AICTE &Affiliated to Anna University, Chennai)
Peruvoyal, (Near Kavaraipettai),
Gummidipoondi Taluk,
Thiruvallur District-601206.
NAME : …………………………………………
DEPARTMENT : …………………………………………
SUBJECT CODE /TITLE : ……………………………………….
YEAR/SEM : …………………………………………
DATE OF EXAMINATION : …………………………………………
REGISTER NUMBER :
Certified that this is the Bonafide record of practical work done by the aforesaid student in the
-------------------------------------------------------------------------------------- during the year _______________
Laboratory in charge Head of the Department
Internal Examiner External Examiner
INDEX
PAGE
S.NO DATE NAME OF THE EXPERIMENTS MARK SIGN
NO
PAGE
S.NO DATE NAME OF THE EXPERIMENTS MARK SIGN
NO
Ex.No. 1 Programs using I/O statements and expressions
Aim
Algorithm
Source Code
#include<stdio.h>
int main()
{
int rad;
float PI = 3.14, area, ci;
printf("\nEnter radius of circle:");
scanf("%d",&rad);
area =PI*rad * rad;
printf("\nArea of circle:%f",area);
ci = 2 * PI * rad;
printf("\nCircumference : %f “,ci);
return(0);
}
Output
Enter radius of circle: 6
Area of circle : 113.040001
Circumference : 37.680000
Result
EXP No.2 A : Using If-else to determine Whether a person is Eligible for Voting or Not
Aim
Algorithm
SourceCode:
#include<stdio.h>
int main()
{
int a;
printf("Enter the age of the person:");
scanf("%d",&a);
if(a>=18)
{
printf("Eligible for voting");
}
else
{
printf("Not eligible for voting\n");
}
return0;
}
Output:
First run:
Enter the age of the person : 23
Eligible for voting
Second run:
Enter the age of the person : 11
Not Eligible for voting
Result
EXP No.2 A : To print the odd number up to the given limits using goto statement
Aim
Algorithm
Source Code:
#include<stdio.h>
void main()
{
int i,n;
printf("\nEnter the starting value:");
scanf("%d",&i);
printf("\nEnter the Ending value:");
scanf("%d",&n);
start:
if(i%2==1)
{
printf("\n%d",i);
}
i++;
if(i<=n)
{
goto start;
}
return 0;
}
Output:
Enter the starting value : 5
Enter the Ending value : 155
7
9
11
13
15
Result
EXP No.2 b : To find and print the present Elevator position using switch case.
Aim
Algorithm
Source Code:
#include<stdio.h>
int main()
{
int a;
printf("Enter Floor No:");
scanf("%d",&a);
switch(a)
{
case1:
printf("\nFirst Floor");
break;
case2:
printf("\nSecond Floor");
break;
case3:
printf("\nThird Floor");
break;
default:
printf("Invalid No");
break;
}
return0;
}
Output :
Enter Floor No :
3Third Floor
Result
EXPNo. 2c To read N positive integer numbers.
Aim
Algorithm
Source Code:
#include<stdio.h>
void main()
{
int number;
while(1)
{
printf("Enter integer number:");
scanf("%d",&number);
if(number==0)
{
printf("Invalid input...\n");
continue;
}
elseif(number<0)
{
printf("Terminating loop...\n");
break;
}
printf("Numberis:%d\n",number);
}
printf("Bye,Bye...\n")
return0;
}
Output :
Enter integer number :
10Numberis : 10
Enter integer number :
20Numberis : 20
Enter integer number :
0Invalidinput…
Enter integer number :
30Numberis : 30
Enter integer number : -
1Terminating loop…
Bye,Bye…
Result
EXPNo.3 A Find a factorial of a number
Aim
Algorithm
Source Code
#include<stdio.h>
#include <conio.h>
voidmain()
{
inti,a,f=1;
printf("Enter a number:\n");
scanf("%d",&a);
for(i=1;i<=a;i++)
f=f*i;
printf("Factorial of%dis%d\n",a,f);
getch();
}
Output.
Enter a number : 5
Factorial of 5is120.
Result
EXPNo.3 B To find a given number is Armstrong or not
Aim
Algorithm
Source Code
#include<stdio.h>
#include<math.h>
int main()
{
int Number, Temp, Reminder, Times =0, Sum=0;
printf("\nPlease Enter number to Check for Armstrong \n");
scanf("%d",&Number);
Temp = Number;
while(Temp!=0)
{
Times = Times + 1;
Temp=Temp/10;
}
Temp = Number;
while(Temp>0)
{
Reminder=Temp%10;
Sum=Sum+pow(Reminder,Times);
Temp= Temp /10;
}
if(Number==Sum )
printf("\n%d is Armstrong Number.\n",Number);
else
printf("\n %d is not a Armstrong Number.\n", Number);
return0;
}
Output :
Please Enter number to Check for Armstrong 153
153 is Armstrong Number.
Result
EXPNo.3C To read any integer number and print its multiplication table
Aim
Algorithm
Source Code
#include <stdio.h>
int main()
{
int num;
int i;
printf("Enter an integer number:");
scanf("%d",&num);
i=1;
do
{
printf("%d\n",(num*i));
i++;
}
while(i<=10);
return 0;
}
Output.
Enter an integer number: 1212
24
36
48
60
72
84
96
108
120
Result
EXPNo.4 A To sort one dimensional array elements in ascending order.
Aim
Algorithm
SourceCode
#include<stdio.h>
#define MAX 100
int main()
{
int arr[MAX], n, i, j;
int temp;
printf("Enter total number of elements:");
scanf("%d",&n);
printf("Enter array elements:\n");
for(i =0; i <n; i++) {
printf("Enter element %d:",i+1);
scanf("%d",&arr[i]);
}
for(i =0; i<n; i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i]>arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] =temp;
}
}
}
printf("\nArray elements after sorting:\n");
for(i =0; i <n; i++)
{
printf("%d\n",arr[i]);
}
return0;
}
Output.
Enter total number of elements : 5
Enter array elements :
Enter array element 1 : 100
Enter array element 2 : 999
Enter array element 3 : 200
Enter array element 4 : 800
Enter array element 5 : 300
Array elements after sorting:
100
200
300
800
999
Result
EXPNo.4 B To find sum of all elements of each row of a matrix.
Aim
Algorithm
Source Code
#include <stdio.h>
#define MAXROW 10
#define MAXCOL 10
int main()
{
int matrix[MAXROW][MAXCOL];
int i,j,r,c;
int sum,product;
printf("Enter number of Rows:");
scanf("%d",&r);
printf("Enter number of Cols:");
scanf("%d",&c);
printf("\nEnter matrix elements:\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("Enterelement[%d,%d]:",i+1,j+1);
scanf("%d",&matrix[i][j]);}
}
printf("\n");
/*sum of all rows*/for(i=0;i<r;i++)
{
sum=0;for(j=0;j<c;j++) /*initializing sum*/
{
printf("%d\t",matrix[i][j]); /*print elements*/
sum+= matrix[i][j];
}
printf("\tSUM:%d",sum);
printf("\n"); /*after each row print newline*/
}
}
Output.
Enternumber ofRows : 3
Enternumber ofCols : 3
Enter matrix elements :
Enter element[1,1] : 1
Enter element[1,2]:2
Enter element[1,3]:3
Enter element[2,1]:4
Enter element[2,2]:5
Enter element[2,3]:6
Enter element[3,1]:7
Enter element[3,2]:8
Enter element[3,3]:9
1 2 3 SUM: 6
4 5 6 SUM: 15
7 8 9 SUM: 24
RESULT
EXPNo.4 C To find out the average of 4 integers.
Aim
Algorithm
SourceCode
#include <stdio.h>
int main()
{
int avg = 0;
int sum =0;
int x=0;
/* Array- declaration – length 4*/
int num[4];
for(x=0; x<4;x++)
{
printf("Enter number %d\n",(x+1));
scanf("%d",&num[x]);
}
for(x=0; x<4;x++)
{
sum = sum+num[x];
}
avg=sum/4;
printf("Average of entered number is: %d", avg);
return0;
}
Output
Enternumber1
23
Enternumber2
66
Enternumber3
90
Enternumber4
12
Average of entered number is: 47
Result
EXPNo.5 To count the total number of vowel or consonant in a string
Aim
Algorithm
Source Code
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
#definestr_size100 //Declare the maximum size of the string
voidmain()
{
charstr[str_size];
inti,len, vowel,cons;
printf("\n\nCount total number of vowel or consonant :\n");
printf(" \n");
printf("Input the string: ");
fgets(str,sizeofstr,stdin);
vowel=0;
cons=0;
len=strlen(str);
for(i=0;i<len;i++)
{
if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u'||str[i]=='A'||str[i]=='E'||str[i]=='I'||
str[i]=='O'||str[i]=='U')
{
vowel++;
}
else if((str[i]>='a'&&str[i]<='z') ||(str[i]>='A'&&str[i]<='Z'))
{
cons++;
}
}
printf("\nThe total number of vowel in the string is : %d\n", vowel);
printf("The total number of consonant in the string is :%d\n\n",cons);
}
Output
Count total number of vowel and consonant:
Input the string: C Programming Laboratory
The total number of vowel in the string is :7
The total number of consonant in the string is : 15
Result
EXPNo.6 A Using passing arrays to a function to display the given array elements is odd or even
Aim
Algorithm
SourceCode
#include<stdio.h>
#include<conio.h>
void check(int num);
int main()
{
intarr[10], i;
printf("Enter10Arrayelements:\n");
for(i=0;i<10; i++)
scanf("%d",&arr[i]);
printf("\nChecking each elements for Even/Odd..\n");
for(i=0;i<10; i++)
check(arr[i]);getch();
return0;
}
void check(intnum)
{
if(num%2==0)
printf("%d is Even\n", num);
else
printf("%disOdd\n",num);
Output
Enter10Arrayelements:
12
23
34
45
56
76
45
23
98
123
Checking each elements for
Even/Odd..12is Even
23 is Odd
34is Even
45 is Odd
56isEven
76isEven
45 is Odd
23 is Odd
98is Even
123 is Odd
Result
EXPNo.6 B To Swap two numbers using Call by reference
Aim
Algorithm
Source Code
#include<stdio.h>
void swap(int*,int*);//Swap function declaration
int main()
{
intx,y;
printf("Enter the value of x and y\n");
scanf("%d%d",&x,&y);
printf("Before Swapping\nx=%d\ny=%d\n",x,y);
swap(&x,&y);
printf("After Swapping\nx=%d\ny=%d\n",x,y);
return0;
}
//Swap function definition
void swap(int*a, int*b)
{
int t;
t= *b;
*b=*a;
*a=t;
}
Output
Enter the value of x and y
45
90
Before Swapping
x =45
y = 90
After Swapping
x=90
y=45
Result
EXPNo.7 To Perform Quick Sort on a set of Entries from a File using Recursion
Aim
Algorithm
Source Code.
#include<stdio.h>
Void quicksort(int [],int,int);
int main()
{
int list[50];intsize, i;
printf("Enter the number of elements: ");
scanf("%d",&size);
printf("Enter the elements to be sorted:\n");
for(i =0; i <size; i++)
{
scanf("%d",&list[i]);
}
quicksort(list, 0, size - 1);
printf("After applying quick sort\n");
for(i=0; i <size; i++)
{
printf("%d",list[i]);
}
printf("\n");
return0;
}
void quicksort(int list[],int low,int high)
{
int pivot, i, j, temp;
if(low <high)
{
pivot = low;i = low;
j = high;
while(i<j)
{
while(list[i] <=list[pivot] &&i<=high)
{
i++;
}
while(list[j] >list[pivot] &&j>=low)
{
j--;
}
if(i < j)
{
temp = list[i];
list[i] = list[j];
list[j]=temp;
}
}
temp = list[j];
list[j] = list[pivot];
list[pivot]=temp;
quicksort(list, low, j - 1);
quicksort(list,j+1,high);
}
}
Output
Enter the number of elements:6
Enter the elements to be
sorted:67
45
24
98
12
38
After applying quick sort
122438456798
Result
EXPNo.8 a To compute the sum of all elements stored in an array using Pointers
Aim
Algorithm
Source Code
#include<stdio.h>
#include<conio.h>
void main()
{
inta[10];
int i,
sum=0;
int*p;
printf("Enter 10 elements\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
p=a;
for(i=0;i<10;i++)
{
sum=sum+*p;p++;
}
printf("The sum of array elements is %d",sum);
}
Output
Enter 10 elements:
1
2
3
4
5
6
7
8
9
12
The sum of array elements is 57
Result
EXPNo.8 b: Using pointer to determine the length of a character string
Aim
Algorithm
SourceCode
#include<stdio.h>
#include<conio.h>
int string_in(char*);
void main()
{
char str[20];
int length;
clrscr();
printf("\nEnter any string:");
gets(str);
length=string_ln(str);
printf("The length of the given string %s is:%d",str,length);
getch();
}
int string_ln(char*p)/* p=&str[0] */
{
int count = 0;while(*p!='\0')
{
count++; p++;
}
return count;
}
Output.
Enter any string:
Technology
The length of the given string Technology is: 10
Result.
EXP No.9 a: Gets and display the report of the student details using Nested Structures
Aim.
Algorithm.
Source Code.
#include <stdio.h>
struct Marks
{
int math;
int physics;
int chemistry;
};
struct Student {
char name[50];
int rollNo;
struct Marks marks;
};
int main()
{
struct Student student;
printf("Enter student's name: ");
fgets(student.name, sizeof(student.name), stdin);
printf("Enter roll number: ");
scanf("%d", &student.rollNo);
printf("Enter marks for Math, Physics, and Chemistry:\n");
scanf("%d %d %d", &student.marks.math, &student.marks.physics, &student.marks.chemistry);
printf("\n--- Student Details ---\n");
printf("Name: %s", student.name);
printf("Roll Number: %d\n", student.rollNo);
printf("Math: %d\n", student.marks.math);
printf("Physics: %d\n", student.marks.physics);
printf("Chemistry: %d\n", student.marks.chemistry);
return 0;
}
Output.
Enter student's name: Pavithra V
Enter roll number: 101
Enter marks for Math, Physics, and Chemistry: 85 90 88
--- Student Details ---
Name: Pavithra V
Roll Number: 101
Math: 85 Physics: 90 Chemistry: 88
Result
EXPNo.9 b : To display the student Name, Marks and Percentage using Union
Aim.
Algorithm.
Source Code.
#include <stdio.h>
struct Student {
char name[50];
int rollNo;
float marks;
};
int main() {
struct Student student;
struct Student *ptr = &student;
printf("Enter student's name: ");
fgets(ptr->name, sizeof(ptr->name), stdin);
printf("Enter roll number: ");
scanf("%d", &ptr->rollNo);
printf("Enter marks: ");
scanf("%f", &ptr->marks);
printf("\n--- Student Details ---\n");
printf("Name: %s", ptr->name);
printf("Roll Number: %d\n", ptr->rollNo);
printf("Marks: %.2f\n", ptr->marks);
return 0;
}
Output.
Enter student's name: Pavi
Enter roll number: 102
Enter marks: 92.5
--- Student Details ---
Name: Pavi
Roll Number: 102
Marks: 92.50
Result:
EXPNo.9 C : Arrays of Structures and Unions
Aim.
Algorithm.
Source Code.
#include <stdio.h>
struct Student {
char name[50];
int rollNo;
float marks;
};
union Data {
int id;
float percentage;
};
int main() {
struct Student students[2];
union Data data;
for (int i = 0; i < 2; i++) {
printf("Enter student's name: ");
scanf("%s", students[i].name);
printf("Enter roll number: ");
scanf("%d", &students[i].rollNo);
printf("Enter marks: ");
scanf("%f", &students[i].marks);
}
printf("\n--- Student Details ---\n");
for (int i = 0; i < 2; i++) {
printf("Name: %s\n", students[i].name);
printf("Roll Number: %d\n", students[i].rollNo);
printf("Marks: %.2f\n", students[i].marks);
}
data.id = 101;
printf("\nUnion Data ID: %d\n", data.id);
data.percentage = 85.5;
printf("Union Data Percentage: %.2f\n", data.percentage);
return 0;
}
Output:
Enter student's name: Pavi
Enter roll number: 101
Enter marks: 90.5
Enter student's name: Niva
Enter roll number: 102
Enter marks: 88.0
--- Student Details ---
Name: Pavi
Roll Number: 101
Marks: 90.50
Name: Niva
Roll Number: 102
Marks: 88.00
Union Data ID: 101
Union Data Percentage: 85.50
Result:
EXP No.10 Files: reading and writing, File pointers, file operations, random access,
Processor directives
Aim.
Algorithm.
Source Code.
#include <stdio.h>
#include <stdlib.h>
#define FILE_NAME "example.txt"
int main() {
FILE *file;
file = fopen(FILE_NAME, "w");
if (file == NULL) {
printf("Unable to open file for writing.\n");
exit(1);
}
fprintf(file, "Hello, World!\nThis is a file handling example.\n");
fprintf(file, "1234567890\n");
fclose(file);
file = fopen(FILE_NAME, "r");
if (file == NULL) {
printf("Unable to open file for reading.\n");
exit(1);
char ch;
printf("File content:\n");
while ((ch = fgetc(file)) != EOF) {
putchar(ch);
fclose(file);
file = fopen(FILE_NAME, "r+");
if (file == NULL) {
printf("Unable to open file for random access.\n");
exit(1);
fseek(file, 18, SEEK_SET);
fprintf(file, "RANDOM");
fclose(file);
file = fopen(FILE_NAME, "r");
if (file == NULL) {
printf("Unable to open file to read modified content.\n");
exit(1);
printf("\nModified file content:\n");
while ((ch = fgetc(file)) != EOF) {
putchar(ch);
fclose(file);
return 0;
OutPut:
File content: Hello, World! This is a file handling example.
1234567890 Modified file content: Hello, World
Result: