You are on page 1of 159

Department of Computer Science and Engineering /Dr.N.G.

P IT

Dr. N.G.P. INSTITUTE OF TECHNOLOGY

Coimbatore-641048

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

LAB MANUAL

PROGRAMMING AND DATA STRUCTURES I LABORATORY
(R2013)

Prepared by Approved by
Ms.G.Selva Priya,AP/CSE HoD/CSE

1
Department of Computer Science and Engineering /Dr.N.G.P IT

2
Department of Computer Science and Engineering /Dr.N.G.P IT

C PROGRAM

Ex.No: 1 SUM OF DIGITS OF A GIVEN NUMBER

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>

3
Department of Computer Science and Engineering /Dr.N.G.P IT

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.
Ex.No: 2 REVERSE THE DIGITS OF A GIVEN NUMBER
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 unit’s place and sum it.
Step 4: Display the Reversed Number.
Step 5: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{

4
Department of Computer Science and Engineering /Dr.N.G.P IT

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.
Ex.No: 3 PRINT ALL COMBINATIONS OF A 4-DIGIT NUMBER

AIM
To write a C program to display all combinations of a 4-Digit Number.

PROCEDURE

Step 1: Start the program.
Step 2: Using 4 For Loops display each combinations of digits 1 → 4.
Step 3: Stop the program.

PROGRAM
#include<stdio.h>

5
Department of Computer Science and Engineering /Dr.N.G.P IT

#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);
}
}
}
}
}

OUTPUT:

6
Department of Computer Science and Engineering /Dr.N.G.P IT

RESULT
Thus a C program to display all combinations of 4 digit number was written, executed and output is
verified successfully.
Ex.No:4 FIND LARGEST DIGIT OF A GIVEN NUMBER

7
Department of Computer Science and Engineering /Dr.N.G.P IT

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.
Ex.No:5 CHECK WHETHER NUMBER & ITS REVERSE ARE SAME

8
Department of Computer Science and Engineering /Dr.N.G.P IT

AIM
To write a C program to check whether a number & its reverse are same or not.

PROCEDURE

Step 1: Start the program.
Step 2: Input a number from the user.
Step 3: Assign the number to a temporary variable.
Step 4: Using While Loop reverse the number.
Step 5: Check whether if the reverse and the value stored in temporary variable are same, if
true display the message “Number & its Reverse are same” or else display “Not same”.
Step 6: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int n,temp,reverse=0;
clrscr();
printf("\nEnter a no:");
scanf("%d",&n);
temp=n;
while(n!=0)
{
reverse=reverse*10;
reverse=reverse+(n%10);
n=n/10;
}
printf("\nThe Reverse of the no %d is:%d",temp,reverse);
if(temp==reverse)
printf("\n%d is a palindrome",temp);
else
printf("\n%d is not a palindrome",temp);
getch();
}

9
Department of Computer Science and Engineering /Dr.N.G.P IT

OUTPUT:

RESULT
Thus C program to check whether the given number is palindrome or not was written, executed &
output is verified successfully.
Ex.No: 6 CHECK WHETHER GIVEN NUMBER IS ARMSTRONG OR NOT

10
Department of Computer Science and Engineering /Dr.N.G.P IT

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();
}

11
Department of Computer Science and Engineering /Dr.N.G.P IT

OUTPUT

RESULT
Thus C program to check given number s Armstrong or not was written, executed & output is
verified successfully.
Ex.No:7 SUM OF ODD-POSITIONED DIGITS AND

12
Department of Computer Science and Engineering /Dr.N.G.P IT

EVEN-POSITIONED DIGITS OF A NUMBER

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();
}

OUTPUT

13
Department of Computer Science and Engineering /Dr.N.G.P IT

RESULT
Thus C program to find sum of Odd & even digits of a number was written, executed & output is
verified successfully.
Ex.No:8 FIND SECOND HIGHEST NUMBER FROM AN ARRAY

14
Department of Computer Science and Engineering /Dr.N.G.P IT

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]);
getch();

15
Department of Computer Science and Engineering /Dr.N.G.P IT

}

OUTPUT

RESULT
Thus a C program to find 2 nd largest element in an array was written, executed & output is verified
successfully.
Ex.No: 9 FIBONACCI SERIES

16
Department of Computer Science and Engineering /Dr.N.G.P IT

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.
Ex.No:10 GENERATE PRIME NUMBERS BETWEEN 50 AND 100

17
Department of Computer Science and Engineering /Dr.N.G.P IT

