Est102 Lab Manual
Est102 Lab Manual
SEMESTER : II
Prepared by Approved by
Signature
Vimal Sankar
Leena Y
Name Asst. Professor
HOD/CS
Dept of CSE
Date 16-12-2019 16-12-2019
1
LAB MANUAL
SUB CODE & NAME: EST102
PROGRAMMING IN C LAB
LIST OF EXPERIMENTS
Cycle I
1. Write a C program to read two numbers, add them and display their sum
2. Write a C program to read the radius of a circle, calculate its area and display
it using macro constant
3. Write a C program to read 3 integer values and find the largest among them
4. Write a C program to read a Natural Number and check whether the number is
prime or not
5. Write a C program to read a three-digit Number and check whether the
number is Armstrong or not
Cycle II
6. Write a C program to read n integers, store them in an array and search for an
element in the array using an algorithm for Linear Search
7. Write a C program to read n integers, store them in an array and sort the
elements in the array using Bubble Sort algorithm
8. Write a C program to read a string (word), store it in an array and check
whether it is a palindrome word or not.
9. Write a C program to read two strings (each one ending with a $ symbol),
store them in arrays and concatenate them without using library functions.
10. Write a C program using structure, read and print data of n employees (Name,
Employee Id and Salary)
Cycle III
11. Write a C program to find the factorial of a given Natural Number n using
recursive function.
12. Write a C program to find the product of two matrices
13. Write a C program to swap the values of two variables using pointers.
14. Create a file and perform the following
i. Write data to the file
ii. Read the data in a given file & display the file content on console
iii. append new data and display on console
15. Open a text input file and count number of characters, words and lines in it;
and store the results in an output file.
2
INDEX
Sl No Program Page No
2 Area of a circle 6
4 Prime or not 10
6 Linear search 12
7 Bubble sort 14
8 Palindrome string 16
3
Exp No: 1
Date:
Aim:
To write a C program to add two numbers
Algorithm:
Step1: start
Step2: input a,b
Step3: c=a+b
Step4: output c
Step5: stop
Flowchart:
4
Program:
#include<stdio.h>
void main ( )
{
int a,b;
printf("\nEnter two numbers:\n");
scanf("%d %d",&a,&b);
printf("\nThe sum = %d",a+b);
}
Output:
5
Exp No: 2
Date:
Area of circle
Algorithm:
Step1: start
Step2: input radius
Step3: area = 3.14 x radius x radius
Step4: output area
Step5: stop
Flowchart:
6
Program:
#include<stdio.h>
#define pi 3.14
void main()
{
int r;
float a;
printf("\nEnter the radius:\n");
scanf("%d",&r);
a=pi*r*r;
printf("\nArea = %f",a);
}
Output:
7
Exp No: 3
Date:
Algorithm:
Step1 : start
Step2 : input a,b,c
Step3 : if a>b goto step 4 else goto step9
Step4 : if a>c goto 5 else goto step7
Step5 : output a as largest
Step6 : goto step
Step7 : output c as largest
Step8 : goto step
Step9 : if b>c goto 10 else goto step12
Step10 : output b as largest
Step11 : goto step
Step12 : output c as largest
Step13 : goto step
Step14 : stop
Flowchart:
8
Program:
#include<stdio.h>
void main()
{
int a,b,c;
printf("\nEnter 3 No:s ");
scanf("%d %d %d",&a,&b,&c);
if(a>b)
{
if(a>c)
{
printf("\n%d is largest",a);
}
else
{
printf("\n%d is largest",c);
}
}
else
{
if(b>c)
{
printf("\n%d is largest",b);
}
else
{
printf("\n%d is largest",c);
}
}
}
Output:
9
Exp No: 4
Date:
Prime or not
Program:
#include<stdio.h>
void main()
{
int i=2,n,f=0;
printf("\nEnter a number: ");
scanf("%d",&n);
while(i<=n/2)
{
if(n%i==0)
{
printf("\n%d is not a prime number:",n);
f++;
break;
}
i++;
}
if(f==0)
{
printf("\n%d is a prime number:",n);
}
}
Output:
10
Exp No: 5
Date:
Program:
#include<stdio.h>
void main()
{
int n,s=0,x,d;
printf("\nEnter a three digit number: ");
scanf("%d",&n);
x=n;
while(n>0)
{
d=n%10;
s=s+d*d*d;
n=n/10;
}
if(s==x)
{
printf("\n%d is armstrong",x);
}
else
{
printf("\n%d is not armstrong",x);
}
}
Output:
11
Exp No: 6
Date:
Linear search
Program:
#include<stdio.h>
void main()
{
int a[10],n,x,i,flag=0;
printf("\nEnter the limit of the array:");
scanf("%d",&n);
printf("\nEnter array elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nEnter the search key: ");
scanf("%d",&x);
for(i=0;i<n;i++)
{
if(a[i]==x)
{
flag=1;
break;
}
}
if(flag==0)
{
printf("\n%d is not present in the array:",x);
}
else
{
printf("\n%d is present at %d",x,i+1);
}
}
12
Output:
13
Exp No: 7
Date:
Bubble sort
Program:
#include<stdio.h>
void main()
{
int a[30],n,i,j,x;
printf("\nEnter the limit:");
scanf("%d",&n);
printf("\nEnter %d values:",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(a[j]>a[j+1])
{
x=a[j];
a[j]=a[j+1];
a[j+1]=x;
}
}
}
printf("\nThe sorted list is:");
for(i=0;i<n;i++)
{
printf("\n%d",a[i]);
}
}
14
Output:
15
Exp No: 8
Date:
Palindrome string
Program:
#include<stdio.h>
#include<string.h>
void main()
{
char str1[20],str2[20];
printf("\nEnter a string: ");
scanf("%s",str1);
strcpy(str2,str1);
strrev(str1);
if(strcmp(str1,str2)==0)
{
printf("\n%s is palindrome",str2);
}
else
{
printf("\n%s is not palindrome",str2);
}
}
Output:
16
Exp No: 9
Date:
Program:
#include<stdio.h>
void main()
{
char str1[20],str2[20],ch;
int i=0,x;
printf("\nEnter the string1 ending with $: ");
do
{
scanf("%c",&ch);
str1[i]=ch;
i++;
}while(ch!='$');
x=--i;
i=0;
scanf("%c",&ch);
printf("\nEnter the string2 ending with $: ");
do
{
scanf("%c",&ch);
str2[i]=ch;
i++;
}while(ch!='$');
printf("\nThe string1 is:");
for(i=0;str1[i]!='$';i++)
{
printf("%c",str1[i]);
}
printf("\nThe string2 is:");
for(i=0;str2[i]!='$';i++)
{
printf("%c",str2[i]);
}
i=0;
while(str2[i]!='$')
{
str1[x]=str2[i];
i++;
x++;
}
str1[x]=str2[i];
17
printf("\nThe concatenated string is:");
i=0;
while(str1[i]!='$')
{
printf("%c",str1[i]);
i++;
}
}
Output:
18
Exp No: 10
Date:
Program:
#include<stdio.h>
struct employee
{
int eid;
char name[20];
float salary;
};
void main()
{
int n,i;
struct employee e[20];
printf("\nEnter the no of employees: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter eid of Employee%d: ",i+1);
scanf("%d",&e[i].eid);
printf("\nEnter name of Employee%d: ",i+1);
scanf("%s",e[i].name);
printf("\nEnter salary of Employee%d: ",i+1);
scanf("%f",&e[i].salary);
}
printf("\nEid\tName\t\tSalary");
for(i=0;i<n;i++)
{
printf("\n%d\t%s\t\t%f",e[i].eid,e[i].name,e[i].salary);
}
}
19
Output:
20
Exp No: 11
Date:
Program:
#include<stdio.h>
int fact(int);
void main()
{
int x,n,f=1;
printf("\nEnter a number:");
scanf("%d",&n);
printf("\nFactorial=%d",fact(n));
}
int fact(int x)
{
if(x==1)
return 1;
else
return(x*fact(x-1));
}
Output:
21
Exp No: 12
Date:
Program:
#include<stdio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],m,n,p,q,i,j,k;
printf("\nEnter the size of first Matrix: ");
scanf("%d %d",&m,&n);
printf("\nEnter the size of second Matrix: ");
scanf("%d %d",&p,&q);
if(n!=p)
{
printf("\nMatrix multiplication not possible: ");
}
else
{
printf("\nEnter %d elements to mat1:\n",m*n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nEnter %d elements to mat2:\n",p*q);
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
22
}
}
printf("\nThe 1st matrix is:");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
}
printf("\nThe 2nd matrix is:");
for(i=0;i<p;i++)
{
printf("\n");
for(j=0;j<q;j++)
{
printf("%d\t",b[i][j]);
}
}
printf("\nThe product matrix is:");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<q;j++)
{
printf("%d\t",c[i][j]);
}
}
}
}
23
Output:
24
Exp No: 13
Date:
Program:
#include<stdio.h>
void main()
{
int x,y,*xp,*yp,temp;
printf("\nEnter two numbers: ");
scanf("%d %d",&x,&y);
xp=&x;
yp=&y;
printf("\nThe numbers before swap: ");
printf("\nx=%d,y=%d ",*xp,*yp);
temp=*xp;
*xp=*yp;
*yp=temp;
printf("\nThe numbers after swap: ");
printf("\nx=%d,y=%d ",*xp,*yp);
}
Output:
25
Exp No: 14
Date:
Program:
#include<stdio.h>
void main()
{
FILE *fptr;
char ch,x;
printf("\nEnter a char: ");
scanf("%c",&ch);
fptr=fopen("test1.txt","w");
putc(ch,fptr);
fclose(fptr);
fptr=fopen("test1.txt","r");
x=getc(fptr);
printf("\nThe char from file: %c",x);
fclose(fptr);
}
Output:
26
Exp No: 15
Date:
Program:
#include<stdio.h>
void main()
{
FILE *fp;
char name[10],title[10];
int age,limit,i;
char gender;
fp=fopen("data2.txt","w");
printf("\nEnter the limit: ");
scanf("%d",&limit);
for(i=0;i<limit;i++)
{
printf("\nEnter the name,age,gender of person%d: ",i+1);
scanf("%s %d %c",name,&age,&gender);
fprintf(fp,"%s\t %d\t %c\n",name,age,gender);
}
fclose(fp);
fp=fopen("data2.txt","r");
printf("\nData from file:\n");
while(fscanf(fp,"%s %d %c",name,&age,&gender)!=EOF)
{
if(gender=='f')
{
printf("\nName: %s\nAge: %d\nGender:
%c",name,age,gender);
}
}
fclose(fp);
}
27
Output:
28