Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (1 vote)
321 views

Lab Manual For C-Programming

The document contains 23 C programming lab manual examples covering basic programming concepts like input/output, arithmetic operations, conditional statements, loops, functions, arrays and pointers. The examples provide the code to write programs to find the sum and average of numbers, calculate area and circumference of a circle, use conditional and logical operators, perform operations like swap two variables, find greatest among three numbers, print patterns, reverse a number, check Armstrong number etc. Each example lists the code snippet and corresponding output for each program.

Uploaded by

Dixit Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
321 views

Lab Manual For C-Programming

The document contains 23 C programming lab manual examples covering basic programming concepts like input/output, arithmetic operations, conditional statements, loops, functions, arrays and pointers. The examples provide the code to write programs to find the sum and average of numbers, calculate area and circumference of a circle, use conditional and logical operators, perform operations like swap two variables, find greatest among three numbers, print patterns, reverse a number, check Armstrong number etc. Each example lists the code snippet and corresponding output for each program.

Uploaded by

Dixit Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Lab Manual for C-Programming

1] Write a program to find sum of two numbers.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,s;
clrscr();
printf(“Enter two no: ”);
scanf(“%d%d",&a,&b);
s=a+b;
printf(“sum=%d”,s);
getch();
}

Output:
Enter two no: 5 6
sum=11

2] Write a program to find area and circumference of circle.

#include<stdio.h>
#include<conio.h>
void main()
{
int r;
float pi=3.14,area,ci;
clrscr();
printf(“enter radius of circle: ”);
scanf(“%d”,&r);
area=pi*r*r;
printf(“area of circle=%f ”,area);
ci=2*pi*r;
printf(“circumference=%f ”,ci);
getch();
}

Output:
enter radius of a circle: 5
area of circle=78.000
circumference=31.4

3] Write a program to find the simple interest.

#include<stdio.h>
#include<conio.h>
void main()
{
int p,r,t,si;
clrscr();
printf(“enter principle, Rate of interest & time to find simple interest: ”);
scanf(“%d%d%d”,&p,&r,&t);
si=(p*r*t)/100;
printf(“simple intrest= %d”,si);
getch();
}

Output:
enter principle, rate of interest & time to find simple interest: 500 5 2

simple interest=50
4] Write a program to convert temperature from degree centigrade to Fahrenheit.

#include<stdio.h>
#include<conio.h>

void main()
{
float c,f;
clrscr();
printf(“enter temp in centigrade: ”);
scanf(“%f”,&c);
f=(1.8*c)+32;
printf(“temp in Fahrenheit=%f ”,f);
getch();
}

Output:
enter temp in centigrade: 32
temp in Fahrenheit=89.59998

5] Write a program to calculate sum of 5 subjects and find percentage.

#include<stdio.h>
#include<conio.h>
void main()
{
int s1,s2,s3,s4,s5,sum,total=500;
float per;
clrscr();
printf(“enter marks of 5 subjects: ”);
scanf(“%d%d%d%d%d”,&s1,&s2,&s3,&s4,&s5);
sum=s1+s2+s3+s4+s5;
printf(“sum=%d”,sum);
per=(sum*100)/total;
printf(“percentage=%f”,per);
getch();
}
Output:
enter marks of 5 subjects:
60
65
50
60
60

sum=300
percentage=60.000

6] Write a program of swapping two no’s by using third variable.

#include<stdio.h>
#include<conio.h>
void main()
{
int a, b,c;
clrscr();
printf(“enter value for a & b: ”);
scanf(“%d%d”, &a, &b);
c=a;
a=b;
b=c;
printf(“after swapping the value of a & b: %d %d” ,a ,b);
getch();
}

Output:
enter value for a & b: 4 5
after swapping the value of a & b: 5 4

7] Write a program of swapping two no’s without using third variable.

#include<stdio.h>
#include<conio.h>
void main()
{
int a, b;
clrscr();
printf(“enter value for a & b: ”);
scanf(“%d%d”, &a, &b);
a=a+b;
b=a-b;
a=a-b;
printf(“after swapping the value of a & b: %d %d” ,a ,b);
getch();
}

Output:
enter value for a & b: 4 5
after swapping the value of a & b: 5 4