AIM
To write a C program to generate Prime Numbers between from 50 to 100.

PROCEDURE

Step 1: Start the program.
Step 2: Using For Loop assign Prime =1 and generate Prime Numbers when Prime equals 1.
Step 3: Stop the program.

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");
}

OUTPUT

18
Department of Computer Science and Engineering /Dr.N.G.P IT

RESULT
Thus C program to generate prime numbers between 50 & 100 was written, executed & output is
verified successfully.
Ex.No:11 COUNT NUMBER OF TIMES A DIGIT IS PRESENT IN A NUMBER

19
Department of Computer Science and Engineering /Dr.N.G.P IT

AIM
To write a C program to count number of times a digit is present in a number.

PROCEDURE

Step 1: Start the program.
Step 2: Input the number from the user.
Step 3: Input the digit to find its occurrence.
Step 4: Generate the digits of the number and & check whether the digit is equal to the digit
that user gave. If found increment the count value.
Step 5: Display the count.
Step 6: Stop the program.

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();
}

OUTPUT

20
Department of Computer Science and Engineering /Dr.N.G.P IT

RESULT
Thus C program to count number of times a digit present in a no was written, executed and output is
verified successfully.
Ex.No:12 INTERCHANGING OF THE ELEMENTS OF AN ARRAY

21
Department of Computer Science and Engineering /Dr.N.G.P IT

WITH THE ELEMENTS OF ANOTHER ARRAY

AIM
To write a C program to interchange elements of one array with other without using 3 rd array.

PROCEDURE

Step 1: Start the program.
Step 2: Input number of elements of array A & B.
Step 3: Input the elements for array A & B.
Step 4: Using temporary variable temp & For Loop interchange 2 array elements.
Step 5: Display the array elements after interchange.
Step 6: Stop the program.

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++)
printf("%d\t",a[i]);

22
Department of Computer Science and Engineering /Dr.N.G.P IT

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 3 rd array was written, executed &
output is verified successfully.
Ex.No:13 ARRANGE THE DIGITS OF A NUMBER IN ASCENDING ORDER

23
Department of Computer Science and Engineering /Dr.N.G.P IT

AIM
To write a C program to arrange the digits of a number in ascending order.

PROCEDURE

Step 1: Start the program.
Step 2: Input the number from the user.
Step 3: Using While Loop assign array with digits of the number.
Step 4: Using For Loop sort the digits in ascending order.
Step 5: Using another For Loop combine the digits as a number.
Step 6: Display the sorted digits of a number.

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;
}
}

24
Department of Computer Science and Engineering /Dr.N.G.P IT

}
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.
Ex.No:14 COUNT THE NUMBER OF VOWELS IN THE GIVEN TEXT

25
Department of Computer Science and Engineering /Dr.N.G.P IT

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.
Ex.No:15 PRODUCT OF 2 MATIRCES

26
Department of Computer Science and Engineering /Dr.N.G.P IT

AIM
To write a C program to find product of 2 matrices.

PROCEDURE

