20ESPL101 Programming in C Lab Manual
20ESPL101 Programming in C Lab Manual
AIM:
ALGORITHM:
Step1: Start
Step2:Declare and initialize the variables for name, address, date of birth, mobile
number and age
Step3:Display name, address, date of birth, mobile number and age
Step4:End
PROGRAM:
#include<stdio.h>
void main()
{
charname[20]="SAIRAM";
char address[80]= "west tambharam,chennai";
int date=20;
int month=10;
int year=1990;
int mobile=987456321;
intage=25;
printf("\n=====================");
printf("\n NAME: %s",name);
printf("\nADDRESS:%s",address);
printf("\n DOB:%d:%d:%d", date , month, year);
printf("\n MOBILE NUMBER:%d", mobile);
printf("\n AGE:%d", age);
printf("\n=====================");
}
OUTPUT:
NAME:SAIRAM
ADDRESS:westtambaram,chennai
DOB:20:10:1990
MOBILE
NUMBER:987456321AGE:25
RESULT
Thus the C Program to display the personal details has been executed and the output
wasverified.
EX N O : 3 b
PROGRAM USING I/O STATEMENTS AND EXPRESSIONS
DATE:
AIM:
ALGORITHM:
Step1: Start
Step2:Declare the variables for name, address, date, month, year, mobile number,
age.
Step3:Read values of name, address, date, month, year, mobile number, age from
the user.
Step4:Displayname, address, date, month, year, mobile number, age.
Step5:End
PROGRAM:
#include<stdio.h>
#include<conio.h
>#include<string.
h>int main()
{
Char name[20];
char address[80];
int date;
int month;
int year;
long int mobile;
char gender[20];
int age;
printf("\nENTER YOUR NAME:=");
gets(name);
printf("\nENTER YOUR ADDRESS=");
gets(address);
printf("\nENTER YOUR date/month/year=");
scanf("%d/%d/
%d",&date,&month,&year);printf("\n
ENTER YOUR AGE=");
scanf("%d",&age);
printf("\nENTER YOUR GENDER (MALE/FEMALE)=");
scanf("%s",gender);
printf("\nENTER YOUR MOBILE NUMBER=");
scanf("%ld", &mobile);
printf("\n=====================");
printf("\n NAME: %s", name);
printf("\n ADDRESS:%s", address);
printf("\n DOB:%d:%d:%d", date , month, year);
printf("\n AGE:%d", age);
printf("\n GENDER:%s", gender);
printf("\n MOBILE NUMBER:%d", mobile);
printf("\n=====================");
return0;
}
OUTPUT:
ENTERYOURGENDER(MALE/FEMALE)=MALE
=====================
NAME:karthikeyan
ADDRESS:westtambharam,chennai.
DOB:3:12:1992
AGE:28
GENDER:MALE
MOBILENUMBER:987654321
========================
RESULT:
Thus the C Program to read and display the userdetails has been executed and the
output was verified.
EX N O : 2 a
PROGRAM TO DISPLAY BIGGEST OF TWO NUMBERS
DATE:
AIM:
ALGORITHM:
Step1:Start
Step3:IfA>B,thengotostep6
Step7:PrintC is greatest
Step8:end
PROGRAM:
#include <stdio.h>
Void main()
{
Int A,B,C;
Printf ("Enter 3 integer number\
n");
scanf ("%d", &A);
scanf ("%d", &B);
scanf("%d",&C);
if(A>B)
{
if(A>C)
{
printf("%d istheGreatestNumber\n",A);
}
else
{
printf("%disthegreatestNumber\n",C);
}
}
else
{
if(B>C){
printf("%disthegreatestNumber\n",B);
}
else{
printf("%disthegreatestNumber\n",C);
}
}
}
OUTPUT:
Thus the C Program to display biggest of two numbers has been executed and the
output was verified .
EXN O : 2b PROGRAM TO CHECK WHETHER THE ENTERED
CHARACTER IS VOWEL OR NOT (USESWITCHCASE)
DATE:
AIM:
ALGORITHM:
Step1: Start
Step2:Declare and initialize the variables
Step3:Get the input from the user and compare with each cases
Step5:End
PROGRAM:
#include<stdio.h>
#include<conio.h
>intmain()
{
charch;
printf("Enter a character: ");
scanf("%c",&ch);
//condition to check character is alphabet or not
if((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z'))
{
switch(ch)
{
case'A':
case'E':
case'I':
case'O':
case'U':
case'a':
case'e':
case'i':
case'o':
case'u':
printf("%c is a VOWEL.\n",ch);
break;
default:
printf("%c is a CONSONANT.\n",ch);
}
}
else
{
printf("%c is not an alphabet.\n",ch);
}
return0;
}
OUTPUT:
Enter a character
Eis avowel
Enter a character
X is a consonant
Enter a character
+ is not an alphabet
RESULT:
Thus the C Program check whether the entered character is vowel or not (Use
switchcase) has been executed and the output was verified.
AIM:
ALGORITHM:
Step1:Start
Step4:Check if year is not divisible by100 and divisible by4 then DISPLAY"is a leap
year"
Step6:Stop
PROGRAM:
#include<stdio.h>
#include <conio.h>
voidmain()
{
intyear;
printf("Enter a year :\n");
scanf("%d",&year);
if((year% 400)==0)
printf("%d is a leapyear \n",year);
else
if((year%100)!=0&&(year%4)==0)
printf("%d is a leapyear\n",year);
else
printf("%d is not a leapyear \n",year);
}
OUTPUT:
Enter a year:
2000
2000 is a leapyear
Enterayear:
1900
1900 is not a leapyear
RESULT:
Thus the C Program to find whether the given year is leap year or not has been
executed successfully and the output was verified.
EX NO:1 b
PROGRAM TO FIND FACTORIAL OF A GIVEN
NUMBER
DATE:
AIM:
ALGORITHM:
Step 5. Readvalueofn
6.1:factorial←factorial,
i←i+1
int i,fact=1,number;
printf("Enter anumber:”);
scanf("%d",&number);
for(i=1;i<=number;i++)
{
fact=fact*i;
printf("Factorial of %d is : %d,number,fact);
return0;
}
OUTPUT:
Enter an number: 5
Factorial of 5 is 120
EX N O : 4 b
PROGRAM TO DISPLAY ARRAY ELEMENTS USING 2D ARRAYS
DATE:
AIM:
ALGORITHM:
Step1:Starttheprogram
Step4:Stoptheprogram
PROGRAM:
#include<stdio.h>
int main(){
/* 2D array declaration*/
int disp[2][3];
int i,j;
{for(j=0;j<3;j++){
scanf("%d",&disp[i][j]);
n");
for(i=0;i<2;i++){
for(j=0;j<3;j++) {
printf("%d",disp[i][j]);
if(j==2){
printf("\n");
}
return0;
OUTPUT:
123
456
EX N O : 4 a
PROGRAM TO DISPLAY ARRAY ELEMENTS USING 1D ARRAY
DATE:
AIM:
To display array elements using 1Darray
ALGORITHM:
#include<stdio.h>
int main()
int i=0;
int marks[5]={20,30,40,50,60};
printf("%d\n”,marks[i]);
return0;
}
OUTPUT:
20
30
40
50
60
RESULT:
Thus the C Program to find the factorial of a given number has been successfully
executed and verified.
RESULT:
Thus the C Program to display the array elements of the 2D array has been executed
and the result was verified
RESULT:
Thus the C Program to display the array elements of the 1D array has been executed
and the result was verified
33
EX N O : 5 a
PROGRAM TO FIND THE LENGTH OF STRING
DATE:
AIM:
ALGORITHM:
(i)UsingLibraryFunction
#include <stdio.h>
#include <string.h>
int main()
{
char a[100];
int length;
printf("\n Enter a string to calculate its length=");
gets(a);
length=strlen(a);
printf("\nLength of the string = %d\n", length);
return0;
}
(i) WithoutUsingLibraryFunction
#include <stdio.h>
#include<string.h>
int main()
{
char i=0;
a[100];
int length;
printf("\nEnter a string to calculate its length=");
scanf(“%s”,str);
while(string1[i] !='\0')
{i++;
}
length=i;
printf (“\n Length of the string = %d\n",length);
return 0;
}
OUTPUT:
RESULT:
Thus the C Program to find the length of the string has been executed and
verified.
EXN O : 6 a
PROGRAM TO PERFORM SWAPPING USING FUNCTIONS
DATE:
AIM:
ALGORITHM:
#include<stdio.h>
voidmain()
void swap(int,int);
inta,b,r;
clrscr();
printf("enter value for a&b: ");
scanf("%d%d",&a,&b);
swap(a,b);
getch();
void swap(inta,int b)
int temp;
temp=a;
a=b;
b=temp
;
printf("after swapping the value for a&b is:%d%d",a,b);
}
OUTPUT:
AIM:
ALGORITHM:
Step3:Find and Display the prime numbers ie.,the numbers that are divisible by1and
itself between the intervals
#include<stdio.h>
/* Function declarations */
int isPrime(intnum);
Void printPrimes(intlowerLimit,intupperLimit);
int main()
scanf("%d%d",&lowerLimit,&upperLimit);
printPrimes(lowerLimit,upperLimit);
return0;
/*Printallprimenumbersbetweenlowerlimitand upperlimit*/
while(lowerLimit<=upperLimit)
/*Printifcurrentnumber isprime*/
if(isPrime(lowerLimit))
{
printf("%d,",lowerLimit);
lowerLimit++;
/*Checkwhether anumberisprimeornot*/
int is Prime(intnum)
inti;
for(i=2;i<=num/2;i++)
/*Ifthenumberisdivisiblebyanynumber*/
/*otherthan1andselfthenitisnotprime*/
if(num%i==0)
return0;
return1;
}
OUTPUT:
1100
All prime number between 1 100 are 2,3,5,7,11,13, 17,19, 23,29,31, 37,41, 43,
47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97
RESULT:
Thus the C Program to find the prime numbers between two intervals has been
executed and verified.
EX N O : 7
PROGRAM TO REVERSE A SENTENCE USING RECURSION
DATE:
AIM:
ALGORITHM:
Step1: Start
Step2:Declare the function reverse
#include<stdio.h>
void reverseSentence();
int main(){
printf("Enterasentence:");
reverseSentence();
return0;
}
Void reverseSentence()
{char c;
scanf("%c",&c);
if (c != '\n') {
reverseSentence();
printf("%c", c);
}
}
OUTPUT:
Enter a sentence: margorpawesome
Awesome program
RESULT
Thus the C Program to reverse a sentence has been executed and verified
PROGRAM TO PERFORM ADDITION OPERATION USING
EXN O : 8 a POINTER
DATE:
AIM:
To add two numbers using pointers and display the result
ALGORITHM:
Step1:Start the program
Step2:Declare two variables x , y and pointer variable p and q
Step3:Assign the address of x and y to p and q
Step4:Assign the sum of x and y to variable sum
Step5:Stop the program
PROGRAM:
#include<stdioh>
int main()
{
int first,second,*p,*q,sum;
printf(“Enter two integers to add\n”);
scanf(“%d%d”,&first,&second);
p=&first;
q=&second;
sum=*p + *q;
srintf(“Sum of entered numbers=”%d\n”,sum);
return 0;
}
OUTPUT:
Enter two integers to add
12
13
Sum of entered numbers=25
RESULT:
Thus the c program to add two numbers using pointer concept has been executed and
verified successfully.
EX.NO:8b
AIM:
To set a pointer values to the corresponding array of integers
ALGORITHM:
Step1:Start the program
Step2:Declare and set an array of five integers
Step3:Declare an array of five pointers to integers
Step4:Set a pointer to point a corresponding integer
Step5:Print the values of integers pointed to by the pointers
Step6:Stop the program
PROGRAM:
#include<stdio.h>
const int ARRAY_SIZE=5;
int main()
{
int array_of_integers[]={5,10,20,40,80};
int i,*array_of_pointers[ARRAY_SIZE];
for(i=0;i<ARRAY_SIZE;i++)
{
array_of_pointers[i]=&array_of_integers[i];
}
for(i=0;i<ARRAY_SIZE;i++)
{
printf(“array_of_integers[%d]=%d\n”,i,*array_of_pointers[i]);
}
return 0;
}
OUTPUT
array_of_integers[0]=5
array_of_integers[1]=10
array_of_integers[2]=20
array_of_integers[3]=40
array_of_integers[4]=80
RESULT:
Thus the c program to set a pointer values to the corresponding array of integer has been
executed and verified successfully.
EX.NO:5b
AIM:
ALGORITHM:
Step1:Start
Step2:Get the two Strings to be concatenated.
Step3: Declare a new String to store the concatenated String.
Step4: Insert the firststring in the new string.
Step5:Insert the second string in the new string.
Step6:Print the concatenated string.
Step7:Stop
PROGRAM:
#include <stdio.h>
#include <string.h>
Int main()
{
char destination[] = "Hello ";
char source[]="World!";
printf("Concatenated String: %s\n", strcat(destination,source));
return0;
}
OUTPUT:
Concatenated String:HelloWorld!
RESULT:
Thus the C Program to concatenate two strings has been executed and the result was
verified
EXN O : 8 B
AIM:
ALGORITHM:
#include <stdio.h>
int main(){
char str[1000], ch;
int count=0;
awesome.
Frequency of e=4
RESULT:
Thus the C Program to find the frequency of a character in a string has been executed
and verified.
EX N O : 9 a
PROGRAM TO STORE STUDENT INFORMATION IN STRUCTURE
DATE: AND DISPLAY IT
AIM:
ALGORITHM:
Step1:Start
Step2:Read student details like name,mark1,2,3
float average;
char name[10],grade;
};
Void struct_funct_student(struct student
stu);
int main()
{
Struct student stud;
printf("\nRoll No.=");
scanf("%d",&stud.roll_no);
printf("Name=");
scanf("%s",stud.name);
printf("Mark1=");
scanf("%d",&stud.mark1);
printf("Mark2=");
scanf("%d",&stud.mark2);
printf("Mark3=");
scanf("%d",&stud.mark3);
struct_funct_student(stud)
;return0;
}
voidstruct_funct_student(struct student stu)
{
stu.total = stu.mark1 + stu.mark2 + stu.mark3;
stu.average=stu.total/3;
if(stu.average>= 90)
stu.grade='S';
else if(stu.average>= 80)
stu.grade='A';
else if(stu.average>= 70)
stu.grade='B';
else if(stu.average>= 60)
stu.grade='C';
else if(stu.average>= 50)
stu.grade='D';
else
stu.grade='F';
printf("\n ROLL NO. \t NAME \t TOTAL \t AVG \
tGRADE\n");
printf("%d\t%s\t%d\t%f\t
%c",stu.roll_no,stu.name,stu.total,stu.average,stu.gr
ade);
}
OUTPUT:
RollNo.= 1
Name=a
Mark1=95
Mark2=94
Mark3=96
1 A 285 95.000000 S
RESULT:
Thus the C Program to store and display student details using structures has been
executed and the result was verified.
EX N O : 9 b
PROGRAM TOREAD THE STUDENT DATA AND CALCULATE
DATE: THE TOTAL MARKS
AIM:
ALGORITHM:
PROGRAM:
#include<stdio.h>
struct student
int
sub1;
int
sub2;
int
sub3;
int
sub4;
int
sub5;
};
voidmain()
{
int i,total=0;
clrscr();
for(i=0;i<=9;i++)
printf("\nEnterMarksinFiveSubjects=");
scanf("%d%d%d",&s[i].sub1,&s[i].sub2,&s[i].sub3,&s[i].sub4,&s[i].sub5);
total=s[i].sub1+s[i].sub2+s[i].sub3+s[i].sub4+s[i].sub5;
printf("\nTotalmarksofs[%d]Student=%d",i,total);
getch();
}
OUTPUT:
80 70 90 80 98
Thus the C Program to print the student details has been executed and the result was
verified.
EXNO:10a
AIM:
To insert, update, delete and append telephone details of an individual or a company into a
telephone directory using random access file.
ALGORITHM:
Step1:Create a random access file
Step2:call the respective procedure to insert, update, delete or append based on user choice
Step3: Access the random access file to make the necessary changes and save
PROGRAM
#include "stdio.h"
#include "string.h"
#include<stdlib.h>
#include<fcntl.h>
Struct dir
{
char name[20];
char number[10];
};
int record = 0;
int main(void) {
intchoice=0;
FILE*fp=fopen("telephone.dat","rb+");
if (fp == NULL ) perror ("Error opening file");
while(choice!=6)
{
printf("\n1 insert\t 2 update\n");
printf("3delete\t4display\n");
printf("5 search\t 6 Exit\n Enter choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: insert(fp); break;
case 2: update(fp); break;
case3: del(fp);break;
case 4: display(fp);
break;
case 5: search(fp); break;
default:
}
}
fclose(fp);
return0;
}
Void insert(FILE*fp)
{
Void update(FILE*fp)
{
char name[20], number[10];
int result;
struct dir contact, blank;
printf("Enter name:");
scanf("%s", name);
rewind(fp);
while(!feof(fp))
{
result = fread(&contact, sizeof(structdir), 1, fp);
if(result!=0&&strcmp(name,contact.name) == 0)
{
printf("Enter number:");
scanf("%s", number);
strcpy(contact.number,number);
fseek(fp, -sizeof(structdir),
SEEK_CUR);fwrite(&contact,
sizeof(structdir), 1, fp);
printf("Updatedsuccessfully\n");
return;
}
}
printf("Recordnotfound\n");
}
Void del(FILE*fp)
{
char name[20], number[10];
intresult, record=0;
structdir contact, blank = {"", ""};
printf("Entername:");
scanf("%s", name);
rewind(fp);
while(!feof(fp))
{
result = fread(&contact, sizeof(structdir), 1, fp);
if(result!=0&&strcmp(name,contact.name)==0)
{
fseek(fp, record*sizeof(structdir),
SEEK_SET);fwrite(&blank,sizeof(structdir),
1,fp);
printf("%d Deleted successfully\n", record-1);
return;
}
record++;
}
printf("notfoundin %drecords\n",record);
}
Void display(FILE*fp)
{
Struct dir
contact;
int result;
rewind(fp);
printf("\n\n Telephone directory\n");
printf("%20s %10s\n", "Name", "Number");
printf("*******************************\
n");while(!feof(fp))
{
result = fread(&contact, sizeof(structdir), 1, fp);
if(result != 0 &&strlen(contact.name) > 0)
printf("%20s%10s\n",contact.name,contact.number);
}
printf("*******************************\n");
}
voidsearch(FILE*fp)
{
Struct dir contact;
int result;
char name[20];
rewind(fp);
printf("\nEnter name:");
scanf("%s",name);
while(!feof(fp))
{
result = fread(&contact, sizeof(structdir), 1, fp);
if(result!=0 &&strcmp(contact.name,name)== 0)
{
printf("\n%20s %10s\n",contact.name, contact.number);
return;
}
}
printf("Recordnotfound\n");
}
OUTPUT:
1insert 2update
3delete 4display
5search 6Exit
Enter choice:
4Telephonedirector
y
Name Number
******************************
*bb 11111
*******************************
1insert 2 update
3delete 4display
5search 6Exit
Enter choice:
5Entername:bb
bb 11111
1insert 2 update
3delete 4display
5search 6Exit
Enterchoice:1
Enter individual/company
name:aaEntertelephonenumber:222
222
1insert 2 update
3delete 4display
5search 6Exit
Enter choice:
2Entername:aa
Enter number:
333333Updated
successfully
1insert 2 update
3delete 4display
5search 6
ExitEnterchoice:
Telephonedirectory
Name Number
******************************
*bb 11111
aa 333333
******************************
*1insert 2 update
3delete 4display
5search 6Exit
Enterchoice:3
Entername:aa
1 Deleted successfully
1insert 2update
3delete 4display
5search 6Exit
Enterchoice:4
Telephonedirectory
Name Number
******************************
*bb 11111
*******************************
1insert 2 update
3delete 4display
5search 6Exit
Enterchoice:6
RESULT:
AIM:
To count the number of account holders whose balance is less than the minimum
Balance using sequential access file.
ALGORITHM:
#include <stdio.h>
Void insert();
Void count();
int main(void)
{
int choice=0;
while(choice!=3)
{
printf("\n1insertrecords\n");
printf("2 Count min balance holders\n");
printf("3Exit\n");
printf("Enter choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: insert();
break;
case2:count();
break;
}
}
}
Void insert()
{
Unsigned int
account,i;
Char name[30];
doublebalance;
FILE*cfPtr;
if((cfPtr=fopen("clients.dat", "w"))==NULL)
{
puts("File couldnotbeopened");
}
else{
int records,i=0;
printf("Enter the No. of records ");
scanf("%d",&records);
while(i<records)
{
printf("Entertheaccount, name,andbalance.");
scanf("%d%29s%lf",&account,name,&balance);
fprintf(cfPtr,"%d%s%.2f\n",account,name,balance);
i++;
}
fclose(cfPtr);
}
}
Void count()
{
Unsigned int
account;
char name[30];
doublebalance;
floatminBal = 5000.00;
int count=0;
FILE*cfPtr;
if((cfPtr=fopen("clients.dat","r"))==NULL)
printf("Filecouldnot be opened");
else
{
printf("%-10s%-13s%s\n","Account","Name","Balance");
fscanf(cfPtr,"%d%29s%lf",&account,name,&balance);
while(!feof(cfPtr))
{
if(balance<minBal)
{
printf("%-10d%-13s%7.2f\
n",account,name,balance);count++;
}
fscanf(cfPtr,"%d%29s%lf",&account,name,&balance);
}
fclose(cfPtr);
printf("Thenumberofaccountholderswhosebalanceislessthantheminimumbalance:
%d",count);
}
}
OUTPUT:
1 Insert records
2 Count min balance
holders
3 Exit
Enter choice:1
Enter the No.ofrecords2
Enter the account, name, and balance.1001 A 10000
Entertheaccount,name,andbalance.1002 B 300
1 Insert records
2 Count min balance
holders
3 Exit
Enterchoice:2
AccountName
Balance1002 B
300.00
The number of account holders whose balanceis less than the minimum balance:
1 insertrecords
2 Count min balance holders
3 Exit
Enterchoice:
*
RESULT:
Thus the C Program to count the number of account holders whose balance is less
than the minimum balance using sequential access file