8] Write a program to find whether given no. is even or odd.

#include<stdio.h>
#include<conio.h>
void main()
{
int n; clrscr();
printf(“enter any no: ”);
scanf(“%d”,&n);
if(n%2==0)
{
printf(“no is even”);
}
else
{
printf(“no is odd”);
}
getch();
}

Output:
enter any no: 5
no is odd
9] Write a program to find that entered year is leap year or not.

#include<stdio.h>
#include<conio.h>
void main()
{
int n; clrscr();
printf(“enter any year: ”);
scanf(“%d”, &n);
if(n%4==0)
printf(“year is a leap year”);
else
printf(“year is not a leap year”);
getch();
}

Output:
enter any year: 1947
year is not a leap year

10] Program to find greatest in 3 numbers.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“enter value of a, b & c: ”);
scanf(“%d%d%d”,&a,&b,&c);
if((a>b)&&(a>c))
{
printf(“a is greatest”);
}
if((b>c)&&(b>a))
{
printf(“b is greatest”);
}
if((c>a)&&(c>b))
{
printf(“c is greatest”);
}
getch();
}

Output:
enter value for a, b& c:
5 7 4
b is greatest

11] Write a program to use switch statement to display Monday to Sunday.

#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf(“enter m for Monday\n t for Tuesday\n w for Wednesday\n h for Thursday\n f for Friday\n s
for Saturday\n u for Sunday”);
scanf(“%c”, &ch);
switch(ch)
{
case ‘m’: printf(“monday”);
break;
case ‘t’: printf(“tuesday”);
break;
case ‘w’: printf(“wednesday”);
break;
case ‘h’: printf(“thursday”);
break;
case ‘f ’: printf(“friday”);
break;
case ‘s’: printf(“saturday”);
break;
case ‘u’: printf(“sunday”);
break;
default : printf(“wrong input”);
break;
}
getch();
}

Output:
Enter
m for Monday
t for Tuesday
w for Wednesday
h for Thursday
f for Friday
s for Saturday
u for Sunday: f
Friday

12] Write a program to display arithmetic operator using switch case.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,n,s,m,su,d;
clrscr();
printf(“enter two no’s : ”);
scanf(“%d%d”,&a,&b);
printf(“enter 1 for sum\n2 for multiply\n3for subtraction\n4 for division: ”);
scanf(“%d”, &n);
switch(n)
{
case 1: s=a+b;
printf(“sum=%d” ,s);
break;
case 2: m=a*b;
printf(“multiply=%d” ,m);
break;
case 3: su=a-b;
printf(“subtraction=%d” ,su);
break;
case 4: d=a/b;
printf(“division=%d” ,d);
break;
default: printf(“wrong input”);
break;
}
getch();
}

Output:
enter two no’s:
8 4
enter 1 for sum
2 for multiply
3 for subtraction
4 for division: 1
sum=12

13] Write a program to show the use of conditional operator.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“enter value for a & b: ”);
scanf(“%d%d”, &a, &b);
(a>b)? printf(“a is greater”) : printf(“b is greater”);
getch();
}

Output:
enter value for a & b: 5 7
b is greater
14] Write a program to use bitwise AND operator between the two integers.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“Read the integers from keyboard:- ”);
scanf(“%d %d”,&a,&b);
c=a&b;
printf(“\nThe Answer after ANDing is: %d ”,c);
getch();
}

Output:
Read the integers from keyboard:- 8 4
The Answer after ANDing is: 0

15] Write a program to shift inputed data by two bits to the left.

#include<stdio.h>
#include<conio.h>
void main()
{
int x,y; clrscr();
printf(“Read the integer from keyboard :- ”);
scanf(“%d”,&x);
x<<=3;
y=x;
printf(“\nThe left shifted data is = %d ”,y);
getch();
}
Output:
Read the integer from keyboard:- 2
The left shifted data is = 16

16] Write a program to calculate sum of ‘n’ natural number.

#include<stdio.h>
#include<conio.h>
void main()
{
int i, sum=0;
clrscr();
for(i=1;i<=10;i++)
{
sum=sum+i;
}
printf(“sum =%d”,sum);
getch();
}