Step 1: Start the program.
Step 2: Input the no of rows for 2 matrices A & B.
Step 3: Input the elements of Matrices A & B.
Step 4: Using For Loop perform Matrix Multiplication & store the result in another matrix.
Step 5: Display the resultant matrix.
Step 6: Stop the program.

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++)
{
for(j=0;j<n;j++)

27
Department of Computer Science and Engineering /Dr.N.G.P IT

{
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.
EX.No:16 COUNT THE NUMBER OF WORDS IN THE GIVEN TEXT

28
Department of Computer Science and Engineering /Dr.N.G.P IT

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();
}
OUTPUT

29
Department of Computer Science and Engineering /Dr.N.G.P IT

RESULT
Thus C program to cunt no of words in the given line of text was written, executed & output is
verifed successfully.
Ex.No:17 SUM AND DIFFERENCE OF TWO MATRICES

30
Department of Computer Science and Engineering /Dr.N.G.P IT

AIM
To write a C program to find sum & difference of 2 matrices.

PROCEDURE

Step 1: Start the program.
Step 2: Input no of rows & columns for 2 matrices from the user.
Step 3: Input elements of 2 matrices from user.
Step 4: Using For Loop find the addition of 2 matrices and assign it to other matrix and
display it.
Step 5: Using another For Loop find the subtraction of 2 matrices and assign it to other matrix
and display it.
Step 6: Stop the program.

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");
for(i=0;i<m;i++)

31
Department of Computer Science and Engineering /Dr.N.G.P IT

{
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.

32
Department of Computer Science and Engineering /Dr.N.G.P IT

Ex.No:18(i) AREA & CIRCUMFERENCE OF A CIRCLE

AIM
To write a C program to find area & circumference of a circle.

PROCEDURE

Step 1: Start the program.
Step 2: Input radius of circle form the user.
Step 3: Calculate area & circumference of circle.
Step 4: Display the area & circumference.
Step 5: Stop the program.

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.

33
Department of Computer Science and Engineering /Dr.N.G.P IT

Ex.No:18(ii) CONVERT C° TO F° & F° TO C°

AIM
To write a C program to convert C° to F° & F° to C°.

PROCEDURE

Step 1: Start the program.
Step 2: Input Celcius from user.
Step 3: Convert celcius to farenheit.
Step 4: Display the degree in farenheit.
Step 5: Input Farenheit from the user.
Step 6: Convert farenheit to celcius.
Step 7: Display the degree in celcius.
Step 8: Stop the program.

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();
}

34
Department of Computer Science and Engineering /Dr.N.G.P IT

OUTPUT

RESULT
Thus C program to Convert C° to F° & F° to C° was written, executed & output is verified
successfully.
Ex.No:19(i) GREATEST OF 3 NUMBERS

35
Department of Computer Science and Engineering /Dr.N.G.P IT

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.
Ex.No.19(ii) CHECK WHETHER THE GIVEN YEAR IS LEAP YEAR OR NOT

36
Department of Computer Science and Engineering /Dr.N.G.P IT

AIM
To write a C program to check whether the given year is leap year or not.

PROCEDURE

Step 1: Start the program.
Step 2: Input the year from user.
Step 3: Using If condition check whether the year is divisible by 4 & not by 100 or divisible
by 400, if true display the year is Leap Year or else display it is not a leap year.
Step 4: Stop the program.

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.
Ex.No:20(i) FIND WHETHER THE GIVEN NUMBER IS EVEN OR ODD

37
Department of Computer Science and Engineering /Dr.N.G.P IT

AIM
To write a C program to find whether the given number is even or odd.

PROCEDURE

Step 1: Start the program.
Step 2: Input a number from the user.
Step 3: Using If condition check whether the no is even or odd, if (n%2==0) display the no is
even or else display the no is odd.
Step 4: Stop the program.

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.
Ex.No:20(ii) SQUARE ROOT OF A NUMBER

38
Department of Computer Science and Engineering /Dr.N.G.P IT

AIM
To write a C program to find Square Root of a number.

PROCEDURE

Step 1: Start the program.
Step 2: Input a number from the user.
Step 3: Using sqrt() display the square root directly.
Step 4: Using ser defined function pass the value to sqroot() and the calculate the square root
& display the result in Main() function.
Step 5: Stop the program.

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;
printf("Enter a no:");

39
Department of Computer Science and Engineering /Dr.N.G.P IT

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.
Ex.No:21 PALINDROME WITHOUT USING STRING FUNCTION

40
Department of Computer Science and Engineering /Dr.N.G.P IT

AIM
To write a C program to check whether the given name is palindrome or not without using String
Function.

PROCEDURE

Step 1: Start the program.
Step 2: Input the name from the user.
Step 3: Using While Loop calculate the length of the string.
Step 4: Using For Loop check the characters from first & reverse are same, if same assign flag
to 1 and display the name is palindrome.
Step 5: Stop the program.

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");
getch();

41
Department of Computer Science and Engineering /Dr.N.G.P IT

}

OUTPUT

RESULT
Thus C program to check whether the given name is palindrome or not was written, executed &
output is verified successfully.
Ex.No:22 SWAPPING OF 2 VALUES USING FUNCTION

42
Department of Computer Science and Engineering /Dr.N.G.P IT

WITHOUT USING 3RD VARIABLE

AIM
To write a C program to swap 2 values using function & without using 3 rd variable.

PROCEDURE

Step 1: Start the program.
Step 2: Declare a function to swap.
Step 3: Input 2 values from user in main().
Step 4: Pass the values to the function swap().
Step 5: In swap(), using addition & subtraction swap the values.
Step 6: Display the values.
Step 7: Stop the program.

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);
}

43
Department of Computer Science and Engineering /Dr.N.G.P IT

OUTPUT

RESULT
Thus C program to swap 2 values using functions and without using 3rd variable was written,
executed & output is verified successfully.
Ex.No:23(i) FACTORIAL OF GIVEN NUMBER USING RECURSIVE FUNCTION

