Documente Academic
Documente Profesional
Documente Cultură
Lab Manual
LABORATORY MANUAL
CS6212
PROGRAMMING AND DATA STRUCTURES I
LABORATORY
(R2013 Regulations)
Prepared By
Approved By
E.Sivaraman
Dr.S.V.Sudha
AP/CSE
HoD/CSE
Dept of CSE
Lab Manual
EX.NO
PAGE NO
C PROGRAMMING
1
Check whether the number and its reverse are same or not
Fibonacci series
10
11
12
13
14
15
16
17
ii
Convert the C to F
ii
ii
18
19
20
21
22
ii
23
Dept of CSE
Lab Manual
24
Transpose of a Matrix
25
26
27
28
29
Count a character that appears number of times in a given text using while loop
30
31
32
33
34
35
36
37
38
Perfect number
39
40
Substring in string
41
N nos divisible by 5
42
43
44
45
46
47
48
49
50
51
52
Dept of CSE
53
54
Lab Manual
Bubble Sort
ii
Quick Sort
iii
Merge Sort
Linear Search
ii
Binary Search
55
CONTENT BEYOND SYLLABI
i
ii
56
Dept of CSE
Lab Manual
C PROGRAMS
Dept of CSE
Lab Manual
Ex.No: 1
AIM
To write a C program to find sum of the digits of a given number.
PROCEDURE
Step 1: Start the program.
Step 2: Input number from the user.
Step 3: Using While Loop get each digit of the number and add to sum variable.
Step 4: Display the sum of the digits.
Step 5: Stop the program
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int num,sum=0,remainder;
clrscr();
printf("Enter a number: ");
scanf("%d",&num);
while(num)
{
remainder=num%10;
num=num/10;
sum=sum+remainder;
}
printf("\nSum of digits of number: %d",sum);
getch();
}
OUTPUT
RESULT
Thus C program to find the sum of the digits of a given number was written and output is verified
successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
Dept of CSE
Lab Manual
Ex.No: 2
AIM
To write a C program to reverse the digits of a given number.
PROCEDURE
Step 1: Start the program.
Step 2: Input the number from the user.
Step 3: Using while loop get the digits from units place and sum it.
Step 4: Display the Reversed Number.
Step 5: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int n, reverse = 0;
clrscr();
printf("Enter a number to reverse\n");
scanf("%d",&n);
while (n != 0)
{
reverse = reverse * 10;
reverse = reverse + (n%10);
n = n/10;
}
printf("Reverse of entered number is = %d\n", reverse);
getch();
}
OUTPUT:
RESULT:
Thus the C program to reverse a given number was written, executed and output is verified
successfully.
Dept of CSE
Lab Manual
Ex.No: 3
AIM
To write a C program to display all combinations of a 4-Digit Number.
PROCEDURE
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,l;
clrscr();
for(i=1; i<5; i++)
{
for(j=1; j<5; j++)
{
for(k=1; k<5; k++)
{
for(l=1; l<5; l++)
{
if(!(i==j || i==k || i==l || j==k || j==l || k==l))
printf("%d%d%d%d\n",i,j,k,l);
}
}
}
}
}
Dept of CSE
Lab Manual
OUTPUT:
RESULT
Thus a C program to display all combinations of 4 digit number was written, executed and output is
verified successfully.
Dept of CSE
Lab Manual
Ex.No:4
AIM
To write a C program to find largest digit of a given number.
PROCEDURE
Step 1: Start the program.
Step 2: Input number from user.
Step 3: Using While Loop get the digits and check whether digit is greater.
Step 4: If condition is false continue with while loop.
Step 5: Display the largest digit.
Step 6: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int n,max=0,rem;
clrscr();
printf("\nEnter a number:");
scanf("%d",&n);
while(n!=0)
{
rem=n%10;
n=n/10;
if(rem>max)
max=rem;
}
printf("\nThe Largest Digit is:%d",max);
getch();
}
OUTPUT
RESULT
Thus the C program to find largest digit of a number was written, executed & output is verified
successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
10
Dept of CSE
Lab Manual
Ex.No:5
AIM
To write a C program to check whether a number & its reverse are same or not.
PROCEDURE
11
Dept of CSE
Lab Manual
OUTPUT:
RESULT
Thus C program to check whether the given number is palindrome or not was written, executed &
output is verified successfully.
12
Dept of CSE
Ex.No: 6
Lab Manual
AIM
To write a C program to check whether given number is Armstrong or not.
PROCEDURE
Step 1: Start the program.
Step 2: Input the number from the user.
Step 3: Assign the number to temporary variable. Using while loop get the digits and find its
cube and add to sum variable.
Step 4: Check whether the original number and the sum of the cube of digits are same if true
display the given number is ARMSTRONG or else NOT AN ARMSTRONG.
Step 5: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int number, sum = 0, temp, remainder;
printf("Enter a number:\n");
scanf("%d",&number);
temp = number;
while( temp != 0 )
{
remainder = temp%10;
sum = sum + remainder*remainder*remainder;
temp = temp/10;
}
if ( number == sum )
printf("\nEntered number is an armstrong number.");
else
printf("\nEntered number is not an armstrong number.");
getch();
}
13
Dept of CSE
Lab Manual
OUTPUT
RESULT
Thus C program to check given number s Armstrong or not was written, executed & output is
verified successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
14
Dept of CSE
Lab Manual
Ex.No:7
AIM
To write a C program to find the sum of even & odd digits of a number.
PROCEDURE
Step 1: Start a program.
Step 2: Input number as string from the user.
Step 3: Using While Loop check whether the string as reached the NULL value if not check
whether the position is even or odd, if even sum it to evendigitsum variable or else sum it to
odddigitsum variable.
Step 4: Display the odd sum value & even sum value.
Step 5: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int odddigitsum=0,evendigitsum=0,i=0;
char n[50]={0};
clrscr();
printf("\nEnter a no:");
scanf("%s",n);
while(n[i]!='\0')
{
if(i%2==0)
odddigitsum=odddigitsum+(n[i]-48);
else
evendigitsum=evendigitsum+(n[i]-48);
++i;
}
printf("\nThe odd digits sum is:%d & even digits sum is:%d", odddigitsum, evendigitsum);
getch();
}
15
Dept of CSE
Lab Manual
OUTPUT
RESULT
Thus C program to find sum of Odd & even digits of a number was written, executed & output is
verified successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
16
Dept of CSE
Lab Manual
Ex.No:8
AIM
To write a C program to find second highest number from an array.
PROCEDURE
Step 1: Start the program.
Step 2: Input an array from the user.
Step 3: Sort the array using For Loop.
Step 4: Display the second highest number by using a[n-2].
Step 5: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int n,a[20],i,j,temp;
clrscr();
printf("\nEnter the no of elements:");
scanf("%d",&n);
printf("\nEnter the elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
/*SORTING*/
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\nThe 2nd Largest element in the given array is:%d",a[n-2]);
17
Dept of CSE
Lab Manual
getch();
}
OUTPUT
RESULT
Thus a C program to find 2nd largest element in an array was written, executed & output is verified
successfully.
18
Dept of CSE
Lab Manual
Ex.No: 9
FIBONACCI SERIES
AIM
To write a C program to generate Fibonacci Series.
PROCEDURE
Step 1: Start the program.
Step 2: Initialize a=-1 & b=1. Input the limit from the user.
Step 3: Using For Loop generate series.
Step 4: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,i,n;
a=-1;
b=1;
printf("\nEnter limit to generate series");
scanf("%d",&n);
printf("\n FIBONACCI SERIES\n");
for(i=0;i<n;i++)
{
c=a+b;
a=b;
b=c;
printf("\t%d",c);
}
printf(\n);
getch();
}
OUTPUT
RESULT
Thus a C program to generate Fibonacci Series was written, executed & output is verified successfully.
19
Dept of CSE
Lab Manual
Ex.No:10
AIM
To write a C program to generate Prime Numbers between from 50 to 100.
PROCEDURE
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int j,count,i=50;
clrscr();
printf("\nThe prime nos between 50 & 100 are:\n");
while(i<=100)
{
count=0;
for(j=1;j<=i;j++)
{
if(i%j==0) //checking whether num is dvisible by j
count++;
}
if(count==2) //if num is divisible by 2 numbers,then it is prime
printf("%d ",i);
i++;
}
getch();
printf("\n\n");
}
20
Dept of CSE
Lab Manual
OUTPUT
RESULT
Thus C program to generate prime numbers between 50 & 100 was written, executed & output is
verified successfully.
21
Dept of CSE
Ex.No:11
Lab Manual
AIM
To write a C program to count number of times a digit is present in a number.
PROCEDURE
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a,temp,j=0,n,digit=0;
clrscr();
printf("\nEnter a no:");
scanf("%d",&a);
temp=a;
printf("\nEnter the digit to find its occurance:");
scanf("%d",&n);
while(temp!=0)
{
digit=temp%10;
temp=temp/10;
if(digit==n)
j=j+1;
}
printf("\nNumber of times %d occured in %d is:%d",n,a,j);
getch();
}
22
Dept of CSE
Lab Manual
OUTPUT
RESULT
Thus C program to count number of times a digit present in a no was written, executed and output is
verified successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
23
Dept of CSE
Lab Manual
Ex.No:12
AIM
To write a C program to interchange elements of one array with other without using 3rd array.
PROCEDURE
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],b[10],m,j,temp;
clrscr();
printf("\nEnter the no of elements for a & b:");
scanf("%d",&m);
printf("\nEnter the elements of array a:");
for(i=0;i<m;i++)
scanf("%d",&a[i]);
printf("\nEnter the elements of array b:");
for(i=0;i<m;i++)
scanf("%d",&b[i]);
for(i=0;i<m;i++)
{
temp=a[i];
a[i]=b[i];
b[i]=temp;
}
printf("\nThe array values of a after interchange:\n");
for(i=0;i<m;i++)
24
Dept of CSE
Lab Manual
printf("%d\t",a[i]);
printf("\nThe array values of b after interchange:\n");
for(i=0;i<m;i++)
printf("%d\t",b[i]);
getch();
}
OUTPUT
RESULT
Thus C program to interchange 2 array elements without using 3rd array was written, executed &
output is verified successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
25
Dept of CSE
Ex.No:13
Lab Manual
AIM
To write a C program to arrange the digits of a number in ascending order.
PROCEDURE
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[10],i,j,t,n,k=0,rev=0;
clrscr();
printf("\nEnter a number:");
scanf("%d",&n);
while(n>0)
{
arr[k++]=n%10;
n=n/10;
}
for(i=0;i<k-1;i++)
{
for(j=i+1;j<k;j++)
{
if(arr[i]>arr[j])
{
t=arr[i];
arr[i]=arr[j];
arr[j]=t;
}
26
Dept of CSE
Lab Manual
}
}
for(i=0;i<k;i++)
rev=(rev*10)+arr[i];
printf("The digits of a number arranged in ascending order is:%d\n",rev);
getch();
}
OUTPUT
RESULT
Thus C program to arrange digits of a number in ascending order was written, executed & output is
verified successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
27
Dept of CSE
Lab Manual
Ex.No:14
AIM
To write a C program to count the number of vowels in the given text
PROCEDURE
Step 1: Start the program.
Step 2: Input the line of text from the user.
Step 3: Using While Loop check whether the loop has reached the end of line. If not check
whether the line contains vowels such as a, e, i, o, u, if found then increment the count value.
Step 4: Display the count.
Step 5: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
char line[80];
int i=0,vowelcount=0;
clrscr();
printf("\nEnter the line of text:");
gets(line);
while(line[i++]!='\0')
{
if(line[i]=='a'||line[i]=='e'||line[i]=='i'||line[i]=='o'||line[i]=='u')
vowelcount++;
}
printf("\nThe number of vowels in the line of text is:%d",vowelcount);
getch();
}
OUTPUT
RESULT
Thus C program to count no of vowels in the given text was written, executed & output is verified
successfully.
28
Dept of CSE
Lab Manual
Ex.No:15
PRODUCT OF 2 MATIRCES
AIM
To write a C program to find product of 2 matrices.
PROCEDURE
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,m,n,a[3][3],b[3][3],c[3][3];
clrscr();
printf("\nEnter thr no of rows & columns of a & b matrices:");
scanf("%d%d",&m,&n);
printf("\nEnter the elements of A Matrix:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
printf("\nEnter the elements of B Matrix:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&b[i][j]);
}
printf("\nThe Matrix Mutiplication is:");
for(i=0;i<m;i++)
{
29
Dept of CSE
Lab Manual
for(j=0;j<n;j++)
{
c[i][j]=0;
for(k=0;k<m;k++)
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
}
printf("\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d\t",c[i][j]);
printf("\n");
}
getch();
}
OUTPUT
RESULT
Thus a C program to find the product of 2 matrices was written, executed & output is verified
successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
30
Dept of CSE
EX.No:16
Lab Manual
AIM
To write a C program to count number of words in the given text.
PROCEDURE
Step 1: Start the program.
Step 2: Input the line of text from the user.
Step 3: Using While Loop check each character of the line if a space is encountered, increment the
count value, continue this process till end of the line is encountered.
Step 4: Display the count value.
Step 5: Stop the program.
PROGRAM
#include <stdio.h>
#include<conio.h>
void main()
{
char s[50],ch;
int i=0,c=0;
clrscr();
printf("Enter any string : ");
do
{
ch=getchar();
s[i]=ch;
i++;
}while(ch!='\n');
s[i]='\0';
for(i=0;s[i]!='\0';i++){
if(s[i]==' '){
c++;
while(s[i]==' ')
i++;
}
}
printf("\n\nTotal words are %d",c+1);
getch();
}
31
Dept of CSE
Lab Manual
OUTPUT
RESULT
Thus C program to cunt no of words in the given line of text was written, executed & output is
verifed successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
32
Dept of CSE
Lab Manual
Ex.No:17
AIM
To write a C program to find sum & difference of 2 matrices.
PROCEDURE
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,m,n,a[3][3],b[3][3],c[3][3],d[3][3];
clrscr();
printf("\nEnter thr no of rows & columns of a & b matrices:");
scanf("%d%d",&m,&n);
printf("\nEnter the elements of A Matrix:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
printf("\nEnter the elements of B Matrix:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&b[i][j]);
}
printf("\nAddition of 2 matrices is:\n");
33
Dept of CSE
Lab Manual
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf("%d\t",c[i][j]);
}
printf("\n");
}
printf("\nSubraction of 2 matrices is:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
d[i][j]=a[i][j]-b[i][j];
printf("%d\t",d[i][j]);
}
printf("\n");
}
getch();
}
OUTPUT
RESULT
Thus C program to find addition & subtraction of 2 matrices was written, executed & output is
verified successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
34
Dept of CSE
Ex.No:18(i)
Lab Manual
AIM
To write a C program to find area & circumference of a circle.
PROCEDURE
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
float r,pi=3.14,area,ci;
printf("Enter the Radius of Circle:");
scanf("%f",&r);
area=pi*r*r;
ci=2*pi*r;
printf("\nThe Area of a circle is:%.2f",area);
printf("\nThe Circumference of a circle is:%.2f",ci);
getch();
}
OUTPUT
RESULT
Thus C program to find Area & Circumference of circle was written, executed & output is verified
successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
35
Dept of CSE
Ex.No:18(ii)
Lab Manual
CONVERT C TO F & F TO C
AIM
To write a C program to convert C to F & F to C.
PROCEDURE
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
float c,fa;
printf("CELCIUS -> FARENHEIT");
printf("\nEnter the degree in Celcius:");
scanf("%f",&c);
fa=((c*9)/5)+32;
printf("\nThe Degree in Farenheit is:%.2f",fa);
printf("\nFARENHEIT -> CELCIUS");
printf("\nEnter the degree in farenheit:");
scanf("%f",&fa);
c=((fa-32)*5)/9;
printf("\nThe Degree in Celcius is:%.2f",c);
getch();
}
36
Dept of CSE
Lab Manual
OUTPUT
RESULT
Thus C program to Convert C to F & F to C was written, executed & output is verified
successfully.
37
Dept of CSE
Lab Manual
Ex.No:19(i)
GREATEST OF 3 NUMBERS
AIM
To write a C program to find greatest of 3 numbers.
PROCEDURE
Step 1: Start the program.
Step 2: Input 3 values from user.
Step 3: Using If Else If Statement check which of 3 values is greater.
Step 4: Display the greatest value.
Step 5: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k;
printf("GREATEST OF 3 NOS");
printf("\nEnter 3 Values:");
scanf("%d%d%d",&i,&j,&k);
if(i>j && i>k)
printf("\n%d is Greatest",i);
else if(j>k)
printf("\n%d is Greatest",j);
else
printf("\n%d is Greatest",k);
getch();
}
OUTPUT
RESULT
Thus C program to find Greatest of 3 numbers was written, executed & output is verified
successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
38
Dept of CSE
Ex.No.19(ii)
Lab Manual
AIM
To write a C program to check whether the given year is leap year or not.
PROCEDURE
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int year;
printf("LEAP YEAR");
printf("\nEnter a year:");
scanf("%d",&year);
if(((year%4==0)&&(year%100!=0))||(year%400==0))
printf("%d is a LEAP YEAR",year);
else
printf("%d is not a LEAP YEAR",year);
getch();
}
OUTPUT
RESULT
Thus C program to check whether the given year is leap year or not was written, executed & output
is verified successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
39
Dept of CSE
Lab Manual
Ex.No:20(i)
AIM
To write a C program to find whether the given number is even or odd.
PROCEDURE
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();
printf("\nEnter a no:");
scanf("%d",&num);
if(num%2==0)
printf("\n%d is an even no",num);
else
printf("\n%d is an odd no",num);
getch();
}
OUTPUT
RESULT
Thus C program to check whether the given no is even or odd was written, executed & output is
verified successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
40
Dept of CSE
Lab Manual
Ex.No:20(ii)
AIM
To write a C program to find Square Root of a number.
PROCEDURE
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<math.h>
float sqroot(float m)
{
float i=0,j;
float x1,x2;
while( (i*i) <= m )
i+=0.1;
x1=i;
for(j=0;j<10;j++)
{
x2=m;
x2/=x1;
x2+=x1;
x2/=2;
x1=x2;
}
return x2;
}
void main()
{
float num,sq_root;
41
Dept of CSE
Lab Manual
printf("Enter a no:");
scanf("%f",&num);
//USING sqrt()
sq_root=sqrt(num);
printf("\nSquare Root of %.3f using sqrt () is : %.3f",num,sq_root);
//Using sqroot()
printf("\nSquare Root of %.3f using user defined function is : %.3f\n",num,sqroot(num));
}
OUTPUT
RESULT
Thus C program to find the square root of a number was written, executed & output is verified
successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
42
Dept of CSE
Lab Manual
Ex.No:21
AIM
To write a C program to check whether the given name is palindrome or not without using String
Function.
PROCEDURE
PROGRAM
#include <stdio.h>
#include<conio.h>
void main()
{
char str[50];
int len=0,i=0,j,flag=1;
printf("Enter a string: ");
gets(str);
while(str[i++]!='\0')
len++;
for(i=0,j=(len-1);i<len/2;i++,j--)
{
if(str[j]!=str[i])
{
flag=0;
break;
}
}
if(flag==1)
printf("String is a palindrome");
else
printf("String is not a palindrome");
43
Dept of CSE
Lab Manual
getch();
}
OUTPUT
RESULT
Thus C program to check whether the given name is palindrome or not was written, executed &
output is verified successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
44
Dept of CSE
Ex.No:22
Lab Manual
AIM
To write a C program to swap 2 values using function & without using 3rd variable.
PROCEDURE
PROGRAM
#include<stdio.h>
#include<conio.h>
void swap(int,int);
void main()
{
int i,j;
printf("Enter 2 numbers:");
scanf("%d %d",&i,&j);
printf("\nThe value of a & b before swap:%d %d",i,j);
swap(i,j);
getch();
}
void swap(int m,int n)
{
m=m+n;
n=m-n;
m=m-n;
printf("\nThe value of a & b after swap:%d %d",m,n);
}
45
Dept of CSE
Lab Manual
OUTPUT
RESULT
Thus C program to swap 2 values using functions and without using 3rd variable was written,
executed & output is verified successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
46
Dept of CSE
Lab Manual
AIM
To write a C program to find factorial of given number using Recursive function.
PROCEDURE
Step 1: Start the program.
Step 2: Declare a function fact().
Step 3: Input a number from the user.
Step 4: Pass the value to the function.
Step 5: If n!=1 then multiply & call the function repeatedly.
Step 6: Return the value to main().
Step 7: Display the factorial.
Step 8: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
int n;
clrscr();
printf("\nEnter the no to find factorial:");
scanf("%d",&n);
printf("\nThe Facorial of %d is:%d",n,fact(n));
getch();
}
int fact(n)
{
if(n==1)
return 1;
else
return(n*fact(n-1));
}
47
Dept of CSE
Lab Manual
OUTPUT
RESULT
Thus C program to find factorial of a no using recursive function was written, executed & output is
verified successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
48
Dept of CSE
Lab Manual
Ex.No:23(ii)
AIM
To write a C program to find factorial of a no without using recursive function.
PROCEDURE
RESULT
Thus the C program to find the factorial of a given number was written, executed & output is
verified successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
49
Dept of CSE
Lab Manual
Ex.No:24
TRANSPOSE OF A MATRIX
AIM
To write a C program to transpose a Matrix.
PROCEDURE
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],m,n,i,j;
printf("Enter the Row & Column Size of a matrix:");
scanf("%d %d",&m,&n);
printf("\nEnter the Elements of Matrix:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nThe original Matrix is:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
50
Dept of CSE
Lab Manual
}
//TANSPOSE
printf("\nThe Transpose of a matrix is:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[j][i]);
}
printf("\n");
}
getch();
}
OUTPUT
RESULT
Thus C Program to find transpose of a matrix was written, executed & output is verified
successfully.
51
Dept of CSE
Lab Manual
Ex.No:25
AIM
To write a C program to concat 2 strings & compare 2 strings without using built-in functions.
PROCEDURE
Step 1: Start the program.
STRING CONCATENATION
Step 2: Input 2 strings from the user.
Step 3: Using a For Loop assign the first string to another string variable.
Step 4: Using another For Loop append the second string to the 3rd string variable value.
Step 5: Display the concatenated string.
STRING COMPARISON
Step 6: Input 2 strings from the user.
Step 7: Using While Loop find the difference between 2 strings. If the difference is equal to 0
displays the strings are equal.
Step 8: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[25],str2[25],str3[25],str4[25];
int i=0,j=0,flag=0;
clrscr();
printf("\nString Concatenation");
printf("\nEnter First String:");
gets(str1);
printf("\nEnter Second String:");
gets(str2);
while(str1[i]!='\0')
i++;
while(str2[j]!='\0')
{
str1[i]=str2[j];
j++;
i++;
52
Dept of CSE
Lab Manual
}
str1[i]='\0';
printf("\nConcatenated String is %s",str1);
printf("\nString Comparison");
i=0;
printf("\nEnter First String:");
gets(str3);
printf("\nEnter Second String:");
gets(str4);
while(str3[i]!='\0' && str4[i]!='\0')
{
if(str3[i]!=str4[i])
{
flag=1;
break;
}
i++;
}
if (flag==0 && str3[i]=='\0' && str4[i]=='\0')
printf("\nStrings are Equal");
else
printf("\nStrings are not Equal");
getch();
}
OUTPUT
RESULT
Thus C program to compare &concatenate 2 strings was written, executed & output is verified
successfully.
53
Dept of CSE
Lab Manual
Ex.No:26
AIM
To write a C program to reverse a given string.
PROCEDURE
Step 1: Start the program.
Step 2: Display menu to reverse using string function & without function.
Step 3: Input the choice from user.
Step 4: If choice is 1, input the string from user & calculate its length. Using For Loop reverse
the string from the last position.
Step 5: if choice is 2, input the string from user & using strrev() reverse the string & display
it.
Step 6: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,ch;
char str1[10];
clrscr();
printf("\n1. Reverse without String Function.\n2. Reverse using String Function");
printf("\nenter a choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
printf("\nREVERSE A STRING WITHOUT STRING FUNCTION");
printf("\nEnter a string:");
scanf("%s",str1);
printf("\nThe Reverse of the given string is:");
for(i=strlen(str1)-1;i>=0;i--)
printf("%c",str1[i]);
break;
54
Dept of CSE
Lab Manual
}
case 2:
{
printf("\nREVERSE A STRING USING STRING FUNCTION");
printf("\nEnter a string:");
scanf("%s",str1);
strrev(str1);
printf("\nThe Reversed string is:%s",str1);
break;
}
}
getch();
}
OUTPUT
RESULT
Thus C program to reverse a string using string function & without using it was written & output is
verified successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
55
Dept of CSE
Lab Manual
Ex.No:27
AIM
To write a C program to count no of lines, vowels, consonants, words & lines of given text.
PROCEDURE
Step 1: Start the program.
Step 2: Input the line of text.
Step 3: Using For Loop count the values using Switch Case by using each character as choice.
Step 4: Display the count of lines, vowels, consonants & words of the text.
Step 5: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,len,s=0,w=1,c=0,v=0;
char s1[20],ch;
clrscr();
printf("\nEnter the Text:");
gets(s1);
len=strlen(s1);
for(i=0;i<=len-1;i++)
{
ch=s1[i];
switch(ch)
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'a':
case 'e':
case 'i':
56
Dept of CSE
Lab Manual
case 'o':
case 'u':
v++;
break;
case '.':
s++;
break;
default:
c++;
}
if(s1[i]==' ' && s1[i-1]!=' ')
{
w++;
}
}
printf("\nOUTPUT:");
printf("\nThe number of vowels is: %d",v);
printf("\nThe number of consonants is: %d",c);
printf("\nThe number of words is: %d",w);
printf("\nThe number of lines is: %d",s);
getch();
}
OUTPUT
RESULT
Thus C program to count no of words, vowels, consonants & lines was written, executed & output is
verified is successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
57
Dept of CSE
Lab Manual
Ex.No:28
AIM
To write a C program to search an element in an array.
PROCEDURE
Step 1: Start the program.
Step 2: Input the no of elements & elements from the user.
Step 3: Input the no to be searched from the user.
Step 4: Using For Loop find the element in the array & display it.
Step 5: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,element,n;
clrscr();
printf("\nSEARCH AN ELMENT");
printf("\nEnter the limit:");
scanf("%d",&n);
printf("\nEnter the elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEnter the element ot be searched:");
scanf("%d",&element);
for(i=0;i<n;i++)
{
if(a[i]==element)
printf("\nThe Element %d is found in %d position",element,i+1);
}
getch();
}
58
Dept of CSE
Lab Manual
OUTPUT
RESULT
Thus C program to search an element in an rray was written, executed & output is verified
successfully.
59
Dept of CSE
Ex.No:29
Lab Manual
AIM
To write a C program to count a character that appears number of times in a given text using while
loop.
PROCEDURE
Step 1: Start the program.
Step 2: Input the text & character to be counted from the user.
Step 3: Using While Loop, if character found increment the count value.
Step 4: Display the count.
Step 5: stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
char text[20],findchar;
int i=0,count=0;
clrscr();
printf("\nEnter the text:");
gets(text);
printf("\nEnter the Char to Count:");
findchar=getchar();
while(text[i]!='\0')
{
if(text[i]==findchar)
count++;
i++;
}
printf("\nCharacter %c found in the String %s: %d times",findchar,text,count);
getch();
}
60
Dept of CSE
Lab Manual
OUTPUT
RESULT
Thus C program to find no of times a character found in the text was written, executed & output is
verified successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
61
Dept of CSE
Ex.No:30
Lab Manual
AIM
To simulate calculator using function.
PROCEDURE
Step 1: Start the program.
Step 2: Declare 5 arithmetic functions.
Step 3: Input 2 numbers from the user.
Step 4: Pass the values to the function & display the result in function.
Step 5: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
void add(int,int);
void sub(int,int);
void mul(int,int);
void div(int,int);
void rem(int,int);
void main()
{
int a,b;
clrscr();
printf("\nEnter 2 nos:");
scanf("%d%d",&a,&b);
add(a,b);
sub(a,b);
mul(a,b);
div(a,b);
rem(a,b);
getch();
}
void add(int x,int y)
{
int z;
z=x+y;
printf("\nAddition of 2 nos is:%d",z);
62
Dept of CSE
Lab Manual
}
void sub(int x,int y)
{
int z;
z=x-y;
printf("\nSubraction of 2 nos is:%d",z);
}
void mul(int x,int y)
{
int z;
z=x*y;
printf("\nMultiplication of 2 nos is:%d",z);
}
void div(int x,int y)
{
float z;
z=x/y;
printf("\nDivision of 2 nos is:%f",z);
}
void rem(int x,int y)
{
int z;
z=x%y;
printf("\nModulus of 2 nos is:%d",z);
}
OUTPUT
RESULT
Thus C program to simulate calculator was written, executed & output is verified successfully.
63
Dept of CSE
Lab Manual
Ex.No:31
AIM
To write a C program to accept any single digit no & display it in words.
PROCEDURE
Step 1: Start the program.
Step 2: Input a single digit no from the user.
Step 3: Using Switch case display the corresponding number in words.
Step 4: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("\nEnter a single Digit No to show in words:\n");
scanf("%d",&a);
switch(a)
{
case 0:
printf("ZERO");
break;
case 1:
printf("ONE");
break;
case 2:
printf("TWO");
break;
case 3:
printf("THREE");
break;
case 4:
printf("FOUR");
break;
64
Dept of CSE
Lab Manual
case 5:
printf("FIVE");
break;
case 6:
printf("SIX");
break;
case 7:
printf("SEVEN");
break;
case 8:
printf("EIGHT");
break;
case 9:
printf("NINE");
break;
default:
printf("\nENTER A SINGLE DIGIT NO");
break;
}
getch();
}
OUTPUT
RESULT
Thus C program to display the entered no in words was written, executed & output is verified
successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
65
Dept of CSE
Ex.No:32
Lab Manual
AIM
To write a C program to accept a string in any case & convert it to Upper case.
PROCEDURE
Step 1: Start the program.
Step 2: Input a string from the user & display it.
Step 3: Using For Loop convert the letters to lower cases.
Step 4: Display the string in lower case.
Step 5: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
int main(){
char str[20];
int i;
clrscr();
printf("Enter any string:");
scanf("%s",str);
printf("The string is:%s",str);
for(i=0;i<=strlen(str);i++){
if(str[i]>=97&&str[i]<=122)
str[i]=str[i]-32;
}
printf("\nThe string in uppercase is:%s",str);
getch();
return 0;
}
OUTPUT
RESULT
Thus a C program to convert any case text to upper case was written, executed & output is verified
successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
66
Dept of CSE
Ex.No:33
Lab Manual
AIM
To write a C program to find largest & smallest element in an array.
PROCEDURE
Step 1: Start the program.
Step 2: Input the no of elements & the elements from the user.
Step 3: Using For Loop arrange the elements in ascending order.
Step 4: Assign a[0] to smallest & a[n-1] to largest variable.
Step 5: Display the Smallest & Largest number of the array.
Step 6: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,j,n,t,small,big;
clrscr();
printf("\nEnter no of elements:");
scanf("%d",&n);
printf("\nEnter the elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
small=a[0];
67
Dept of CSE
Lab Manual
big=a[n-1];
printf("\nThe Smallest element is:%d",small);
printf("\nThe Largest element is:%d",big);
getch();
}
OUTPUT
RESULT
Thus C program to find smallest & largest no of an array was written, executed & output is verified
successfully.
68
Dept of CSE
Ex.No:34
Lab Manual
AIM
To write a C program to find sum of 2 nos using function.
PROCEDURE
Step 1: Start the program.
Step 2: Declare an integer function.
Step 3: In main(), input 2 values from the user.
Step 4: Pass the values to function, add it in function and return the value to main().
Step 5: Display the sum value.
Step 6: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
int add(int,int);
void main()
{
int a,b,sum;
clrscr();
printf("\nEnter 2 nos:");
scanf("%d%d",&a,&b);
sum=add(a,b);
printf("\nThe addition of 2 nos is:%d",sum);
getch();
}
int add(int m,int n)
{
int p;
p=m+n;
return p;
}
OUTPUT
RESULT
Thus C program to add 2 numbers using function was written, executed & output is verified successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
69
Dept of CSE
Lab Manual
Ex.No:35
AIM
To write a C program to arrange the names in alphabetical order using arrays.
PROCEDURE
Step 1: Start the program.
Step 2: Input no of names & the names from the user.
Step 3: Using a For Loop assign the names to temp array.
Step 4: Using For Loop & strcmpi() compare and sort the names using strcpy().
Step 5: Display the sorted names.
Step 6: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[10][8], Tname[10][8], temp[8];
int i, j, N;
clrscr();
printf("Enter the Limit\n");
scanf("%d", &N);
printf("Enter %d names\n", N);
for(i=0; i< N ; i++)
{
scanf("%s",name[i]);
strcpy (Tname[i], name[i]);
}
for(i=0; i < N-1 ; i++)
{
for(j=i+1; j< N; j++)
{
if(strcmpi(name[i],name[j]) > 0)
{
strcpy(temp,name[i]);
70
Dept of CSE
Lab Manual
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
}
}
printf("\n----------------------------------------\n");
printf("Input Names\tSorted names\n");
printf("------------------------------------------\n");
for(i=0; i< N ; i++)
{
printf("%s\t\t%s\n",Tname[i], name[i]);
}
printf("------------------------------------------\n");
getch();
}
OUTPUT
RESULT
Thus C program to sort names of an array in alphabetical order was written, executed & output is
verified successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
71
Dept of CSE
Lab Manual
Ex.No:36
AIM
To write a C program to check whether given no is prime or not.
PROCEDURE
Step 1: Start the program.
Step 2: Input the no from the user.
Step 3: Using For Loop find the remainder & if remainder is 0 increment c. If c equals 2 display the
no is prime or else not a prime.
Step 4: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int i,m=0,j,n;
clrscr();
printf("\nenter the number to be checked:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
j=n%i;
if(j==0)
{
m=m+1;
}
}
if (m==2)
{
printf("\nthe given number is prime");
}
else
printf("\nthe given number is not prime");
getch();
}
72
Dept of CSE
Lab Manual
OUTPUT
RESULT
Thus C program to check whether given no is prime or not was written, executed & output is
verified successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
73
Dept of CSE
Ex.No:37
Lab Manual
AIM
To write a C program to generate the employee payroll using structures.
PROCEDURE
Step 1: Start the program.
Step 2: Declare a structure called emp.
Step 3: In main(), using structure variable input the name, designation, basic pay, TA, DA &
HRA.
Step 4: Calculate netpay & display the details.
Step 5: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct emp
{
char ename[10],desg[10];
int bpay,ta,da,hra,netpay;
}e;
void main()
{
clrscr();
printf("\nEMPLOYEE DEATILS");
printf("\nEnter Employee Name:");
scanf("%s",e.ename);
printf("\nEnter Designation:");
scanf("%s",e.desg);
printf("\nEnter the Basic Pay:");
scanf("%d",&e.bpay);
printf("\nEnter TA,DA & HRA:");
scanf("%d%d%d",&e.ta,&e.da,&e.hra);
e.netpay=e.bpay+e.ta+e.da+e.hra;
printf("\nEmployee Name:%s\nEmployee Designation:%s",e.ename,e.desg);
printf("\nBasic Pay:%d\nTA:%d\nDA:%d\nHRA:%d",e.bpay,e.ta,e.da,e.hra);
printf("\nNet Salaray is:%d",e.netpay);
74
Dept of CSE
Lab Manual
getch();
}
OUTPUT
RESULT
Thus C program to generate employee details using structure was written, executed & output is
verified successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
75
Dept of CSE
Lab Manual
Ex.No:38
PERFECT NUMBER
AIM
To write a C program to find whether a given number is a perfect number or not.
ALGORITHM
Step 1: Start the program.
Step 2: Get the number.
Step 3: Check whether the number is perfect number or not.
Step 4: If the number is perfect display it as perfect or else display it not a perfect number.
Step 5: Stop the program.
PROGRAM
#include<stdio.h>
int main()
{
int n,i=1,sum=0;
printf("Enter a number: ");
scanf("%d",&n);
while(i<n)
{
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%d is a perfect number",i);
else
printf("%d is not a perfect number",i);
return 0;
}
76
Dept of CSE
Lab Manual
OUTPUT
RESULT
Thus C program to find the given number is perfect or not was written, executed and output
is verified successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
77
Dept of CSE
Lab Manual
Ex.No:39
AIM
To write a C program to find area of 4 geometric shapes.
ALGORITHM
Step 1: Start the program.
Step 2: Declare 4 shape functions.
Step 3: In main() using switch case call fucntions to define area of 4 shapes.
Step 4: Display the area.
Step 5: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
void recarea();
void triarea();
void sqarea();
void cuarea();
void main()
{
int ch;
clrscr();
do
{
printf("\n1.Area of Rectangle\n2.Area of Triangle\n3.Area of Square\n4.Area of
Cube\n5.Exit");
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\tArea of Rectangle");
printf("\n\t=================");
recarea();
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
78
Dept of CSE
Lab Manual
break;
case 2:
printf("\n\tArea of Triangle");
printf("\n\t=================");
triarea();
break;
case 3:
printf("\n\tArea of Square");
printf("\n\t=================");
sqarea();
break;
case 4:
printf("\n\tArea of Cube");
printf("\n\t=================");
cuarea();
break;
case 5:
exit(0);
default:
printf("\nInvalid Choice");
}
}while(ch<=5);
getch();
}
void recarea()
{
int area,l,b;
printf("\nEnter the length and breadth of Rectangle:");
scanf("%d%d",&l,&b);
area=l*b;
printf("\nArea of Rectangle is:%d",area);
}
void triarea()
{
long float area,h,b;
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
79
Dept of CSE
Lab Manual
80
Dept of CSE
Lab Manual
RESULT
Thus C program to find area of 4 geometric shapes was written, executed and output is
verified successfully.
81
Dept of CSE
Lab Manual
Ex.No:40
SUBSTRING IN A STRING
AIM
To write a C program to find whether substring is in the string or not.
ALGORITHM
Step 1: Start the program.
Step 2: Get the string and substring.
Step 3: Check whether substring is in string or not.
Step 4: Display the result.
Step 5: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
char str[80],search[10];
int count1=0,count2=0,i,j,flag;
clrscr();
puts("Enter a string:");
gets(str);
puts("Enter search substring:");
gets(search);
while (str[count1]!='\0')
count1++;
while (search[count2]!='\0')
count2++;
for(i=0;i<=count1-count2;i++)
{
for(j=i;j<i+count2;j++)
{
flag=1;
if (str[j]!=search[j-i])
82
Dept of CSE
Lab Manual
{
flag=0;
break;
}
}
if (flag==1)
break;
}
if (flag==1)
puts("SEARCH SUCCESSFUL!");
else
puts("SEARCH UNSUCCESSFUL!");
getch();
}
OUTPUT
RESULT
Thus C program to check whether given substring is in string or not was written, executed
and output is verified successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
83
Dept of CSE
Lab Manual
Ex:No:41
N NUMBERS DIVISIBLE BY 5
AIM
To write a C program to display N numbers divisible by 5.
ALGORITHM
Step 1: Start the program.
Step 2: Get the limit.
Step 3: Based on the limit find the numbers divisible by 5.
Step 4: Display the result.
Step 5: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int i, num;
printf(\nEnter the limit:);
scanf(%d,&num);
printf("Integers divisible by 5 are \n");
for(i=1;i<=num;i++)
{
if (i % 5 == 0)
printf("%3d,", i);
}
}
OUTPUT
RESULT
Thus C program to display N numbers divisible by 5 was written, executed and output is
verified successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
84
Dept of CSE
Ex.No:42
Lab Manual
AIM
To write a C program to perform arithmetic operations using pointers and functions.
ALGORITHM
Step 1: Start the program.
Step 2: Declare required functions.
Step 3: In main(), call 4 functions by passing address of values got.
Step 4: In each function, perform arithmetic operation and display the results.
Step 5: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
void add(int *,int *);
void sub(int *,int *);
void mul(int *,int *);
void div(int *,int *);
void main()
{
int a,b;
printf("\nARITHMETIC OPERATIONS USING POINTERS AND FUNCTIONS");
printf("\nEnter 2 numbers:");
scanf("%d%d",&a,&b);
add(&a,&b);
sub(&a,&b);
mul(&a,&b);
div(&a,&b);
}
void add(int *x,int *y)
{
printf("\nAddition of 2 numbers is:%d",*x+*y);
}
void sub(int *x,int *y)
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
85
Dept of CSE
Lab Manual
{
printf("\nSubtraction of 2 numbers is:%d",*x-*y);
}
void mul(int *x,int *y)
{
printf("\nProduct of 2 numbers is:%d",*x**y);
}
void div(int *x,int *y)
{
printf("\nQuotient of 2 numbers is:%d",*x/(*y));
}
OUTPUT
RESULT
Thus C program to find perform arithmetic operations using pointers and functions was
written, executed and output is verified successfully.
86
Dept of CSE
Ex.No:43:
Lab Manual
AIM
To write a C program to copy a file and to append contents to a file.
ALGORITHM
Step 1: Start the program.
Step 2: Using file pointer copy one file content to another.
Step 3: Using another file pointer append contents to a file.
Step 4: Stop the program.
PROGRAM
#include <stdio.h>
#include <stdlib.h> // For exit()
void main()
{
FILE *fptr1, *fptr2, *fptr3, *fptr4;
char filename[100], c;
printf("\nFILE COPY\n");
printf("Enter the filename to open for reading \n");
scanf("%s", filename);
// Open one file for reading
fptr1 = fopen(filename, "r");
if (fptr1 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
printf("Enter the filename to open for writing \n");
scanf("%s", filename);
// Open another file for writing
fptr2 = fopen(filename, "w");
if (fptr2 == NULL)
{
printf("Cannot open file %s \n", filename);
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
87
Dept of CSE
Lab Manual
exit(0);
}
// Read contents from file
c = fgetc(fptr1);
while (c != EOF)
{
fputc(c, fptr2);
c = fgetc(fptr1);
}
printf("\nContents copied to %s", filename);
printf("\nFILE APPEND\n");
printf("Enter the file name to open for reading:\n");
scanf("%s",filename);
fptr3=fopen(filename,"r");
if (fptr3 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
printf("Enter the file name to open for appending:\n");
scanf("%s",filename);
fptr4=fopen(filename,"a");
if (fptr4 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
c=fgetc(fptr3);
while(c!=EOF)
{
fputc(c,fptr4);
c=fgetc(fptr3);
}
printf("\nContents appended to %s", filename);
fclose(fptr1);
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
88
Dept of CSE
Lab Manual
fclose(fptr2);
fclose(fptr3);
fclose(fptr4);
}
OUTPUT
RESULT
Thus C program to perform file copy and append operation was written, executed and
output is verified successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
89
Dept of CSE
Ex.No:44
Lab Manual
AIM
To write a C program to perform sequential file access.
ALGORITHM
Step 1: Start the program.
Step 2: Define a structure.
Step 3: Get the number of records.
Step 4: Based on number of records, write the content to a file and access the content
sequentially.
Step 5: Stop the program.
PROGRAM
#include <stdio.h>
typedef struct
{
int usn;
char name[25];
int m1,m2,m3;
}STD;
STD s;
void main()
{
int i,n,usn_key,opn;
FILE *fp;
printf(" How many Records ? ");
scanf("%d",&n);
fp=fopen("stud.dat","w");
for(i=0;i<n;i++)
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
90
Dept of CSE
Lab Manual
{
printf("Read the Info for Student: %d (usn,name,m1,m2,m3) \n",i+1);
scanf("%d%s%d%d%d",&s.usn,s.name,&s.m1,&s.m2,&s.m3);
fwrite(&s,sizeof(s),1,fp);
}
fclose(fp);
fp=fopen("stud.dat","r");
do
{
printf("Press 1- Display\t 2- Search\t 3- Exit\t Your Option?");
scanf("%d",&opn);
switch(opn)
{
case 1: printf("\n Student Records in the File \n");
display(fp);
break;
case 2: printf(" Read the USN of the student to be searched ?");
scanf("%d",&usn_key);
if(search(fp,usn_key))
{
printf("Success ! Record found in the file\n");
printf("%d\t%s\t%d\t%d\t%d\n",s.usn,s.name,s.m1,s.m2,s.m3);
}
else
printf("
Failure!!
Record
with
USN
%d
found\n",usn_key);
break;
case 3: printf(" Exit!! Press a key . . .");
getch();
break;
default: printf(" Invalid Option!!! Try again !!!\n");
break;
}
}while(opn != 3);
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
91
not
Dept of CSE
Lab Manual
fclose(fp);
}/* End of main() */
OUTPUT
RESULT
Thus C program to perform sequential file access was written, executed and output is
verified successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
92
Dept of CSE
Lab Manual
Ex.No:45
AIM
To write a C program to perform random file access.
ALGORITHM
Step 1: Start the program.
Step 2: Define a structure.
Step 3: Get the number of records.
Step 4: Based on number of records, write the content to a file and access the content
sequentially.
Step 5: Stop the program.
PROGRAM
#include<stdio.h>
struct emp
{
int empno;
char ename[20];
int sal;
long int phno;
};
int main()
{
struct emp x;
FILE *p;
int n,i,t,rec;
char choice[1];
p=fopen("employee","wb+"); /* opening the file in binary write/read mode*/
printf("How many records?");
scanf("%d",&n);
printf("Enter %d records:\n",n);
for(i=1;i<=n;i++)
{
printf("\n\nEmpno:");
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
93
Dept of CSE
Lab Manual
scanf("%d",&x.empno);
printf("Name:");
scanf("%s",x.ename);
printf("Salary:");
scanf("%d",&x.sal);
printf("Phone No:");
scanf("%ld",&x.phno);
fwrite(&x,sizeof(x),1,p);
}
printf("Press any key to continue....\n");
getch();
while(1)
{
printf("\nEnter the record number:");
scanf("%d",&rec);
if(rec<1||rec>n)
printf("Search failure...");
else
{
fseek(p,(rec-1)*sizeof(x),0); /* placing record pointer to the required record*/
fread(&x,sizeof(x),1,p);
printf("Empno:%d\nName:%s\nSalary:%d\nPhno:%ld",x.empno,x.ename,x.sal,x.ph
no); /* Printing the record*/
}
printf("\nWant to continue...y/n:"); /* accepting confirmation to continue*/
scanf("%s",choice);
if(strcmp(choice,"n")==0)
break;
}
fclose(p);
return 0;
}
94
Dept of CSE
Lab Manual
OUTPUT
RESULT
Thus C program to perform Random file access was written, executed and output is verified
successfully.
95
Dept of CSE
Lab Manual
DATA STRUCTURES
96
Dept of CSE
Lab Manual
ALGORITHM
Step 1: Start the program.
Step 2: Declare the functions to be performed.
Step 3: Declare the required variables.
Step 4: Get the size and choice from the user.
Step 5: Based on the choice perform the operations such as creation of list, inserting an
element, displaying the list, deleting an element & searching an element.
Step 6: Display the result based on the choice.
Step 7: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
void create();
void display();
void insert(int pos,int item);
void del(int pos);
void search(int item);
int arr[10],size;
void main()
{
int ch,item,pos;
clrscr();
printf("\nArray Implementation of List ADT\n");
print("================================");
printf("\nEnter the size of the array:");
scanf(%d,&size);
do
{
printf("\n1.Create\n2.Display\n3.Insert\n4.Delete\n5.Search");
printf("\nEnter the Choice:");
scanf(%d,&ch);
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
97
Dept of CSE
Lab Manual
switch(ch)
{
case 1:
printf("\nCreating a List:\n");
create();
break;
case 2:
printf("Display the List:\n");
display();
break;
case 3:
printf("\nInsert an item:\n");
printf("Enter the item and position:");
scanf(%d%d,&item,&pos);
insert(pos,item);
break;
case 4:
printf("\nDelete an item:\n");
printf("\nEnter the position:");
scanf(%d,&pos);
del(pos);
break;
case 5:
printf("\nSearch an Item:\n");
printf("\nEnter the item:");
scanf(%d,&item);
search(item);
break;
default:
printf("\nInvalid Choice");
}
}while(ch<=5);
getch();
}
98
Dept of CSE
Lab Manual
void create()
{
int k;
for(k=0;k<size;k++)
{
printf("\nEnter the item[%d]:",k+1);
scanf(%d,&arr[k]);
}
printf("\nList created successfully");
}
void display()
{
int j;
for(j=0;j<size;j++)
printf(%d\t,arr[j]);
}
void insert(int pos,int item)
{
int i;
for(i=size+1;i>pos;i--)
arr[i]=arr[i-1];
arr[i]=item;
printf("\nList after Insertion:\n");
for(i=0;i<size+1;i++)
printf(%d,arr[i]);
size++;
}
void del(int pos)
{
int i;
for(i=pos-1;i<size;i++)
arr[i]=arr[i+1];
size--;
printf("\nList after Deletion:\n");
for(i=0;i<size;i++)
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
99
Dept of CSE
Lab Manual
printf(%d\t,arr[i]);
}
void search(int item)
{
int i;
for(i=0;i<size;i++)
{
if(arr[i]==item)
printf(\nItem %d is present in position %d,item,i);
}
}
OUTPUT
100
Dept of CSE
Lab Manual
RESULT
Thus C program to implement List ADT using Array was written, executed and ouput is
verified successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
101
Dept of CSE
Lab Manual
ALGORTIHM
Step 1: Start the program.
Step 2: Declare a structure for Linked List.
Step 3: Declare the operations involved in Linked List.
Step 4: In main (), using do-while loop call the different operations based on user choice.
Step 5: Perform the operations such as creating a list, Inserting an element into the list,
Deleting an element from the list, Check for duplication, Find the previous and next element
in the list, sort the list and display the required result.
Step 6: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<process.h>
struct list
{
int data;
struct list *next;
};
typedef struct list node;
void create(node *);
void insert(node *);
void del(node *);
void duplicate(node *,node *);
void display(node *);
void findprev(node *);
void findnext(node *);
void find(node *);
void sort(node *);
void main()
{
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
102
Dept of CSE
Lab Manual
node *head;
int opt,a=0;
clrscr();
head=(node *)malloc(sizeof(node)); /*Allocation of Memory*/
do
{
printf("\n DEPARTMENT OF IT");
printf("\n linked list implementation");
printf("\n1.create\n2.insert\n3.delete\n4.duplicate\n5.display\n6.findprevious
\n7.findnext\n8.find\n9.sort\n10.exit");
printf("\n enter the choice\n");
scanf(%d,&opt);
switch(opt)
{
case 1:
create(head);
printf("\n the list is created");
break;
case 2:
insert(head);
printf("\n the element is inserted");
break;
case 3:
del(head);
break;
case 4:
duplicate(head,head);
break;
case 5:
display(head);
break;
case 6:
findprev(head);
break;
case 7:
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
103
Dept of CSE
Lab Manual
findnext(head);
break;
case 8:
find(head);
break;
case 9:
sort(head);
break;
case 10:
exit(0);
break;
default:
printf("\n wrong choice");
}
}while(1);
getch();
}
void create(node *head) /*Node Creation*/
{
printf("\n enter a node");
printf("\n type -999 to stop\n");
scanf(%d,&head->data);
if(head->data==-999)
head->next=NULL;
else
{
head->next=(node *)malloc(sizeof(node));
create(head->next);
}
}
void insert(node *head) /*Node Insertion*/
{
node *newnode,*temp,*prev;
int i=1,pos,newn;
printf("\n enter the element to be inserted");
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
104
Dept of CSE
Lab Manual
scanf(%d,&newn);
printf("\n enter the position to be inserted");
scanf(%d,&pos);
newnode=(node *)malloc(sizeof(node));
newnode->data=newn;
newnode->next=NULL;
temp=head;
while(temp!=NULL&&i<pos)
{
prev=temp;
temp=temp->next;
i++;
}
if(temp==NULL)
{
prev->next=newnode;
newnode->next=NULL;
}
else
{
prev->next=newnode;
newnode->next=temp;
}
}
void display(node *p) /*Displaying of created and inserted nodes*/
{
printf("\n singly linked list");
printf("\n head->");
while(p->next!=NULL)
{
printf(%d->,p->data);
p=p->next;
}
printf(%d,p->data);
}
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
105
Dept of CSE
Lab Manual
106
Dept of CSE
Lab Manual
{
p=p->next;
}
printf("\n the element next to %d is:%d,x,p->next->data);
}
void find(node *p) /*To find the position of given node*/
{
int x,i=0;
printf("\n enter the element");
scanf(%d,&x);
while(p->next!=NULL&&p->data!=x)
{
p=p->n ext;
i++;
}
printf("\n the element found at position %d",++i);
}
void sort(node *p) /*To sort the nodes in ascending order*/
{
node *newn,*counter;
int temp;
newn=p;
for(;newn->next!=NULL;newn=newn->next)
{
for(counter=newn->next;counter!=NULL;counter=counter->next)
{
if(newn->data!=-999&&counter->data!=-999)
{
if(newn->data>counter->data)
{
temp=newn->data;
newn->data=counter->data;
counter->data=temp;
}
}
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
107
Dept of CSE
Lab Manual
}
}
printf("\n sorted list");
display(p);
}
void del(node *head) /*To delete a particular node*/
{
node *temp;
int x,y=0;
printf("\n enter a no to delete:");
scanf(%d,&x);
if(head==NULL)
printf("\n list is empty");
temp=head->next;
while(temp->data!=x&&temp->next!=NULL)
{
temp=temp->next;
head=head->next;
}
if(temp->data==x&&temp->next!=NULL)
{
head->next=temp->next;
y++;
}
free(temp);
if(y==0)
printf("\n element not found \n");
}
108
Dept of CSE
Lab Manual
OUTPUT
109
Dept of CSE
Lab Manual
110
Dept of CSE
Lab Manual
RESULT
Thus C program to implement List ADT using Linked List was written, executed and output
is verified successfully.
111
Dept of CSE
Ex.No:48
Lab Manual
AIM
To write a C program for implementing Stack ADT using Array.
ALGORITHM
Step 1: Start the program.
Step 2: Define Push() to insert the element into stack.
Step 3: Define Pop() to delete an element from the stack.
Step 4: Display the values from the stack.
Step 5: Stop the program.
PROGRAM
#include<stdio.h>
#include<string.h>
#define MAX 4 //you can take any number to limit your stack size
int stack[MAX];
int top;
void push()
{
int token;
if(top==MAX-1)
{
printf("\nStack full");
return;
}
printf("\nEnter the element to be inserted:");
scanf(%d,&token);
top=top+1;
stack[top]=token;
}
int pop()
{
int t;
if(top==-1)
{
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
112
Dept of CSE
Lab Manual
return -1;
}
t=stack[top];
top=top-1;
return t;
}
void show()
{
int i;
printf("\nThe Stack elements are:\nTOP-->");
for(i=top;i>=0;i--)
{
printf("\t|%d|\n",stack[i];
}
}
void main()
{
int choice,token;
top=-1;
clrscr();
printf("STACK USING ARRAY");
do
{
printf("\n1.PUSH\n2.POP\n3.show or display\n4.exit");
printf("\nEnter your choice for the operation: ");
scanf(%d,&choice);
switch(choice)
{
case 1:
push();
show();
break;
case 2:
token=pop();
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
113
Dept of CSE
Lab Manual
if(token==-1)
printf("\nStack empty");
else
{
printf("\nThe element deleted is:%d",token);
show();
break;
}
case 3:
show();
break;
case 4:
exit(0);
default:printf("\nWrong choice");
break;
}
}while(choice<5);
getch();
}
OUTPUT
114
Dept of CSE
Lab Manual
RESULT
Thus C program to implement Stack ADT using Array was written, executed and output is
verified successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
115
Dept of CSE
Lab Manual
AIM
To write a C program for implementing Stack ADT using Linked List.
ALGORITHM
Step 1: Start the program.
Step 2: Define a structure for Linked list.
Step 3: Define Push() to insert value into the stack.
Step 4: Define Pop() to delete an element from the stack.
Step 5: Display the stack values.
Step 6: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
struct Node
{
int Data;
struct Node *next;
}*top;
void popStack()
{
struct Node *var=top;
if(var==top)
{
top = top->next;
free(var);
}
else
printf("\nStack Empty");
}
void push(int value)
{
struct Node *temp;
temp=(struct Node *)malloc(sizeof(struct Node));
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
116
Dept of CSE
Lab Manual
temp->Data=value;
if (top == NULL)
{
top=temp;
top->next=NULL;
}
else
{
temp->next=top;
top=temp;
}
}
void display()
{
struct Node *var=top;
if(var!=NULL)
{
printf("\nElements are as:\nTOP->");
while(var!=NULL)
{
printf("\t|%d|\n",var->Data);
var=var->next;
}
}
else
printf("\nStack is Empty");
}
void main()
{
int i=0;
top=NULL;
clrscr();
while(1)
{
printf(" \n1. Push to stack");
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
117
Dept of CSE
Lab Manual
118
Dept of CSE
Lab Manual
OUTPUT
119
Dept of CSE
Lab Manual
RESULT
Thus C program to implement Stack ADT using Linked List was written, executed and
output is verified successfully.
120
Dept of CSE
Lab Manual
ALGORITHM
Step 1: Start the program.
Step 2: Get the infix expression.
Step 3: If the expression is operand push to stack and if the expression is operator pop the
elements and display it.
Step 4: Display the final Postfix Expression.
Step 5: Stop the program.
PROGRAM
#include<stdio.h>
#include<ctype.h>
#define MAX 100
typedef struct stack
{
int data[MAX];
int top;
}stack;
int priority(char);
void init(stack *);
int empty(stack *);
int full(stack *);
char pop(stack *);
void push(stack *,char);
char top(stack *);
void main()
{
stack s;
char x;
int token;
clrscr();
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
121
Dept of CSE
Lab Manual
init(&s);
printf("\nINFIX TO POSTFIX EXPRESSION CONVERSION");
printf("\nEnter infix expression:");
while((token=getchar())!='n')
{
if(isalnum(token))
printf("%c",token);
else if(token == '(')
push(&s,'(');
else
{
if(token == ')')
while((x=pop(&s))!='(')
printf("%c",x);
else
{
while(priority(token)<=priority(top(&s)) && !empty(&s))
{
x=pop(&s);
printf("%c",x);
}
push(&s,token);
}
}
}
while(!empty(&s))
{
x=pop(&s);
printf("%c",x);
}
getch();
}
int priority(char x)
{
if(x == '(')
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
122
Dept of CSE
Lab Manual
return(0);
if(x == '+' || x == '-')
return(1);
if(x == '*' || x == '/' || x == '%')
return(2);
return(3);
}
void init(stack *s)
{
s->top=-1;
}
int empty(stack *s)
{
if(s->top==-1)
return(1);
else
return(0);
}
int full(stack *s)
{
if(s->top==MAX-1)
return(1);
else
return(0);
}
void push(stack *s,char x)
{
s->top=s->top+1;
s->data[s->top]=x;
}
char pop(stack *s)
{
int x;
x=s->data[s->top];
s->top=s->top-1;
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
123
Dept of CSE
Lab Manual
return(x);
}
char top(stack * s)
{
return(s->data[s->top]);
}
OUTPUT
RESULT
Thus C program to convert infix to postfix was written, executed and output is verified
successfully.
124
Dept of CSE
Lab Manual
ALGORITHM
Step 1: Start the program.
Step 2: Define Push() to push the operands.
Step 3: Define Pop() to pop the operands if an operator is encountered.
Step 4: Based on the operator perform the operation and display the result.
Step 5: Stop the program.
PROGRAM
#include<stdio.h>
#include<cmath.h>
#include<ctype.h>
int top=NULL;
double val;
void push(double);
double pop();
int a[10];
void main()
{
int i; double op, op1,op2;
char pos_str[10],r;
clrscr();
printf("\nEnter Postfix Expression:");
gets(pos_str);
for(i=0;i<strlen(pos_str);i++)
{
r=pos_str[i];
if(isdigit(r)>0)
{
push(r-48);
}
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
125
Dept of CSE
Lab Manual
else
{
op1=pop();
op2=pop();
switch(pos_str[i])
{
case '+':
op=op1+op2;
break;
case '*':
op=op1*op2;
break;
case '/':
op=op1/op2;
break;
case '-':
op=op1-op2;
break;
case '^':
op=pow(op1,op2);
break;
}
push(op);
}
}
printf("\nResult of Postfix expression is:%d",op);
getch();
}
void push(double val)
{
top=top+1;
a[top]=val;
}
double pop()
{
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
126
Dept of CSE
Lab Manual
val=a[top];
top=top-1;
return(val);
}
OUTPUT
RESULT
Thus C program to evaluate postfix expression using Array implementation of Stack ADT
was written, executed and output is verified successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
127
Dept of CSE
Lab Manual
ALGORITHM
Step 1: Start the program.
Step 2: Initialize rear and front to 0.
Step 3: Get the choice from the user.
Step 4: If the choice is 1 enqueue the element into queue.
Step 5: If the choice is 2 dequeue the element from queue.
Step 6: If the choice is 3 display the queue.
Step 7: Stop the program.
PROGRAM
#include<stdio.h>
#define MAX 5
void insert();
int del();
int queue[MAX], rear=0, front=0;
void display();
int main()
{
int choice, token;
clrscr();
do
{
printf(\nARRAY IMPLEMENTATION OF QUEUE ADT);
printf("\n1.Insert");
printf("\n2.Delete");
printf("\n3.show or display");
printf("\n4.Exit");
printf("\nEnter your choice for the operation: ");
scanf(%d,&choice);
switch(choice)
{
case 1:
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
128
Dept of CSE
Lab Manual
insert();
display();
break;
case 2:
token=del();
printf("\nThe token deleted is:%d",token);
display();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nWrong choice");
break;
}
}while(choice<5);
getch();
}
void display()
{
int i;
printf("\nThe queue elements are:\nFRONT->");
for(i=rear;i<front;i++)
{
printf("\t%d\n",queue[i]);
}
printf("->REAR");
}
void insert()
{
int token;
if(rear==MAX)
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
129
Dept of CSE
Lab Manual
{
printf("\nQueue full");
return;
}
printf("\nEnter the token to be inserted:");
scanf(%d,&token);
queue[front]=token;
front=front+1;
}
int del()
{
int t;
if(front==rear)
{
printf("\nQueue empty");
return 0;
}
rear=rear+1;
t=queue[rear-1];
return t;
}
OUTPUT
130
Dept of CSE
Lab Manual
RESULT
Thus C program to implement Queue ADT using Array was written, executed and output is
verified successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
131
Dept of CSE
Lab Manual
AIM
To write a C program for implementing Queue ADT using Linked List.
ALGORITHM
Step 1: Start the program.
Step 2: Create a structure for linked list.
Step 3: Based on the node defined in the structure, enqueue and dequeue the elements from
the queue.
Step 4: Display the result.
Step 5: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct Node
{
int Data;
struct Node* next;
}*rear, *front;
void delQueue()
{
struct Node *var=rear;
if(var==rear)
{
rear = rear->next;
free(var);
}
else
printf("\nQueue Empty");
}
void push(int value)
{
struct Node *temp;
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
132
Dept of CSE
Lab Manual
133
Dept of CSE
Lab Manual
while(1)
{
printf(" \nLINKED LIST IMPLEMENTATION OF QUEUE ADT");
printf(" \n1. Enqueue");
printf(" \n2. Dequeue");
printf(" \n3. Display Data of Queue");
printf(" \n4. Exit\n");
printf(" \nChoose Option: ");
scanf(%d,&i);
switch(i)
{
case 1:
int value;
printf("\nEnter a valueber to push into Queue: ");
scanf(%d,&value);
push(value);
display();
break;
case 2:
delQueue();
display();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nwrong choice for operation");
}
}
}
134
Dept of CSE
Lab Manual
OUTPUT
135
Dept of CSE
Lab Manual
RESULT
Thus C program to implement Queue ADT using Linked List was written, executed and
output is verified successfully.
136
Dept of CSE
Lab Manual
Ex.No:54(i)
BUBBLE SORT
AIM
To write a C program for implementing Bubble Sort.
ALGORITHM
Step 1: Start the program.
Step 2: Get the limit and array elements.
Step 3: Based on the array elements sort it and display the Pass results.
Step 4: Display the final sorted array.
Step 5: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
void bubble_sort(int[], int);
void main()
{
int arr[30], num, i;
clrscr();
printf("\nBUBBLE SORT");
printf("\nEnter no of elements :");
scanf("%d", &num);
printf("\nEnter array elements :");
for (i = 0; i < num; i++)
scanf("%d", &arr[i]);
bubble_sort(arr, num);
getch();
}
void bubble_sort(int iarr[], int num)
{
int i, j, k, temp;
printf("\nUnsorted Data:");
for (k = 0; k < num; k++)
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
137
Dept of CSE
Lab Manual
{
printf("%5d", iarr[k]);
}
for (i = 1; i < num; i++)
{
for (j = 0; j < num - 1; j++)
{
if (iarr[j] > iarr[j + 1])
{
temp = iarr[j];
iarr[j] = iarr[j + 1];
iarr[j + 1] = temp;
}
}
printf("\nAfter pass %d : ", i);
for (k = 0; k < num; k++)
{
printf("%5d", iarr[k]);
}
}
}
OUTPUT
RESULT
Thus C program to implement Bubble Sort was written, Executed and output is verified
successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
138
Dept of CSE
Lab Manual
Ex.No: 54(iii)
QUICK SORT
AIM
To write a C program for implementing Quick Sort.
ALGORITHM
Step 1: Start the program.
Step 2: Get the limit and the values from the user.
Step 3: Call quick(), to sort the elements. In quick(), assign a pivot value based on this sort
the rest of the elements.
Step 4: Display the sorted list.
Step 5: Stop the program.
PROGRAM
#include<stdio.h>
int i,j,n,pivot,arr[20];
void quick(int a[],int first,int last);
void swap1(int a[],int i,int j);
void main()
{
clrscr();
printf("QUICK SORT");
printf("\nEnter the Limit:");
scanf(%d,&n);
printf("\nEnter the elements:");
for(i=0;i<n;i++)
scanf(%d,&arr[i]);
quick(arr,0,n-1);
printf("\nThe Sorted List is:\n");
for(i=0;i<n;i++)
printf(%d\t,arr[i]);
getch();
}
void quick(int a[],int first, int last)
{
if(first<last)
{
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
139
Dept of CSE
Lab Manual
pivot=a[first];
i=first;
j=last;
while(i<j)
{
while(a[i]<=pivot && i<last)
i++;
while(a[j]>=pivot && j>first)
j--;
if(i<j)
swap1(a,i,j);
}
swap1(a,first,j);
quick(a,first,j-1);
quick(a,j+1,last);
}
}
void swap1(int a[],int i,int j)
{
int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
OUTPUT
RESULT
Thus C program to implement quick sort was written, executed and output is verified
successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
140
Dept of CSE
Lab Manual
Ex.No:54(iii)
MERGE SORT
AIM
To write a C program for implementing Merge Sort.
ALGORITHM
Step 1: Start the program.
Step 2: Get the limit and array from the user.
Step 3: First split the array through divide technique and sort it.
Step 4: Secondly, merge the sorted array using conquer technique.
Step 5: Display the sorted array.
Step 6: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
void mergesort(int a[],int i,int j);
void merge(int a[],int i1,int j1,int i2,int j2);
int main()
{
int a[30],n,i;
clrscr();
printf("\nMERGE SORT");
printf("\nEnter no of elements:");
scanf("%d",&n);
printf("Enter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
printf("\nSorted array is :");
for(i=0;i<n;i++)
printf("%d ",a[i]);
getch();
return 0;
}
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
141
Dept of CSE
Lab Manual
//left recursion
mergesort(a,mid+1,j);
//right recursion
merge(a,i,mid,mid+1,j);
}
}
void merge(int a[],int i1,int j1,int i2,int j2)
{
int temp[50];
int i,j,k;
i=i1;
j=i2;
k=0;
while(i<=j1 && j<=j2)
{
if(a[i]<a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
}
while(i<=j1)
temp[k++]=a[i++];
while(j<=j2)
temp[k++]=a[j++];
//Transfer elements from temp[] back to a[]
for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j];
}
142
Dept of CSE
Lab Manual
OUTPUT
RESULT
Thus C program to implement Merge Sort was written, executed and output is verified
successfully.
143
Dept of CSE
Lab Manual
Ex.No: 55(i)
LINEAR SEARCH
AIM
To write a C program for implementing Linear Search.
ALGORITHM
Step 1: Start the program.
Step 2: Get the limit and elements from the user.
Step 3: Pass the list and the element to be found to linear().
Step 4: Search for the element, if present, display the position else display it is not present.
Step 5: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
int linear(int [],int,int);
void main()
{
int i,n,find,pos,list[100];
clrscr();
printf("\nLinear Search");
printf("\n=============");
printf("\nEnter the limit:");
scanf(%d,&n);
printf("\nEnter the elements:");
for(i=0;i<n;i++)
scanf(%d,&list[i]);
printf("\nThe list is:\n");
for(i=0;i<n;i++)
printf("%d\t",list[i]);
printf("\nEnter the element to be searched:");
scanf(%d,&find);
pos=linear(list,n,find);
if(pos>=0)
printf("\nThe element %d present in:%d",find,pos+1);
else
printf("\nElement not present");
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
144
Dept of CSE
Lab Manual
getch();
}
int linear(int a[],int n,int data)
{
int i;
for(i=0;i<n;i++)
{
if(a[i]==data)
return i;
}
return -1;
}
OUTPUT
RESULT
Thus C++ program to implement Linear Search was written, executed and output is verified
successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
145
Dept of CSE
Lab Manual
Ex.No: 11(ii)
BINARY SEARCH
AIM
To write a C program for implementing Binary Search.
ALGORITHM
Step 1: Start the program.
Step 2: Get the limit and elements from the user.
Step 3: Sort the elements and then perform search.
Step 4: In binary(), find the mid value based on this search the element and display the
position.
Step 5: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
int binary(int [],int,int,int);
void sort(int [],int);
void main()
{
int list[100];
int n,i,find,pos;
clrscr();
printf("\nBINARY SEARCH");
printf("\nEnter the limit:");
scanf(%d,&n);
printf("\nEnter the elements:");
for(i=0;i<n;i++)
scanf(%d,&list[i]);
sort(list,n);
printf("\nThe sorted list:\n");
for(i=0;i<n;i++)
printf("%d\t",list[i]);
printf("\nEnter the element to be searched:");
scanf(%d,&find);
pos=binary(list,find,0,n);
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
146
Dept of CSE
Lab Manual
if(pos!=-1)
printf("\nThe element is in:"<<pos+1<<"position.");
else
printf("\nThe element is not present");
}
int binary(int list[],int find,int beg,int end)
{
int mid;
if(beg>end)
return -1;
mid=(beg+end)/2;
if(find==list[mid])
return mid;
else if(find<list[mid])
return binary(list,find,beg,mid-1);
else
return binary(list,find,mid+1,end);
}
void sort(int list[],int n)
{
int i,j,temp;
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
if(list[i]>list[j])
{
temp=list[i];
list[i]=list[j];
list[j]=temp;
}
}
}
}
147
Dept of CSE
Lab Manual
OUTPUT
RESULT
Thus C program to implement Binary Search was written, executed and output is verified
successfully.
148
Dept of CSE
Ex.No:56(i)
Lab Manual
AIM
To write a C program for implementing List ADT using Cursor.
ALGORITHM
Step 1: Start the program.
Step 2: Initialize the Cursor.
Step 3: Using Cursoralloc(), allocate the space for the values.
Step 4: Using insert(), insert the values into the allocated space.
Step 5: Using del(), delete the value for the cursor.
Step 6: Display the cursor list.
Step 7: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
#define SIZE 11
struct node
{
int data;
int nextpos;
};
typedef struct node cursor;
cursor cursorspace[SIZE];
void initialize();
void display();
void del(int x);
int findprev(int y);
void insert(int z);
int cursor_alloc()
{
int p;
p=cursorspace[0].nextpos;
cursorspace[0].nextpos=cursorspace[p].nextpos;
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
149
Dept of CSE
Lab Manual
return p;
}
void cursorfree(int p)
{
for(int i=cursorspace[p].nextpos;i<SIZE-1;i++)
{
if(cursorspace[i].nextpos=cursorspace[0].nextpos)
{
cursorspace[i].nextpos=p;
break;
}
}
cursorspace[p].nextpos =cursorspace[0].nextpos;
cursorspace[p].data=NULL;
cursorspace[0].nextpos=p;
}
void initialize()
{
for(int i=0;i<SIZE-1;i++)
{
cursorspace[i].data=NULL;
cursorspace[i].nextpos=i+1;
}
cursorspace[SIZE-1].data=NULL;
cursorspace[SIZE-1].nextpos=0;
}
void display()
{
printf("\nCursor Implementation");
printf("\n=====================");
printf("\n\tSlot\tElement\tNext Position";
for(int i=0;i<SIZE;i++)
{
printf("\n\t%d\t%d",i,cursorspace[i].data);
printf("%d\t\n",cursorspace[i].nextpos);
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
150
Dept of CSE
Lab Manual
}
}
void insert(int z)
{
int tmp;
tmp=cursor_alloc();
if(tmp==0)
printf("\nOut of Space");
else
cursorspace[tmp].data=z;
}
void del(int x)
{
int p,tmp;
p=findprev(x);
if(p==0)
{
tmp=1;
cursorfree(tmp);
}
else
{
tmp=cursorspace[p].nextpos;
cursorspace[p].nextpos=cursorspace[tmp].nextpos;
cursorfree(tmp);
}
}
int findprev(int y)
{
int p=0,i;
if(cursorspace[0].data==y)
{
return 0;
}
for(i=0;y!=cursorspace[i].data;i++)
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
151
Dept of CSE
Lab Manual
{
p=cursorspace[i].nextpos;
}
for(i=0;cursorspace[i].nextpos!=p;i++);
p=i;
return p;
}
void main()
{
int choice,element;
initialize();
do
{
printf("\nCURSOR IMPLEMENTATION OF LIST ADT");
printf("\n=================================");
printf("\n1.Insert\n2.Delete\n3.Display\n4.Exit");
printf("\nEnter the choice:");
scanf(%d,&choice);
switch(choice)
{
case 1:
printf("\nEnter an element to insert:");
scanf(%d,&element);
insert(element);
break;
case 2:
printf("\nEnter an element to delete:");
scanf(%d,&element);
del(element);
break;
case 3:
display();
break;
case 4:
exit(0);
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
152
Dept of CSE
Lab Manual
break;
default:
printf("\nInvalid Choice");
}
}while(choice<=4);
}
OUTPUT
153
Dept of CSE
Lab Manual
154
Dept of CSE
Lab Manual
155
Dept of CSE
Lab Manual
156
Dept of CSE
Lab Manual
157
Dept of CSE
Lab Manual
RESULT
Thus C program to implement List ADT using Cursor was written, executed and output is
verified successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
158
Dept of CSE
Lab Manual
Ex.No:56(ii)
RADIX SORT
AIM
To write a C program for implementing Radix Sort.
ALGORITHM
Step 1: Start the program.
Step 2: Get the limit and elements of array.
Step 3: Pass the array element to radix() to sort the array.
Step 4: Display the sorted array.
Step 5: Stop the program.
PROGRAM
#include <stdio.h>
#include<conio.h>
#define MAX 100
#define SHOWPASS
void print(int *a, int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d\t", a[i]);
}
void radix_sort(int *a, int n)
{
int i, b[MAX], m = 0, exp = 1;
for (i = 0; i < n; i++)
{
if (a[i] > m)
m = a[i];
}
while (m / exp > 0)
{
int box[10] = {0};
for (i = 0; i < n; i++)
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
159
Dept of CSE
Lab Manual
160
Dept of CSE
Lab Manual
RESULT
Thus C program to implement Radix Sort was written, executed and output is verified
successfully.
Dr.NGP IT/CSE/Lab Manual/II SEM/Programming and Data Structures I Laboratory (R2013)
161