Output:

sum=55

17] Write a program to find factorial of a number.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,fact=1;
clrscr();
printf(“Enter any no: ”);
scanf(“%d” ,&n);
for(i=n ; i>=1 ; i--)
{
fact = fact * i;
}

printf(“Factorial=%d” ,fact);
getch();
}

Output:
Enter any no: 4
Factorial=24

18] Write a program to display series and find sum of 1+3+5+……..+n.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,sum=0;
clrscr();
printf(“Enter any no: ”);
scanf(“%d”,&n);
for(i=1;i<n;i=i+2)
{
printf(“%d+”,i);
sum=sum+i;
}
printf("%d",n);
printf("\nsum=%d",sum+n);
getch();
}

Output:
Enter any no: 7
1+3+5+7
Sum=16

19] Write a program to print Fibonacci series up to 100.

#include<stdio.h>
#include<conio.h>
void main()
{
int a=1,b=1,c=0,i;
clrscr();
printf("%d\t%d\t" ,a ,b);
for(i=0 ; i<=10 ; i++)
{
c=a+b;
if(c<100)
{
printf("%d\t",c);
}
a=b;
b=c;
}
getch();
}

Output:
0 1 1 2 3 5 8 13 21 34 55 89

20] Write a program to print a table of any number.

#include<stdio.h>
#include<conio.h>
void main()
{
int n, i;
printf("Enter an integer: ");
scanf("%d",&n);
for(i=1; i<=10; i++)
{
printf("%d * %d = %d \n", n, i, n*i);
}
getch();
}

Output:
Enter an integer: 2

2*1=2
2*2=4
2*3=6
2*4=8
2*5=10
2*6=12
2*7=14
2*8=16
2*9=18
2*10=20

21] Write a program to reverse a given number.

#include<stdio.h>
#include<conio.h> void
main()
{
int n,rem,r=0;
clrscr();
printf(“enter any no to get its reverse: ”);
scanf(“%d”,&n);
while(n>=1)
{
rem=n%10;
r=r*10+rem;
n=n/10;
}
printf(“reverse=%d”,r);
getch();
}

Output:
enter any no to get its reverse: 456
reverse=654

22] Write a program to find whether a given number is Armstrong number or not.

#include<stdio.h>
#include<conio.h> void
main()
{
int n, rem, a, res=0;
clrscr();
printf(“enter three digit number = ”);
scanf(“%d”,&a);
n=a;
while(n!=0)
{
rem=n%10;
res=res+(rem*rem*rem);
n=n/10;
}
if(res==a)
{
printf(“%d is Armstrong number ”,a);
}
else
{
printf(“%d is not an Armstrong number ”,a);
}
getch();
}

Output:
enter any no to get its reverse: 456
reverse=654

23] Write a program to print stars Sequence1:

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1; j<=i ; j++)
{
printf(“*”);
}
printf(“\n”);
}
getch();
}

Output:
*
**
***
****
*****
24] Write a program to print stars Sequence2.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k;
clrscr();
for(i=1; i<=5 ; i++)
{
for(j=5; j>=i ; j--)
{
printf(“ ”);
}
for(k=1; k<=i ; k++)
{
printf(“*”);
}
printf(“\n”);
}
getch();
}

Output:
*
**
***
****
*****

25] Write a program to print stars Sequence3.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k;
clrscr();

for(i=1; i<=5; i++)


{
for(j=5; j>=i ; j--)
printf(“ ”);
{
for(k=1; k<=i*2-1; k++)

printf(“*”);
}
printf(“\n”);
}
getch();
}

Output:

*
***
*****
*******
*********

26] Write a program to find whether given no. is a prime no. or not.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,r=0;
clrscr();
printf(“Enter any no: ”);
scanf(“%d”,&n);
for(i=2;i<=n-1;i++)
{
if(n%i==0)
{
r=1;
}
break;
}
if(r==0)
printf(“prime no”);
else
printf(“Not prime”);
getch();
}

Output:
Enter any no: 16
Not prime