44
Department of Computer Science and Engineering /Dr.N.G.P IT

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));
}

45
Department of Computer Science and Engineering /Dr.N.G.P IT

OUTPUT

RESULT
Thus C program to find factorial of a no using recursive function was written, executed & output is
verified successfully.
Ex.No:23(ii) FACTORIAL OF GIVEN NUMBER

46
Department of Computer Science and Engineering /Dr.N.G.P IT

WITHOUT USING RECURSIVE FUNCTION

AIM
To write a C program to find factorial of a no without using recursive function.

PROCEDURE

Step 1: Start the program.
Step 2: Input a no from user.
Step 3: Using For Loop find the factorial.
Step 4: Display the factorial value.
Step 5: Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int fact=1,i,num;
clrscr();
printf("Enter the number:\n");
scanf("%d", &num);
for(i=1;i<=num;i++)
{
fact=fact*i;
}
printf("The factorial of %d is %d",num,fact);
getch();
}
OUTPUT

RESULT
Thus the C program to find the factorial of a given number was written, executed & output is
verified successfully.
Ex.No:24 TRANSPOSE OF A MATRIX

47
Department of Computer Science and Engineering /Dr.N.G.P IT

AIM
To write a C program to transpose a Matrix.

PROCEDURE

Step 1: Start the program.
Step 2: Input no of rows & columns from user.
Step 3: Input the elements from user.
Step 4: Using For Loop display the original matrix.
Step 5: Using another For Loop display the Transpose by using a[j][i].
Step 6: Stop the program.

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");
}

48
Department of Computer Science and Engineering /Dr.N.G.P IT

//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.
Ex.No:25 STRING CONCATENATION & STRING COMPARISON

49
Department of Computer Science and Engineering /Dr.N.G.P IT

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 3 rd 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++;
}

50
Department of Computer Science and Engineering /Dr.N.G.P IT

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.
Ex.No:26 REVERSE A GIVEN STRING

51
Department of Computer Science and Engineering /Dr.N.G.P IT

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;
}

52
Department of Computer Science and Engineering /Dr.N.G.P IT

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.

53
Department of Computer Science and Engineering /Dr.N.G.P IT

Ex.No:27 COUNT THE NUMBER OF LINES,
VOWELS, CONSONANTS, WORDS OF A GIVEN TEXT

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':

54
Department of Computer Science and Engineering /Dr.N.G.P IT

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.

55
Department of Computer Science and Engineering /Dr.N.G.P IT

Ex.No:28 SEARCHING OF AN ELEMENT IN AN ARRAY

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();
}

56
Department of Computer Science and Engineering /Dr.N.G.P IT

OUTPUT

RESULT
Thus C program to search an element in an rray was written, executed & output is verified
successfully.

57
Department of Computer Science and Engineering /Dr.N.G.P IT

Ex.No:29 COUNT A CHARACTER THAT APPEARS NUMBER OF TIMES
IN A GIVEN TEXT USING WHILE LOOP

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();
}

58
Department of Computer Science and Engineering /Dr.N.G.P IT

OUTPUT

RESULT
Thus C program to find no of times a character found in the text was written, executed & output is
verified successfully.

59
Department of Computer Science and Engineering /Dr.N.G.P IT

Ex.No:30 SIMULATE THE CALCULATOR USING FUNCTION

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);

60
Department of Computer Science and Engineering /Dr.N.G.P IT

}
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.

61
Department of Computer Science and Engineering /Dr.N.G.P IT

Ex.No:31 ACCEPT ANY SINGLE DIGIT NUMBER AND
DISPLAY IT IN WORDS

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;

62
Department of Computer Science and Engineering /Dr.N.G.P IT

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.

63
Department of Computer Science and Engineering /Dr.N.G.P IT

Ex.No:32 ACCEPT A STRING IN ANY CASE AND
CONVERT IT TO UPPER CASE

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.

64
Department of Computer Science and Engineering /Dr.N.G.P IT

Ex.No:33 FIND THE SMALLEST AND LARGEST ELEMENT OF AN ARRAY

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];

65
Department of Computer Science and Engineering /Dr.N.G.P IT

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.

66
Department of Computer Science and Engineering /Dr.N.G.P IT

Ex.No:34 SUM OF TWO NUMBERS USING FUNCTION
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.

67
Department of Computer Science and Engineering /Dr.N.G.P IT

