C Lab Manual
C Lab Manual
Table Of Contents
S.NO LIST OF EXPERIMENTS
1. Programs using I/O statements and expressions
2. Programs using decision-making constructs
3. Write a program to find whether the given year is leap year or not?(Hint:not every
centurion year is a leap.for example 1700,1800 and 1900 is not a leap year)
7. Populate an array with height of persons and find how many persons are above the
average height
8. Populate a two dimensional array with height and weight of person and compute th
Body Mass Index of the Individuals
9 Given a String “a$bcd./fg”find its reverse without changing the position of special
characters.(Example input:a@gh%;j and output:j@hg%;a)
10 Convert the given decimal number into library,octal and hexadecimal numbers
using user defined functions
Algorithm to convert Decimal to Binary number
Divide the input decimal number by 2 and store the remainder
Store the quotient back to the input number variable.
Prepared by Reviewed By Approved by Dr.K.Sakthivel
A.RAJALAKSHMI HOD/CSE PRINCIPAL
AP/CSE Dr.S.Erana Veerappa Dinesh
LABORATORY ACTIVITY MANUAL
15 Compute internal mark of students for five different subjects using structures and
functions
16 Count the number of account holders whose balance is less than the minimum
balance using sequential access file
EXERCISE-1
AIM
To Write a C Program for addition and subtraction of two numbers and display the result using
I/O statements
Reference
List of Apparatus
HARDWARE:
1. Personal Computer/user
SOFTWARE:
Turbo C compiler
PRINCIPLE
C Programming has several in built library functions to perform input and output tasks.Two
commonly used functions for I/O (Input/Output)are printf() and scanf().The Scanf() function
reads formatted input from standard input(keyboard)whereas the printf() function sends
formatted output to the standard output.An Expression is any legal combination of symbols that
represents a value.C Programming provides its own rules of Expression ,whether it is legal
expression or illegal expression.Every expression consists of at least one oprand and can have
one or more operators.Operands are values and Operators are Symbols that represent Particular
actions.
Steps:
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
int a,b,c;
scanf("%d%d",&a,&b);
c=a+b;
c=a+b;
getch();
OUTPUT
Result:
EXERCISE-2
Aim:
To write a C Program for finding maximum value using decision making constructs.
Reference
List of Apparatus
HARDWARE:
1. Personal Computer/user
SOFTWARE:
Turbo C compiler
Principle
Decision making is about deciding the order of execution of statements based on certain
conditions or repeat a group of statements until certain specified conditions are met.C Language
handles decision making by supporting the following statements,
If statement
Switch statement
Conditional operator statement(?:Operator)
Goto Statement
Steps
Step 5.1:If it is true then check the inner condition as a>c using if statement
Step 5.2:If the inner condition is true then print a is the greatest Else print c is the greatest
PROGRAM
#include<stdio.h>
void main()
int a,b,c;
printf("Enter 3 numbers");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
if(a>c)
else
}else
if(b>c)
else
}}}
OUTPUT
Result
EXERCISE-3
Aim
Reference
List of Apparatus
HARDWARE:
1. Personal Computer/user
SOFTWARE:
Turbo C compiler
Principle
C Programming has several in built library functions to perform input and output tasks.Two
commonly used functions for I/O (Input/Output)are printf() and scanf().The Scanf() function
reads formatted input from standard input(keyboard)whereas the printf() function sends
formatted output to the standard output.An Expression is any legal combination of symbols that
represents a value.C Programming provides its own rules of Expression ,whether it is legal
expression or illegal expression.Every expression consists of at least one oprand and can have
one or more operators.Operands are values and Operators are Symbols that represent Particular
actions.
Steps:
Program
#include<stdio.h>
#include<conio.h>
void main()
int year;
printf("Enter a year");
scanf("%d",&year);
if(year%4==0)
{
Prepared by Reviewed By Approved by Dr.K.Sakthivel
A.RAJALAKSHMI HOD/CSE PRINCIPAL
AP/CSE Dr.S.Erana Veerappa Dinesh
LABORATORY ACTIVITY MANUAL
if(year%100==0)
if(year%100==0)
if(year%400==0)
else
else
else
Result:
EXERCISE-4
Design a Calculator
Aim:
Reference
List of Apparatus
HARDWARE:
1. Personal Computer/user
SOFTWARE:
Turbo C compiler
Principle
A Switch statement allows a variable to be tested for equality against a list of values.Each
value is called a case,and the variable being switched on is checked for each switch case.When
the variable being switched on is equal to a case,the statements following that case will execute
until a break statement is reached.
Steps:
#include<stdio.h>
void main()
int a,b,c;
printf("Enter 3 numbers");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
if(a>c)
else
}else
if(b>c)
else
PROGRAM
#include<stdio.h>
void main()
int n1,n2,n3,opt;
scanf("%d%d",&n1,&n2);
scanf("%d",&opt);
switch(opt)
case 1:
n3=n1+n2;
printf("result:%d",n3);
break;
case 2:
n3=n1-n2;
printf("result:%d",n3);
break;
case 3:
n3=n1*n2;
printf("result:%d",n3);
break;
case 4:
n3=n1/n2;
printf("result:%d",n3);
case 5:
n1=n1^2;
n2=n2^2;
printf("result:%d%d",n1,n2);
break;
OUTPUT
Result:
Thus the program was implemented and the output was verified.
Prepared by Reviewed By Approved by Dr.K.Sakthivel
A.RAJALAKSHMI HOD/CSE PRINCIPAL
AP/CSE Dr.S.Erana Veerappa Dinesh
LABORATORY ACTIVITY MANUAL
EXERCISE-5
Aim:
Reference
List of Apparatus
HARDWARE:
1. Personal Computer/user
SOFTWARE:
Turbo C compiler
Principle
Decision making is about deciding the order of execution of statements based on certain
conditions or repeat a group of statements until certain specified conditions are met.C language
handles decision
Steps
Step6:If the result is equal to original number then print Armstrong number else print not
Armstrong number
PROGRAM
#include<stdio.h>
void main()
int number,originalnumber,remainder,result=0;
scanf("%d",&number);
originalnumber=number;
while(originalnumber!=0)
remainder=originalnumber%10;
result+=remainder*remainder*remainder;
originalnumber/=10;
if(result==number)
else
OUTPUT
Result
EXERCISE-6
Prepared by Reviewed By Approved by Dr.K.Sakthivel
A.RAJALAKSHMI HOD/CSE PRINCIPAL
AP/CSE Dr.S.Erana Veerappa Dinesh
LABORATORY ACTIVITY MANUAL
Aim:
Reference
List of Apparatus
HARDWARE:
1. Personal Computer/user
SOFTWARE:
Turbo C compiler
Principle
An array is a collection of data that holds fixed number of values of same type. Arrays have 0 as
the first index not 1.In this example, mark[0]
If the size of an array is n, to access the last element,(n-1) index is used. In this example, mark[4]
Steps
Step 6:Sort the numbers Based on the weight in the increasing order as shown below<10,its
weight>,<36,its weight><89.its weight>.’,
#include<stdio.h>
#include<math.h>
#include<conio.h>
int prime(intnum)
inti,flag=0;
for(i=2;i<num;i++)
if(num%i==0)
flag=1;
break;
return flag;
intgetWeight(int n)
int w=0;
floatcube_root=pow(n,1.0/3.0);
if(cube_root==ceil(cube_root))
w+=5;
if(n%4==0&&n%6==0)
w+=4;
if(prime(n)==0)
w+=3;
return w;
int main()
intnums[15];
clrscr();
intws[15];
inti,j,t,n;
Prepared by Reviewed By Approved by Dr.K.Sakthivel
A.RAJALAKSHMI HOD/CSE PRINCIPAL
AP/CSE Dr.S.Erana Veerappa Dinesh
LABORATORY ACTIVITY MANUAL
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&nums[i]);
for(i=0;i<n;i++)
ws[i]=getWeight(nums[i]);
for(i=0;i<n;i++)
printf("%d:%d\t",nums[i],ws[i]);
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(ws[j]>ws[j+1])
t=ws[j+1];
ws[j+1]=ws[j];
ws[j]=t;
t=nums[j+1];
nums[j+1]=nums[j];
nums[j]=t;
Prepared by Reviewed By Approved by Dr.K.Sakthivel
A.RAJALAKSHMI HOD/CSE PRINCIPAL
AP/CSE Dr.S.Erana Veerappa Dinesh
LABORATORY ACTIVITY MANUAL
printf("\nsorted:\n");
for(i=0;i<n;i++)
printf("%d%d\t",nums[i],ws[i]);
return 0;
getch();
OUTPUT
Result:
EXERCISE-7
To Write A C Program Populate an array with height of persons and find how many persons are
above the average height
Reference
List of Apparatus
HARDWARE:
1. Personal Computer/user
SOFTWARE:
Turbo C compiler
Principle
Loops are user in programming to repeat a specific block until some end condition is met. There
are three loops in C programming:
For loop
While loop
do…while loop
Steps:
#include <stdio.h>
#include<conio.h>
void main ()
clrscr();
inti,n,count =0;
floatperson_h[100];
float sum=0,avg;
scanf("%d",&n);
for(i=0;i<n;i++)
{
Prepared by Reviewed By Approved by Dr.K.Sakthivel
A.RAJALAKSHMI HOD/CSE PRINCIPAL
AP/CSE Dr.S.Erana Veerappa Dinesh
LABORATORY ACTIVITY MANUAL
printf("\nenterperson_h[%d]=",i);
scanf("%f",&person_h[i]);
sum=sum+person_h[i];
avg=sum/n;
for(i=0;i<n;i++)
if(avg<person_h[i])
count++;
getch();
Result:
Thus the Program was implemented and the output was verified.
EXERCISE-8
Find BMI
Aim
To write a C Program Populate a two dimensional array with height and weight of person and
compute the Body Mass Index of the Individuals
Reference
List of Apparatus
HARDWARE:
1. Personal Computer/user
SOFTWARE:
Turbo C compiler
Principle
An array is a collection of data that holds fixed number of values of same type. Arrays have 0 as
the first index not 1.In this example, mark[0]
If the size of an array is n, to access the last element,(n-1) index is used. In this example, mark[4]
Populate an array with height of persons and find how many persons are above the average
height
Steps:
Step5:Calculate t=(w/h*h)
Program
#include<stdio.h>
#include<conio.h>
void main()
inti,n;
floatperson_hw[100][2];
floatbmi;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%f%f", &person_hw[i][0],&person_hw[i][1]);
bmi =person_hw[i][1]/(person_hw[i][0]*person_hw[i][0]);
Prepared by Reviewed By Approved by Dr.K.Sakthivel
A.RAJALAKSHMI HOD/CSE PRINCIPAL
AP/CSE Dr.S.Erana Veerappa Dinesh
LABORATORY ACTIVITY MANUAL
getch();
}}
OUTPUT
Result
Thus the program was implemented and the output was verified.
Prepared by Reviewed By Approved by Dr.K.Sakthivel
A.RAJALAKSHMI HOD/CSE PRINCIPAL
AP/CSE Dr.S.Erana Veerappa Dinesh
LABORATORY ACTIVITY MANUAL
EXERCISE-9
Aim:
To write a C Program Given a String “a$bcd./fg”find its reverse without changing the position of
special characters.(Example input:a@gh%;j and output:j@hg%;a)
Reference
List of Apparatus
HARDWARE:
1. Personal Computer/user
SOFTWARE:
Turbo C compiler
Principle
String are declared in a similar manner as arrays.only difference is that,String are of char type
Steps:
Program
#include<stdio.h>
#include<string.h>
#include<conio.h>
int main()
char s[20],s1[20]={"00000000000000000000"};
inti,j,length,flag;
gets(s);
length=strlen(s);
for(i=0;i<length;i++)
if(!((s[i]>='a'&&s[i]<='z')||(s[i]>='A'&&s[i]<='Z')||(s[i]>='0'&&s[i]<='9')))
s1[i]=s[i];
j=length-1;
Prepared by Reviewed By Approved by Dr.K.Sakthivel
A.RAJALAKSHMI HOD/CSE PRINCIPAL
AP/CSE Dr.S.Erana Veerappa Dinesh
LABORATORY ACTIVITY MANUAL
for(i=0;i<length;i++)
flag=0;
if(((s[i]>='a'&&s[i]<='z')||(s[i]>='A'&&s[i]<='Z')||(s[i]>='0'&&s[i]<='9')))
do
if(s1[j]=='0')
s1[j]=s[i];
flag=1;
j--;
else
j--;
while(flag!=1);
s1[length]='\0';
printf("\nReverse String:%s\n",s1);
return(0);
Prepared by Reviewed By Approved by Dr.K.Sakthivel
A.RAJALAKSHMI HOD/CSE PRINCIPAL
AP/CSE Dr.S.Erana Veerappa Dinesh
LABORATORY ACTIVITY MANUAL
getch();
Result:
EXERCISE-10
Aim:
To write a c Program Convertthe given decimal number into binary,octal and hexadecimal
numbers using user defined functions
Reference
List of Apparatus
HARDWARE:
1. Personal Computer/user
SOFTWARE:
Turbo C compiler
Principle
C uses a variety of number types—different numeric data types, so to speak. Table 1 lists them
all, along with other statistical information.
The table is something you’ll refer to now and again because only the truly insane would
memorize it all.
Function elements
1.Function declaration
2.Function definition
3.Function calling
Equivalent binary number will be the remainders in above process in reverse order.
Steps:
PROGRAM
#include<stdio.h>
longdecimalToBinary(long n);
longdecimalToOctal(long n);
longdecimalToHexadecimal(long n);
int main()
long decimal;
scanf("%ld",&decimal);
return 0;
longdecimalToBinary(long n)
int remainder;
long binary=0,i=1;
while(n!=0)
remainder=n%2;
n=n/2;
binary=binary+(remainder*i);
i=i*10;
return binary;
longdecimalToOctal(long n)
Prepared by Reviewed By Approved by Dr.K.Sakthivel
A.RAJALAKSHMI HOD/CSE PRINCIPAL
AP/CSE Dr.S.Erana Veerappa Dinesh
LABORATORY ACTIVITY MANUAL
int remainder;
long octal=0,i=1;
while(n!=0)
remainder=n%8;
n=n/8;
octal=octal+(remainder*i);
i=i*10;
return octal;
longdecimalToHexadeciaml(long n)
int remainder;
long hexadecimal=0,i=1;
while(n!=0)
remainder=n%16;
n=n/16;
Prepared by Reviewed By Approved by Dr.K.Sakthivel
A.RAJALAKSHMI HOD/CSE PRINCIPAL
AP/CSE Dr.S.Erana Veerappa Dinesh
LABORATORY ACTIVITY MANUAL
hexadecimal=hexadecimal+(remainder*i);
i=i*10;
return hexadecimal;
OUTPUT:
Result
Thus the Program was implemented and the output was verified.
EXERCISE-11
Built In Functions
AIM
To Write a C Program From a given paragraph perform the following using built in functions
Principle
Steps:
Program:
#include<stdio.h>
#include<ctype.h>
#include<string.h>
intwordCount(char *);
int main()
{
Prepared by Reviewed By Approved by Dr.K.Sakthivel
A.RAJALAKSHMI HOD/CSE PRINCIPAL
AP/CSE Dr.S.Erana Veerappa Dinesh
LABORATORY ACTIVITY MANUAL
charpara[MAX]={0};
printf("Enter a Paragraph:");
scanf("%[^\n]s",para);
capitalize(para);
replace(para);
return 0;
int i=0;
do
if(i==0||(p[i-1]==''&&(p[i-2]=='.'||p[i-2]=='?')))
do
p[i]=toupper(p[i]);
Prepared by Reviewed By Approved by Dr.K.Sakthivel
A.RAJALAKSHMI HOD/CSE PRINCIPAL
AP/CSE Dr.S.Erana Veerappa Dinesh
LABORATORY ACTIVITY MANUAL
i++;
while(p[i]!='');
i++;
while(p[i]!='\0');
intwordCount(char *p)
inti,count=0;
for(i=0;p[i]!='\0';i++)
if(p[i]==``)
count++;
return count+1;
{
Prepared by Reviewed By Approved by Dr.K.Sakthivel
A.RAJALAKSHMI HOD/CSE PRINCIPAL
AP/CSE Dr.S.Erana Veerappa Dinesh
LABORATORY ACTIVITY MANUAL
char word[20],rpword[20];
charstr[10],rp[100],pp[100];
int i=0,j=0,k,len,length;
scanf("%s",word);
scanf("%s",rpword);
length=strlen(p);
for(k=0;k<length;k++)
j=0;
do
if(p[k+j]!=''||p[k+j]!='.'||p[k+j]!='?')
str[j]=p[k+j];
j++;
else
{
Prepared by Reviewed By Approved by Dr.K.Sakthivel
A.RAJALAKSHMI HOD/CSE PRINCIPAL
AP/CSE Dr.S.Erana Veerappa Dinesh
LABORATORY ACTIVITY MANUAL
j++;
while((p[k+j]!='')&&(p[k+j]!='.')&&(p[k+j]!='?')&&(p[k+j]!='\0'));
str[j]='\0';
if(strcmp(str,word)==0)
len=strlen(str);
strncpy(pp,p,k);
strncpy(rp,p+k+len,length-k-len);
strcpy(p,pp);
strcat(p,rpword);
strcat(p,rp);
/*for(i=0;i<len;i++)
p[k+i]=rpword[i];
*/
}}}
Result
Prepared by Reviewed By Approved by Dr.K.Sakthivel
A.RAJALAKSHMI HOD/CSE PRINCIPAL
AP/CSE Dr.S.Erana Veerappa Dinesh
LABORATORY ACTIVITY MANUAL
EXERCISE-12
Towers of Hanoi
Aim:
Reference
List of Apparatus
HARDWARE:
1. Personal Computer/user
SOFTWARE:
Turbo C compiler
Principle:
Each transfer or move should consist of taking the upper disk fom one of the stacks and then
placing it the of another stack i.e.only a topmost disk on the stack can be moved.
Larger disk cannot be placed over smaller disk;placing of disk should be in increasing prder.
This Program uses recursive function and solves the towers of Hanoi.The tower
Mathematical puzzle .it contains of three rods and a number of desks of different sizes which
can slide onto any rod .
The puzzle starts with the disks in a neet stack in assending order of size on one rod,thesmallestat
the top we have to obtain the same strack
On the third rod .here is the source cord of the c program for solving towers of Hanoi.
Steps:
Step1:Start the Program
Step6:Each move of the disk should be placed on the top of another stack.
Program:
#include<stdio.h>
#include<conio.h>
Intmain()
intnum;
scanf("%d",&num);
towers(num,'A','C','B');
return 0;
void towers(intnum,charfrompeg,chartopeg,charauxpeg)
if(num==1)
return;
towers(num-1,frompeg,auxpeg,topeg);
towers(num-1,auxpeg,topeg,frompeg);
OUTPUT
Result:
Thus the program was implemented and the output was verified
EXERCISE-13
Pass by Reference
Aim
To Write the program sort the list of numbers using pass by reference
Reference
List of Apparatus
HARDWARE:
1. Personal Computer/user
SOFTWARE:
Turbo C compiler
Principle
Sorting is any process of arranging items systematically,and has two common,yet distinct
meanings:ordering:arranging items in a sequence ordered by some
criterion;categorizing :grouping items with similar properties
program:
#include<stdio.h>
#include<conio.h>
int main()
int i;
int a[10]={45,35,23,12,78,67,43,90,88,33};
sort(a);
for(i=0;i<10;i++)
printf("%d",a[i]);
return 0;
intc,d,swap;
for(c=0;c<9;c++)
Prepared by Reviewed By Approved by Dr.K.Sakthivel
A.RAJALAKSHMI HOD/CSE PRINCIPAL
AP/CSE Dr.S.Erana Veerappa Dinesh
LABORATORY ACTIVITY MANUAL
for(d=0;d<9-c;d++)
if(array[d]>array[d+1])
swap=array[d];
array[d]=array[d+1];
array[d+1]=swap;
getch();
OUTPUT
Result:
EXERCISE-14
To write a C Program Generate salary slip of employees using structures and pointers
Reference
List of Apparatus
HARDWARE:
1. Personal Computer/user
SOFTWARE:
Turbo C compiler
Principle:
A struct in the C Programming language (and many derivatives) is a composite data type
declaration that defines a physically grouped list od variables to
Be placed under one name in a block of memory, allowing the different variables to be accessed
via a single pointer, or the struct declared name which
Returns the same address. The struct can contain many other complex and simple data types in
an association, so is a natural organizing type for records like the mixed data types in lists of
directory entries reading a hard drive(file length, name type, extension, physical
( cylinder,disk,head indexes) address, etc.),or other mixed record type(patient names, assress,
telephone… insurance codes, balance, etc.)
Steps:
Program
#include<stdio.h>
#include<conio.h>
struct employee
int no;
char name[10];
intdesignationNo;
intdaysWorked;
};
void main()
int i;
Prepared by Reviewed By Approved by Dr.K.Sakthivel
A.RAJALAKSHMI HOD/CSE PRINCIPAL
AP/CSE Dr.S.Erana Veerappa Dinesh
LABORATORY ACTIVITY MANUAL
intdaySal[4]={2000,1000,750,500};
employeePtr=&emp[0];
for(i=0;i<12;i++)
printf("\t%s",employeePtr->name);
printf("\tSalary:%d\n",daySal[employeePtr->designationNo-1]*employeePtr->daysWorked);
employeePtr++;
OUTPUT
EXERCISE-15
To write a C Program to Compute internal mark of students for five different subjects using
structures and functions
Reference
List of Apparatus
HARDWARE:
1. Personal Computer/user
SOFTWARE:
Turbo C compiler
Principle
Pointers in C are easy and fun to learn. Some C programming tasks are performed mor easily
with pointers, and other tasks, such as dynamic memory allocation, cannot be performed without
using pointers. So it becomes necessary to learn pointers to become a perfect C programmer.
Let’s start learning them in simple and easy steps.
As you know,every variable is a memory location and memory location has its address defined
which can be accessed using ampersand(&) operator, which denotts an address in memory.
Steps:
Step6:Print total
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define S 50
struct student
char name[30];
introllno;
int sub[5];
};
inti,j,total;
Prepared by Reviewed By Approved by Dr.K.Sakthivel
A.RAJALAKSHMI HOD/CSE PRINCIPAL
AP/CSE Dr.S.Erana Veerappa Dinesh
LABORATORY ACTIVITY MANUAL
for(i=0;i<num;i++)
total=0;
for(j=0;j<5;j++)
total=total+s[i].sub[j];
int main()
clrscr();
inti,j,n;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%s",&st[i].name);
scanf("%d",&st[i].rollno);
for(j=0;j<5;j++)
scanf("%d",&st[i].sub[j]);
internal_M(st,n);
return 0;
OUTPUT
Result:Thus the Program was implemented and the output was verified
EXERCISE-16
Aim
Count the number of account holders whose balance is less than the minimum balance using
sequential access file
Reference
List of Apparatus
HARDWARE:
1. Personal Computer/user
SOFTWARE:
Turbo C compiler
Principle:
Steps:
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct customer
char name[20];
intacct_num;
floatacct_balance;
};
int main()
{FILE *fp;
float min=5000;
fp=fopen("accounts.txt","w");
if(fp==NULL)
exit(1);
}
Prepared by Reviewed By Approved by Dr.K.Sakthivel
A.RAJALAKSHMI HOD/CSE PRINCIPAL
AP/CSE Dr.S.Erana Veerappa Dinesh
LABORATORY ACTIVITY MANUAL
int choice=1;
do
printf("\nName:");
scanf("%s",cust.name);
printf("Acct Num:");
scanf("%d",&cust.acct_num);
printf("Balance");
scanf("%f",&cust.acct_balance);
fwrite(&cust,sizeof(struct customer),1,fp);
scanf("%d",&choice);
while(choice==1);
fclose(fp);
fp=fopen("accounts.txt","r");
if(fp==NULL)
exit(1);
else
while(fread(&cust,sizeof(struct customer),1,fp))
if(cust.acct_balance<=min)
}}
fclose(fp);
return 0;
OUTPUT
Result
Thus the program was implemented and the output was verified.
MINI PROJECT