27] Write a program to display sum of series 1+1/2+1/3+……….+1/n.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,sum=0;
clrscr();
printf(“Enter any no: ”);
scanf(“%d”,&n);
printf(“1”);
for(i=2;i<=n-1;i++)
{
printf(“ 1/%d +”,i);
}
for(i=1; i<=n ; i++)
{
sum=sum+i;
}
printf(“ 1/%d”,n);
printf(“\nSum=1/%d”,sum+1/n);
getch();
}

Output:
Enter any no: 7
1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 + 1/7
Sum=1/28

28] Write a program to display an array.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i;
clrscr();
printf(“enter elements of an array: ”);
for(i=0;i<5;i++)
{
scanf(“%d”,&a[i]);
}
for(i=1;i<5;i++)
{
printf(“Elements = %d ”,a[i]);
}
getch();
}
Output:
enter elements of an array:
5
4
7
1
2
Elements = 5 4 7 1 2

29] Write a program to find the maximum no. in an array.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],max,i;
clrscr();
printf(“enter element for the array: ”);
for(i=0;i<5;i++)
{
scanf(“%d”,&a[i]);
}
max=a[0];
for(i=1;i<5;i++)
{
if(max<a[i])
{
max=a[i];
}
}
printf(“maximum no = %d”, max);
getch();
}

Output:
enter elements for array:
5
4
7
1
2
maximum no = 7

30] Write a program to print sum & average of 10 elements of an array.

#include<stdio.h>
#include<conio.h>

void main()
{

int a[10],i,sum=0;

float av;
clrscr();

printf(“enter elements of an array: ”);

for(i=0;i<10;i++)

{
scanf(“%d”,&a[i]);
}
for(i=0;i<10;i++)
{
sum=sum+a[i];
}
printf(“sum=%d”,sum);
av=sum/10;
printf(“average=%.2f”,av);
getch();
}

Output:
enter elements of an array:
4
5
6
1
2
3
5
5
4
7
sum=42
average=4.22

31] Write a program to display a matrix.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][2],b[3][2],i,j;
clrscr();
printf(“enter value for a matrix: ”);
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
scanf(“%d”,&a[i][j]);
}
printf(“enter value for b matrix: ”);
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
scanf(“%d”,&b[i][j]);
}
printf(“\na matrix is\n\n”);
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf(“ %d ”,a[i][j]);
}
printf(“\n”);
}
printf(“\nb matrix is\n\n”);
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf(“ %d ”,b[i][j]);
}
printf(“\n”);
}
getch();
}

Output:
enter value for a matrix:
7
8
9
4
5
6

enter value for b matrix:


3
2
1
4
5
6

a matrix is

7 8
9 4
5 6

b matrix is
3 2
1 4
5 6
32] Write a program to find sum of two matrices.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][2],b[3][2],c[3][2], i , j;
clrscr();
printf(“Enter value for 1 matrix: ”);
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
scanf(“%d”,&a[i][j]);
}
printf(“Enter value for 2 matrix: ”);
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
scanf(“%d”,&b[i][j]);
}
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
c[i][j]=a[i][j]+b[i][j];
}
printf(“Sum of matrix is\n”);
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf(“%d\t”,c[i][j]);
}
printf(“\n”);
}
getch();
}

Output:
Enter value for 1 matrix:
1
2
3
4
5
6
Enter value for 2 matrix:
4
5
6
1
3
2

Sum of matrix is 5 7
9 5
8 8

33] Write a program to find transpose of a matrix.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][2],b[2][3],i,j;
clrscr();
printf(“Enter value for matrix: ”);
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
scanf(‘%d”,&a[i][j]);
}
printf(“Matrix:\n”);
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf(“ %d ”,a[i][j]);
}
printf(“\n”);
}
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
b[j][i]=a[i][j];
}
printf(“Transpose matrix:\n”);
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
printf(“ %d ”,b[i][j]);
printf(“\n”);
}
getch();
}

Output:
Enter value for matrix:
4
5
6
1
2
3
Matrix:
4 5
6 1
2 3

Transpose matrix:
4 6 2
5 1 3
34] Write a program to find factorial of a number using functions.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,f;
int fact(int);
clrscr();
printf(“enter a no: ”);
scanf(“%d”,&a);
f=fact(a);
printf(“factorial= %d”,f);
getch();
}
int fact(int x)
{
int fac=1,i;
for(i=x;i>=1;i--)
{
fac=fac*i;
}
return(fac);
}