Ex.No:35 ARRANGE THE GIVEN NAMES
IN THE ALPHABETICAL ORDER USING ARRAYS

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]);

68
Department of Computer Science and Engineering /Dr.N.G.P IT

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.

69
Department of Computer Science and Engineering /Dr.N.G.P IT

Ex.No:36 CHECK THE GIVEN NUMBER IS PRIME OR NOT

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();
}

70
Department of Computer Science and Engineering /Dr.N.G.P IT

OUTPUT

RESULT
Thus C program to check whether given no is prime or not was written, executed & output is
verified successfully.

71
Department of Computer Science and Engineering /Dr.N.G.P IT

Ex.No:37 GENERATION OF EMPLOYEE PAYROLL USING STRUCTURES

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);

72
Department of Computer Science and Engineering /Dr.N.G.P IT

getch();
}

OUTPUT

RESULT
Thus C program to generate employee details using structure was written, executed & output is
verified successfully.

73
Department of Computer Science and Engineering /Dr.N.G.P IT

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;
}

74
Department of Computer Science and Engineering /Dr.N.G.P IT

OUTPUT

RESULT
Thus C program to find the given number is perfect or not was written, executed and output
is verified successfully.

75
Department of Computer Science and Engineering /Dr.N.G.P IT

Ex.No:39 AREA OF 4 GEOMETRIC SHAPES

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();

76
Department of Computer Science and Engineering /Dr.N.G.P IT

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;

77
Department of Computer Science and Engineering /Dr.N.G.P IT

printf("\nEnter the height and breadth of Triangle:");
scanf("%lf%lf",&h,&b);
area=0.5*b*h;
printf("\nArea of Triangle is:%lf",area);
}
void sqarea()
{
int area,side;
printf("\nEnter the side of a Square:");
scanf("%d",&side);
area=side*side;
printf("\nArea of Square is:%d",area);
}
void cuarea()
{
int area,cuside;
printf("\nEnter the side of a Cube:");
scanf("%d",&cuside);
area=cuside*cuside*cuside;
printf("\nArea of Cube is:%d",area);
}

78
Department of Computer Science and Engineering /Dr.N.G.P IT

RESULT
Thus C program to find area of 4 geometric shapes was written, executed and output is
verified successfully.

79
Department of Computer Science and Engineering /Dr.N.G.P IT

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])

80
Department of Computer Science and Engineering /Dr.N.G.P IT

{
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.

81
Department of Computer Science and Engineering /Dr.N.G.P IT

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.

82
Department of Computer Science and Engineering /Dr.N.G.P IT

Ex.No:42 ARITHMETIC OPERATION USING POINTERS AND FUNCTIONS

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)

83
Department of Computer Science and Engineering /Dr.N.G.P IT

{
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.

84
Department of Computer Science and Engineering /Dr.N.G.P IT

Ex.No:43: FILE COPY AND APPEND

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)
{

85
Department of Computer Science and Engineering /Dr.N.G.P IT

printf("Cannot open file %s \n", filename);
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);

86
Department of Computer Science and Engineering /Dr.N.G.P IT

fclose(fptr1);
fclose(fptr2);
fclose(fptr3);
fclose(fptr4);
}

OUTPUT

RESULT

87
Department of Computer Science and Engineering /Dr.N.G.P IT

Thus C program to perform file copy and append operation was written, executed and
output is verified successfully.
Ex.No:44 SEQUENTIAL ACCESS FILE

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 display(FILE *);
int search(FILE *,int);

void main()
{
int i,n,usn_key,opn;
FILE *fp;
printf(" How many Records ? ");
scanf("%d",&n);

88
Department of Computer Science and Engineering /Dr.N.G.P IT

fp=fopen("stud.dat","w");
for(i=0;i<n;i++)
{
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 not
found\n",usn_key);
break;
case 3: printf(" Exit!! Press a key . . .");
getch();
break;
default: printf(" Invalid Option!!! Try again !!!\n");
break;

89
Department of Computer Science and Engineering /Dr.N.G.P IT

}
}while(opn != 3);
fclose(fp);
}/* End of main() */

void display(FILE *fp)
{
rewind(fp);
while(fread(&s,sizeof(s),1,fp))
printf("%d\t%s\t%d\t%d\t%d\n",s.usn,s.name,s.m1,s.m2,s.m3);
}
int search(FILE *fp, int usn_key)
{
rewind(fp);
while(fread(&s,sizeof(s),1,fp))
if( s.usn == usn_key) return 1;
return 0;
}

OUTPUT

90
Department of Computer Science and Engineering /Dr.N.G.P IT

RESULT
Thus C program to perform sequential file access was written, executed and output is
verified successfully.
Ex.No:45 RANDOM ACCESS FILE

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);

91
Department of Computer Science and Engineering /Dr.N.G.P IT

for(i=1;i<=n;i++)
{
printf("\n\nEmpno:");
scanf("%d",&x.empno);
printf("Name:");
scanf("%s",x.ename);
printf("Salary:"); /* Accepting the data from keyboard*/
scanf("%d",&x.sal);
printf("Phone No:");
scanf("%ld",&x.phno);
fwrite(&x,sizeof(x),1,p); /* Writing objects onto the file*/
}
printf("Press any key to continue....\n");
getch(); /* To wait for user confirmation*/
while(1)
{
printf("\nEnter the record number:");
scanf("%d",&rec); /* Accepting record number */
if(rec<1||rec>n) /* Checking for validity of record number*/
printf("Search failure...");
else
{
fseek(p,(rec-1)*sizeof(x),0); /* placing record pointer to the required record*/
fread(&x,sizeof(x),1,p); /* Reading the record*/
printf("Empno:%d\nName:%s\nSalary:%d\nPhno:
%ld",x.empno,x.ename,x.sal,x.phno); /* 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;
}

92
Department of Computer Science and Engineering /Dr.N.G.P IT

OUTPUT

93
Department of Computer Science and Engineering /Dr.N.G.P IT

RESULT
Thus C program to perform Random file access was written, executed and output is verified
successfully.

DATA STRUCTURES

94
Department of Computer Science and Engineering /Dr.N.G.P IT

Ex.No: 46 ARRAY IMPLEMENTATION OF LIST ADT
AIM
To write a C program to implement List ADT using Array.
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
{

95
Department of Computer Science and Engineering /Dr.N.G.P IT

printf("\n1.Create\n2.Display\n3.Insert\n4.Delete\n5.Search");
printf("\nEnter the Choice:");
scanf(“%d”,&ch);
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);

96
Department of Computer Science and Engineering /Dr.N.G.P IT

getch();
}

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];

97
Department of Computer Science and Engineering /Dr.N.G.P IT

size--;
printf("\nList after Deletion:\n");
for(i=0;i<size;i++)
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

98
Department of Computer Science and Engineering /Dr.N.G.P IT

RESULT
Thus C program to implement List ADT using Array was written, executed and ouput is
verified successfully.

99
Department of Computer Science and Engineering /Dr.N.G.P IT

Ex.No: 47 LINKED LIST IMPLEMENTATION OF LIST ADT
AIM
To write a C program for implementing List ADT using Linked List.
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()
{

100
Department of Computer Science and Engineering /Dr.N.G.P IT

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:

101
Department of Computer Science and Engineering /Dr.N.G.P IT

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");

102
Department of Computer Science and Engineering /Dr.N.G.P IT

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);
}

103
Department of Computer Science and Engineering /Dr.N.G.P IT

void duplicate(node *p,node *q) /*To check any duplication */
{
int a=0;
q=p->next;
while(p->next!=NULL&&q!=NULL)
{
if(p->data==q->data)
{
printf("\n duplication is found\n");
a++;
break;
}
p=p->next;
q=q->next;
}
if(a==0)
printf("\n duplication is not found\n");
}
void findprev(node *p) /*To find previous node of a given node*/
{
int x;
printf("\n enter the element");
scanf(“%d”,&x);
while(p!=NULL&&p->next->data!=x)
{
p=p->next;
}
printf(“\nthe element previous to %d is %d”,x,p->data) ;
}
void findnext(node *p) /*To find next node of given node*/
{
int x;
printf("\n enter the element");
scanf(“%d”,&x);
while(p->next!=NULL&&p->data!=x)

104
Department of Computer Science and Engineering /Dr.N.G.P IT

{
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->next;
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;
}
}

105
Department of Computer Science and Engineering /Dr.N.G.P IT

}
}
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");
}

106
Department of Computer Science and Engineering /Dr.N.G.P IT

OUTPUT

107
Department of Computer Science and Engineering /Dr.N.G.P IT

108
Department of Computer Science and Engineering /Dr.N.G.P IT

RESULT
Thus C program to implement List ADT using Linked List was written, executed and output
is verified successfully.

109
Department of Computer Science and Engineering /Dr.N.G.P IT