Output:
enter a no: 5
factorial=120

35] Write a program to swap two numbers using functions.

#include<stdio.h>
#include<conio.h>
void main()
{
void swap(int,int);
int a,b,r;
clrscr();
printf(“enter value for a & b: ”);
scanf(“%d%d”, &a, &b);
swap(a,b);
getch();
}
void swap(int a,int b)
{
int temp;
temp=a; a=b;
b=temp;
printf(“after swapping the value for a & b is : %d %d”,a,b);
}

Output:
enter value for a & b: 4 5
after swapping the value for a & b : 5 4

36] Write a program to find square of a number using functions.

#include<stdio.h>
#include<conio.h>
void main()
{
int rev(int);
int r,a;
clrscr();
printf(“enter any no: ”);
scanf(“%d”,&a);
r=rev(a);
printf(“square is : %d”,r);
getch();
}
int rev(int x)
{
return(x*x);
}

Output:
enter any no: 5
square is : 25

37] Write a program to show call by value.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,swap();
clrscr();
a=5;
b=10;
printf(”value of a=%d & value of b=%d before swap ”,a,b);
swap(a,b);
printf(“\nvalue of a =%d & b=%d after swap”,a,b);
getch();
}

int swap(int x, int y)


{
int temp;
temp=x;
x=y;
y=temp;
}

Output:
value of a=5 & value of b=10 before swap
value of a=5 & b=10 after swap
38] Write a program to show call by reference.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,*aa,*bb,swap();
clrscr();
a=5;
b=10;
aa=&a;
bb=&b;
printf(“value of a= %d & value of b=%d before swap”,a,b);
swap(aa,bb);
printf(“\nvalue of a=%d & b=%d after swap”,a,b);
getch();
}
int swap(int *x, int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}

Output:
value of a= 5 & value of b=10 before swap
value of a=10 & b=5 after swap

39] Write a program to find factorial of a number using recursion.

#include<stdio.h>
#include<conio.h>
void main()
{
int n; clrscr();
printf(“enter number: ”);
scanf(“%d”,&n);
if(n<0)
printf(“invalid number”);
else
printf(“%d!=%d”,n,fact(n));
getch();
}
int fact(int x)
{
if(x==0)
return 1;
else
return(x*fact(x-1));
}

Output:
enter number: 5
5!=120

40] Write a program to show input and output of a string.

#include<stdio.h>
#include<conio.h>
void main()
{
char a[50];
clrscr();
printf(“enter any string: ”);
gets(a);
puts(a);
getch();
}
Output:
enter any string: hi everyone
hi everyone

41]Write a program to find whether a string is palindrome or not.

#include<stdio.h>
#include<conio.h>
void main()
{
char s1[20],s2[20];
clrscr();
printf(“enter a string: ”);
scanf(“%s”,s1);
strcpy(s2,s1);
strrev(s2);
if(strcmp(s1,s2)==0)
printf(“string is a palindrome”);
else
printf(“not a palindrome string”);
getch();
}

Output:
enter a string: abc
not a palindrome string

42] Write a program to print the roll number, name, age and marks of a student using
structures

#include <stdio.h>
#include <conio.h>
int main()
{
struct student
{
int roll_no;
char name[30];
int age;
int marks;
};
struct student p1 = {1,"Brown",14,78};
printf("%d\n %s\n %d\n %d",p1.roll_no,p1.name,p1.age,p1.marks);
return 0;
}

Output:
1
Brown
14
78

43] Write a program to add two numbers using pointers.

#include<stdio.h>
#include<conio.h>
void main()
{
int *p1,*p2,sum;
clrscr();
printf(“enter two no’s: ”);
scanf(“%d%d”,&*p1,&*p2);
sum=*p1+*p2;
printf(“sum=%d”,sum);
getch();
}

Output:
enter two no’s: 10 20
sum=30
44] Write a program to subtract two number using pointers.

#include<stdio.h>
#include<conio.h>
void main()
{
int *p1,*p2,c;
clrscr();
printf(“enter two no’s: ”);
scanf(“%d%d”,&*p1,&*p2);
c=*p1-*p2;
printf(“sum=%d” ,c);
getch();
}