Ex.No:48 ARRAY IMPLEMENTATION OF STACK ADT

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)
{

110
Department of Computer Science and Engineering /Dr.N.G.P IT

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();

111
Department of Computer Science and Engineering /Dr.N.G.P IT

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

112
Department of Computer Science and Engineering /Dr.N.G.P IT

RESULT
Thus C program to implement Stack ADT using Array was written, executed and output is
verified successfully.

113
Department of Computer Science and Engineering /Dr.N.G.P IT

Ex.No: 49 LINKED LIST IMPLEMENTATION OF STACK ADT

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));

114
Department of Computer Science and Engineering /Dr.N.G.P IT

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");

115
Department of Computer Science and Engineering /Dr.N.G.P IT

printf(" \n2. Pop from Stack");
printf(" \n3. Display data of Stack");
printf(" \n4. Exit\n");
printf(" \nChoose Option: ");
scanf(“%d”,&i);
switch(i)
{
case 1:
int value;
printf("\nEnter a value to push into Stack: ");
scanf(“%d”,&value);
push(value);
display();
break;
case 2:
popStack();
display();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nwrong choice for operation");
}
}
getch();
}

116
Department of Computer Science and Engineering /Dr.N.G.P IT

OUTPUT

117
Department of Computer Science and Engineering /Dr.N.G.P IT

RESULT
Thus C program to implement Stack ADT using Linked List was written, executed and
output is verified successfully.

118
Department of Computer Science and Engineering /Dr.N.G.P IT

Ex.No:50 INFIX TO POSTFIX EXPRESSION CONVERSION

AIM
To write a C program to convert infix expression to postfix expression.

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();

119
Department of Computer Science and Engineering /Dr.N.G.P IT

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 == '(')

120
Department of Computer Science and Engineering /Dr.N.G.P IT

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;

121
Department of Computer Science and Engineering /Dr.N.G.P IT

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.

122
Department of Computer Science and Engineering /Dr.N.G.P IT

Ex.No: 51 POSTFIX EXPRESSION EVALUATION USING ARRAY
IMPLEMENTATION STACK ADT
AIM
To write a C program to evaluate postfix expression using Array Implementation of Stack
ADT.
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);
}

123
Department of Computer Science and Engineering /Dr.N.G.P IT

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()
{

124
Department of Computer Science and Engineering /Dr.N.G.P IT

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.

125
Department of Computer Science and Engineering /Dr.N.G.P IT

Ex.No: 52 ARRAY IMPLEMENTATION OF QUEUE ADT
AIM
To write a C program for implementing Queue ADT using Array.
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:

126
Department of Computer Science and Engineering /Dr.N.G.P IT

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)

127
Department of Computer Science and Engineering /Dr.N.G.P IT

{
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

128
Department of Computer Science and Engineering /Dr.N.G.P IT

RESULT
Thus C program to implement Queue ADT using Array was written, executed and output is
verified successfully.

129
Department of Computer Science and Engineering /Dr.N.G.P IT

Ex.No: 53 LINKED LIST IMPLEMENTATION OF QUEUE ADT

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;

130
Department of Computer Science and Engineering /Dr.N.G.P IT

temp=(struct Node *)malloc(sizeof(struct Node));
temp->Data=value;
if (front == NULL)
{
front=temp;
front->next=NULL;
rear=front;
}
else
{
front->next=temp;
front=temp;
front->next=NULL;
}
}
void display()
{
struct Node *var=rear;
if(var!=NULL)
{
printf("\nElements are as:\nFRONT->");
while(var!=NULL)
{
printf("\t%d\n",var->Data);
var=var->next;
}
printf("->REAR\n");
}
else
printf("\nQueue is Empty");
}
int main()
{
int i=0;
front=NULL;

131
Department of Computer Science and Engineering /Dr.N.G.P IT

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");
}
}
}

132
Department of Computer Science and Engineering /Dr.N.G.P IT

OUTPUT

133
Department of Computer Science and Engineering /Dr.N.G.P IT

RESULT
Thus C program to implement Queue ADT using Linked List was written, executed and
output is verified successfully.

134
Department of Computer Science and Engineering /Dr.N.G.P IT

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:");

135
Department of Computer Science and Engineering /Dr.N.G.P IT

for (k = 0; k < num; k++)
{
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

136
Department of Computer Science and Engineering /Dr.N.G.P IT

Thus C program to implement Bubble Sort was written, Executed and output is verified
successfully.
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)
{

137
Department of Computer Science and Engineering /Dr.N.G.P IT

if(first<last)
{
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

138
Department of Computer Science and Engineering /Dr.N.G.P IT

Thus C program to implement quick sort was written, executed and output is verified
successfully.
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();

139
Department of Computer Science and Engineering /Dr.N.G.P IT

return 0;
}
void mergesort(int a[],int i,int j)
{
int mid;
if(i<j)
{
mid=(i+j)/2;
mergesort(a,i,mid); //left recursion
mergesort(a,mid+1,j); //right recursion
merge(a,i,mid,mid+1,j); //merging of two sorted sub-arrays
}
}
void merge(int a[],int i1,int j1,int i2,int j2)
{
int temp[50]; //array used for merging
int i,j,k;
i=i1; //beginning of the first list
j=i2; //beginning of the second list
k=0;
while(i<=j1 && j<=j2) //while elements in both lists
{
if(a[i]<a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
}
while(i<=j1) //copy remaining elements of the first list
temp[k++]=a[i++];
while(j<=j2) //copy remaining elements of the second list
temp[k++]=a[j++];
//Transfer elements from temp[] back to a[]
for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j];
}

140
Department of Computer Science and Engineering /Dr.N.G.P IT

OUTPUT

RESULT

141
Department of Computer Science and Engineering /Dr.N.G.P IT

Thus C program to implement Merge Sort was written, executed and output is verified
successfully.
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);

142
Department of Computer Science and Engineering /Dr.N.G.P IT

else
printf("\nElement not present");
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

143
Department of Computer Science and Engineering /Dr.N.G.P IT

Thus C++ program to implement Linear Search was written, executed and output is verified
successfully.
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:");

144
Department of Computer Science and Engineering /Dr.N.G.P IT

scanf(“%d”,&find);
pos=binary(list,find,0,n);
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;
}
}
}

145
Department of Computer Science and Engineering /Dr.N.G.P IT

}

OUTPUT

RESULT

146
Department of Computer Science and Engineering /Dr.N.G.P IT

Thus C program to implement Binary Search was written, executed and output is verified
successfully.

Ex.No:56(i) CURSOR IMPLEMENTATION OF LIST ADT

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;

147
Department of Computer Science and Engineering /Dr.N.G.P IT

p=cursorspace[0].nextpos;
cursorspace[0].nextpos=cursorspace[p].nextpos;
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++)
{

148
Department of Computer Science and Engineering /Dr.N.G.P IT

printf("\n\t%d\t%d",i,cursorspace[i].data);
printf("%d\t\n",cursorspace[i].nextpos);
}
}
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;

149
Department of Computer Science and Engineering /Dr.N.G.P IT

}
for(i=0;y!=cursorspace[i].data;i++)
{
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;

150
Department of Computer Science and Engineering /Dr.N.G.P IT

case 4:
exit(0);
break;
default:
printf("\nInvalid Choice");
}
}while(choice<=4);
}

OUTPUT

151
Department of Computer Science and Engineering /Dr.N.G.P IT

152
Department of Computer Science and Engineering /Dr.N.G.P IT

153
Department of Computer Science and Engineering /Dr.N.G.P IT

154
Department of Computer Science and Engineering /Dr.N.G.P IT

155
Department of Computer Science and Engineering /Dr.N.G.P IT

RESULT
Thus C program to implement List ADT using Cursor was written, executed and output is
verified successfully.

156
Department of Computer Science and Engineering /Dr.N.G.P IT

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++)

157
Department of Computer Science and Engineering /Dr.N.G.P IT

box[a[i] / exp % 10]++;
for (i = 1; i < 10; i++)
box[i] += box[i - 1];
for (i = n - 1; i >= 0; i--)
b[--box[a[i] / exp % 10]] = a[i];
for (i = 0; i < n; i++)
a[i] = b[i];
exp *= 10;
#ifdef SHOWPASS
printf("\n\nPASS : ");
print(a, n);
#endif
}
}
void main()
{
int arr[MAX];
int i, num;
printf("\nRADIX SORT");
printf("\nEnter total elements (num < %d) : ", MAX);
scanf("%d", &num);
printf("\nEnter %d Elements : ", num);
for (i = 0; i < num; i++)
scanf("%d", &arr[i]);
printf("\nARRAY : ");
print(&arr[0], num);
radix_sort(&arr[0], num);
printf("\n\nSORTED : ");
print(&arr[0], num);
}

158
Department of Computer Science and Engineering /Dr.N.G.P IT

RESULT
Thus C program to implement Radix Sort was written, executed and output is verified
successfully.

159