Output:
enter two no’s: 20 10
sum=10

45] Write a program to search an element in the array using Linear Search.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10], i, item, flag=0;
clrscr();
printf("Enter the data in the array");
for(i=0;i<10;i++)
{
scanf("%d", &a[i]);
}

printf("Enter the element to be searched");


scanf("%d", &item);
for(i=0 ; i<10 ; i++)
{
if(item==a[i])
{
flag=1;
break;
}
}
if(flag==0)
printf("Element Not Found");
else
printf("Element Found at Position =%d" ,i);
getch();
}

46] Write a program to search an element in the array using Binary Search.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20], n, mid, beg, i, end, item, loc=-1;
clrscr();
printf("Enter the number of elements to be entered\n");
scanf("%d", &n);
printf("Enter the elements in ascending order");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element to be searched");
scanf("%d", &item);
beg=0;
end=n-1;
while(beg<=end)
{
mid=(beg+end)/2;
if(item==a[mid])
{
loc = mid;
break;
}
else if(a[mid]<item)
beg = mid+1;
else
end = mid-1;
}
if(loc== -1)
printf("Element Not Present");
else
printf("Element found at =%d", loc);
getch();
}

47] Write a program to implement Bubble Sort.

#include<stdio.h>
#include<conio.h>
void bubble(int [],int);
void main()
{
int a[20],i,n;
clrscr();
printf("Enter the number of items in the array");
scanf("%d",&n);
printf("Enter the data in the array");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
bubble(a,n);
getch();
}

void bubble(int a[],int n)


{
int i,temp,j,p;
for(i=1;i<n;i++)
{
for(p=0 ; p<n-i ; p++)
{
if(a[p]>a[p+1])
{
temp = a[p];
a[p]=a[p+1];
a[p+1]=temp;
}
}
}
for(i=0; i<n ; i++)
printf("\n%d" ,a[i]);
}

48] Write a program to implement Stack using array.

#include<stdio.h>
#include<conio.h>
#include<process.h>

void push();
void pop();
void display();

int top;
int a[5];

void main()
{
int choice;
char ch;
top= -1;
clrscr();
do
{
printf("\n\t 1. PUSH");
printf("\n\t 2. POP");
printf("\n\t 3. DISPLAY");
printf("\n\t 4. EXIT");
printf("\nEnter your choice");
scanf("%d", &choice);
switch(choice)
{
case 1: push();
break;

case 2: pop();
break;
case 3: display();
break;

case 4: exit(0);

default : printf("\n BAD CHOICE");


}

printf("\ndo you want to continue y/n");


ch=getche();
}
while(ch=='y');
}

void push()
{
int item;
if(top==4)
printf("STACK IS FULL");
else
{
printf("Enter the item to be inserted");
scanf("%d",&item);
top=top+1;
a[top]=item;
//top=tope;
}
}

void pop()
{
int item;
if(top==-1)
printf("STACK IS EMPTY");
else
{
item=a[top];
top=top-1;
printf("%d is deleted", item);
//top=tope;
}
}

void display()
{
int i;
for(i=top ; i>=0 ; i--)
printf("\n%d", a[i]);
}

49] Write a program to implement Queue using array.

#include<stdio.h>
#include<conio.h>
#include<process.h>

void insert();
void delet();
void display();
int front ,rear;
int q[5];

void main()
{
int choice;
char ch;
front=-1;
rear=-1;
clrscr();
do
{
printf("\n\t 1. INSERT");
printf("\n\t 2. DELETE");
printf("\n\t 3. DISPLAY");
printf("\n\t 4. EXIT");
printf("\nEnter your choice");
scanf("%d", &choice);
switch(choice)
{
case 1: insert();
break;
case 2: delet();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nBAD CHOICE");
}
printf("\ndo you want to continue y/n");
ch=getche();
}
while(ch=='y'||'Y');
}

void insert()
{
int item;
if(((front==1)&&(rear==5))||(front==rear+1))
{
printf("QUEUE IS FULL");
}
else
{
printf("Enter the element");
scanf("%d",&item);
if(front==-1)
{
front=1; rear=1;
}
else if(rear==5)
{
rear = 0;
}
else
{
rear = rear + 1;
}
q[rear]=item;
}
}

void delet()
{
int item;
if(front==-1)
{
printf("QUEUE IS EMPTY");
}
else
{
item=q[front];
if(front==rear)
{
front=-1;
rear=-1;
}
else if(front==5)
{
front=0;
}
else
front=front+1;
printf("%d is deleted" ,item);
}
}

void display()
{
int i;
if(front==-1)
printf("QUEUE IS EMPTY");
else
{
for(i=front ; i<=rear ; i++)
{
printf("\n%d" ,q[i]);
}
}
}

50] Write a program to implement Linked List.

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<process.h>

struct node
{
int info;
struct node *next;
};

struct node *start=NULL;

void ins();
void ins_at_beg ();
void ins_at_mid();
void ins_at_end();
void del();
void del_at_beg();
void del_at_mid();
void del_at_end();
void display();
int count();

void main()
{
int ch=0,i=0,cnt;
clrscr();
while(1)
{
printf("***********menu************");
printf("\n1.insert");
printf("\n2.delete");
printf("\n3.display");
printf("\n4.count");
printf("\n5.exit");
printf ("\n enter your choice : ");
scanf("%d", &ch);

switch(ch)
{
case 1:ins();
break;
case 2:del();
break;
case 3:display();
break;
case 4:cnt=count();
printf("\n the no of nodes : %d\n", cnt);
break;
case 5:exit(1);

}
}
}

void ins()
{
int j=0,ch1=0;
printf("\n enter your choice");
printf("\n1.insert at the beggning");
printf("\n2.insert at the middle");
printf("\n3.insert at the end");
scanf ("%d",&ch1);
switch(ch1)
{
case 1:ins_at_beg();
break;
case 2:ins_at_mid();
break;
case 3:ins_at_end();

}
}
void ins_at_beg()
{
int info;
struct node *t=(struct node *)malloc(sizeof(struct node));
printf("\n enter information to be inserted in the beginning");
scanf("%d", &info);
t->info=info;
t->next=start;
start=t;
}

void ins_at_mid()
{
int inform, x, i;
struct node *t=(struct node *)malloc(sizeof(struct node));
struct node *p=start;
printf("\nenter the location after which new node to be added");
scanf("%d", &x);
for(i=1;i<x;i++)
p=p->next;
printf("\nenter information of the new node");
scanf("%d", &inform);
t->info=inform;
t->next=p->next;
p->next=t;
}

void ins_at_end()
{
int inform1;
struct node *t=(struct node *)malloc(sizeof(struct node));
struct node *p=start;

printf("\n enter information to be added");


scanf("%d",&inform1);
t->info=inform1;
while(p->next!=NULL)
p=p->next;

p->next=t;
t->next=NULL;
}

void del()
{
int k=0,ch2=0; printf("\n enter your choice");
printf("\n1.delete at the beginning");
printf("\n2.delete at the middle");
printf("\n3.delete at the end");
scanf ("%d",&ch2);
switch(ch2)
{
case 1:del_at_beg();
break;
case 2:del_at_mid();
break;
case 3:del_at_end();
break;
}
}

void del_at_beg()
{
struct node *t=start;
start=start->next;
free(t);
}

void del_at_mid()
{
int n;
struct node *cur=start;
struct node *pre=start;
printf("\nenter information to be deleted");
scanf("%d",&n);
while(cur->info!=n)
{
pre=cur;
cur=cur->next;
}
pre->next=cur->next;
free(cur);
}
void del_at_end()
{
struct node *cur=start;
struct node *pre=start;
while(cur->next!=NULL)
{
pre=cur;
cur=cur->next;
}
pre->next=NULL;
free(cur);
}

void display()
{
struct node *p=start;
printf("\n\n***************LINK LIST*****************\n\n");
while(p!=NULL)
{
printf("%d\n",p->info);
p=p->next;
}
}
int count()
{
int c=0; {
struct q=q->next;
node c=c+1;
*q=start;
}
while(q!=N return c;
ULL)
}

You might also like