C Practical File

Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

C Practical File By Harsh Singhvi

Index
1. Program to find the sum of two numbers
2. Program to find area and circumference of circle
3. Program to find the simple interest
4. Program to convert temperature from degree centigrade to Fahrenheit
5. Program to calculate sum of 5 subjects and find percentage
6. Program to swap two numbers with third variable
7. Program to swap two numbers without third variable
8. Program to Check Number is Even or Odd
9. Program to Check Leap Year
10. Program to Find the Greatest of three Numbers
11. Program to display Monday to Sunday using Switch Statement
12. Program for Arithmetic Operations using Switch Statement
13. Program to use conditional operator
14. Program to show Bitwise AND Operator between two integers
15. Program to shift the input data by two bits to left
16. Program to print sum and average of First n Natural Numbers
17. Program to find Factorial using loop
18. Program to print first n elements of Fibonacci Series
19. Program to Print Multiplication Table of number
20. Program to Reverse an Integer
21. Program to check Armstrong Number
22. Program to print star Sequence 1
23. Program to print star Sequence 2
24. Program to print star Sequence 3
25. Program to Check Whether Number is Prime or not
26. Program to display an Array
27. Program to find the Maximum Number in an Array
28. Program to print sum & average of 10 elements of an Array
29. Program to Display Matrix
30. Program to find sum of two Matrices
31. Program to find transpose of a Matrix
32. Program to find factorial of a number using Functions
33. Program to find multiplication of a two Matrix
34. Program to swap two numbers using Functions
35. Program to find square of a number using Functions
36. Program to show Call by Value
37. Program to show call by Reference
38. Program to find factorial of a number using recursion
39. Program to show input and output of a string
40. Program to find whether a string is Palindrome or not
41. Program to print the roll number, name, age and marks of a student using
structures
42. Program to add two numbers using Pointers
43. Program to subtract two number using pointers
44. Program to search an element in the array using Linear Search
45. Program to implement Linked List
1. AIM: Program to find the sum of two numbers.
Code:

#include<stdio.h>

int main() {
int a, b, sum;

printf("\nEnter two no: ");


scanf("%d %d", &a, &b);

sum = a + b;

printf("Sum : %d", sum);

return(0);
}

Output:

Enter two no: 5 6


Sum : 11

2. AIM: Program to find area and circumference of circle.


Code:

#include<stdio.h>

int main() {

int rad;
float PI = 3.14, area, ci;

printf("\nEnter radius of circle: ");


scanf("%d", &rad);

area = PI * rad * rad;


printf("\nArea of circle : %f ", area);

ci = 2 * PI * rad;
printf("\nCircumference : %f ", ci);

return (0);
}

Output:
Enter radius of a circle : 1
Area of circle : 3.14
Circumference : 6.28

3. AIM: Program to find the simple interest.


Code:

#include<stdio.h>

int main() {
int amount, rate, time, si;

printf("\nEnter Principal Amount : ");


scanf("%d", &amount);

printf("\nEnter Rate of Interest : ");


scanf("%d", &rate);

printf("\nEnter Period of Time : ");


scanf("%d", &time);

si = (amount * rate * time) / 100;


printf("\nSimple Interest : %d", si);

return(0);
}

Output:

Enter Principal Amount : 500


Enter Rate of interest : 5
Enter Period of Time : 2
Simple Interest : 50

4. AIM: Program to convert temperature from degree


centigrade to Fahrenheit.
Code:

#include<stdio.h>

int main() {
float celsius, fahrenheit;

printf("\nEnter temp in Celsius : ");


scanf("%f", &celsius);

fahrenheit = (1.8 * celsius) + 32;


printf("\nTemperature in Fahrenheit : %f ", fahrenheit);
return (0);
}

Output:

Enter temp in Celsius : 32


Temperature in Fahrenheit : 89.59998

5. AIM: Program to calculate sum of 5 subjects and find


percentage.
Code:

#include<stdio.h>

int main() {
int s1, s2, s3, s4, s5, sum, total = 500;
float per;

printf("\nEnter marks of 5 subjects : ");


scanf("%d %d %d %d %d", &s1, &s2, &s3, &s4, &s5);

sum = s1 + s2 + s3 + s4 + s5;
printf("\nSum : %d", sum);

per = (sum * 100) / total;


printf("\nPercentage : %f", per);

return (0);
}

Output:

Enter marks of 5 subjects : 80 70 90 80 80


Sum : 400
Percentage : 80.00

6. AIM: Program to swap two numbers with third variable.


Code:

#include<stdio.h>
int main() {
double first, second, temp;
printf("Enter first number: ");
scanf("%lf", &first);
printf("Enter second number: ");
scanf("%lf", &second);
temp = first;
first = second;
second = temp;
printf("\nAfter swapping, firstNumber = %.2lf\n", first);
printf("After swapping, secondNumber = %.2lf", second);
return 0;
}

Output:

Enter first number: 1.20


Enter second number: 2.45

After swapping, firstNumber = 2.45


After swapping, secondNumber = 1.20

7. AIM: Program to swap two numbers without third


variable.
Code:

#include<stdio.h>
int main()
{
int a=10, b=20;
printf("Before swap a=%d b=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\nAfter swap a=%d b=%d",a,b);
return 0;
}

Output:

Before swap a=10 b=20


After swap a=20 b=10

8. AIM: Program to Check Number is Even or Odd.


Code:

#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);

if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);

return 0;
}

Output:

Enter an integer: -7
-7 is odd.

9. AIM: Program to Check Leap Year.


Code:

#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);

if (year % 400 == 0) {
printf("%d is a leap year.", year);
}
else if (year % 100 == 0) {
printf("%d is not a leap year.", year);
}
else if (year % 4 == 0) {
printf("%d is a leap year.", year);
}
else {
printf("%d is not a leap year.", year);
}

return 0;
}

Output:

Enter a year: 1900


1900 is not a leap year.

10. AIM: Program to Find the Greatest of three Numbers.


Code:

#include <stdio.h>
int main() {
double n1, n2, n3;
printf("Enter three different numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
if (n1 >= n2 && n1 >= n3)
printf("%.2f is the largest number.", n1);
if (n2 >= n1 && n2 >= n3)
printf("%.2f is the largest number.", n2);
if (n3 >= n1 && n3 >= n2)
printf("%.2f is the largest number.", n3);

return 0;
}

Output:

Enter three numbers: -4.5


3.9
5.6
5.60 is the largest number.

11. AIM: Program to display Monday to Sunday using Switch


Statement.
Code:

#include<stdio.h>
#include<conio.h>
int main()
{
int num;
printf("Enter a no.: ");
scanf("%d",&num);
switch(num)
{
case 1: printf("Monday");
break;
case 2: printf("Tuesday");
break;
case 3: printf("Wednesday");
break;
case 4: printf("Thursday");
break;
case 5: printf("Friday");
break;
case 6: printf("Saturday");
break;
case 7: printf("Sunday");
break;
default: printf("You have entered a wrong choice.");
break;
}
return 0;
}

Output:

Enter a no.: 1
Monday

12. AIM: Program for Arithmetic Operations using Switch


Statement.
Code:

#include<stdio.h>
#include<conio.h>
int main()
{
int a,b;
int op;
printf(" 1.Addition\n 2.Subtraction\n 3.Multiplication\n 4.Division\n");
printf("Enter the values of a & b: ");
scanf("%d %d",&a,&b);
printf("Enter your Choice : ");
scanf("%d",&op);
switch(op)
{
case 1 :
printf("Sum of %d and %d is : %d",a,b,a+b);
break;
case 2 :
printf("Difference of %d and %d is : %d",a,b,a-b);
break;
case 3 :
printf("Multiplication of %d and %d is : %d",a,b,a*b);
break;
case 4 :
printf("Division of Two Numbers is %d : ",a/b);
break;
default :
printf(" Enter Your Correct Choice.");
break;
}
return 0;
}

Output:

1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter the values of a & b: 20 15
Enter your Choice : 1
Sum of 20 and 15 is : 35

13. AIM: Program to use conditional operator.


Code:

#include<stdio.h>
#include<conio.h>
int main()
{
int a, b, Greatest;
printf("Enter two numbers: ");
scanf("%d", &a);
scanf("%d", &b);
Greatest = (a >= b) ? a : b;
printf("Greatest = %d", Greatest);
return 0;
}

Output:

Enter two numbers: 12 1


Greatest = 12

14. AIM: Program to show Bitwise AND Operator between two


integers.
Code:

#include<stdio.h>
#include<conio.h>
int main()
{
int a, b, bitwiseAND;
printf("Enter two numbers: ");
scanf("%d", &a);
scanf("%d", &b);
bitwiseAND = a&b;
printf("Bitwise AND = %d", bitwiseAND);
return 0;
}

Output:

Enter two numbers: 12 25


Bitwise AND = 8
15. AIM: Program to shift the input data by two bits to
left.

Code:

#include<stdio.h>
#include<conio.h>
int main()
{
int a;
printf("Enter a number: ");
scanf("%d", &a);
printf("Data Shifted by 2 bits to left = %d", a << 2);
return 0;
}

Output:

Enter a number: 2
Data Shifted by 2 bits to left = 8

16. AIM: Program to print sum and average of First n


Natural Numbers.

Code:

#include<stdio.h>

int main() {
int i = 1, n, sum=0;
printf("Enter Number: ");
scanf("%d", &n);

for (i = 1; i <= n; i++) {


sum += i;
}

printf("SUM = %d", sum);


printf("AVG = %d", sum/n);

return (0);
}

Output:

Enter Number: 3
SUM = 6
AVG = 2
17. AIM: Program to find Factorial using loop.

Code:

#include<stdio.h>
int main()
{
int i,fact=1,number;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=number;i++){
fact=fact*i;
}
printf("Factorial of %d is: %d",number,fact);
return 0;
}

Output:

Enter a number: 5
Factorial of 5 is: 120

18. AIM: Program to print first n elements of Fibonacci


Series.

Code:

#include<stdio.h>
int main()
{
int n1=0,n2=1,n3,i,number;
printf("Enter the number of elements:");
scanf("%d",&number);
printf("\n%d %d",n1,n2);
for(i=2;i<number;++i)
{
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
}
return 0;
}

Output:

Enter the number of elements:15


0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
19. AIM: Program to Print Multiplication Table of number.

Code:

#include <stdio.h>
int 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);
}
return 0;
}

Output:

Enter an integer: 9
9 * 1 = 9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90

20. AIM: Program to Reverse an Integer.

Code:

#include <stdio.h>
int main() {
int n, rev = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0) {
remainder = n % 10;
rev = rev * 10 + remainder;
n /= 10;
}
printf("Reversed number = %d", rev);
return 0;
}

Output:
Enter an integer: 2345
Reversed number = 5432

21. AIM: Program to check Armstrong Number.


Code:

#include<stdio.h>
int main()
{
int n,r,sum=0,temp;
printf("enter the number: ");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
printf("armstrong number ");
else
printf("not armstrong number");
return 0;
}

Output:

enter the number: 153


armstrong number

22. AIM: Program to print star Sequence 1.


Code:

#include<stdio.h>
#include<conio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1; j<=i ; j++)
{
printf("*");
}
printf("\n");
}
return 0;
}

Output:

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

23. AIM: Program to print star Sequence 2.


Code:

#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,k;
for(i=1; i<=5 ; i++)
{
for(j=5; j>=i ; j--)
{
printf(" ");
}
for(k=1; k<=i ; k++)
{
printf("*");
}
printf("\n");
}
return 0;
}

Output:

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

24. AIM: Program to print star Sequence 3.


Code:

#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,k;
for(i=1; i<=5; i++)
{
for(j=5; j>=i ; j--)
printf(" ");
{
for(k=1; k<=i*2-1; k++)
printf("*");
}
printf("\n");
}
return 0;
}

Output:

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

25. AIM: Program to Check Whether Number is Prime or not.


Code:

#include<stdio.h>

int main() {
int num, i, count = 0;

printf("Enter a number: ");


scanf("%d", &num);

for (i = 2; i <= num / 2; i++) {


if (num % i == 0) {
count++;
break;
}
}

if (count == 0)
printf("%d is a prime number", num);
else
printf("%d is not a prime number", num);

return 0;
}

Output:
Enter a number: 12
12 is not a prime number

26. AIM: Program to display an Array.


Code:

#include<stdio.h>
#include<conio.h>
int main()
{
int a[5],i;
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]);
}
}

Output:

enter elements of an array:


5
4
7
1
2
Elements = 5 4 7 1 2

27. AIM: Program to find the Maximum Number in an Array.


Code:

#include<stdio.h>
#include<conio.h>
int main()
{
int a[5],max,i;

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

Output:

enter elements for array:


5
4
7
1
2
maximum no = 7

28. AIM: Program to print sum & average of 10 elements of


an Array.
Code:

#include<stdio.h>
#include<conio.h>
int main() {
int a[10],i,sum=0;
float av;
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);
return 0;
}

Output:

enter elements of an array:


4
5
6
1
2
3
5
5
4
7
sum=42
average=4.22

29. AIM: Program to Display Matrix.


Code:

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

int main()
{
int a[3][2],b[3][2],i,j;
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");
}
return 0;
}

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

30. AIM: Program to find sum of two Matrices.


Code:

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

int main()
{
int a[3][2], b[3][2] ,c[3][2] ,i , j;
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");
}
return 0;
}

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 matrixis
5 7
9 5
8 8

31. AIM: Program to find transpose of a Matrix.


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

int main()
{
int a[3][2], b[2][3], i, j;

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

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

32. AIM: Program to find factorial of a number using


Functions.
Code:

#include<stdio.h>
#include<conio.h>
int main()
{
int a,f;
int fact(int);
printf("enter a no: ");
scanf("%d",&a);
f=fact(a);
printf("factorial= %d",f);
return 0;
}

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

33. AIM: Program to find multiplication of a two Matrix.


Code:

#include<stdio.h>
#include<stdlib.h>
int main(){
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}

printf("multiply of the matrix=\n");

for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}

for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}

Output:

enter the number of row=3


enter the number of column=3
enter the first matrix element=
1 1 1
2 2 2
3 3 3
enter the second matrix element=
1 1 1
2 2 2
3 3 3
multiply of the matrix=
6 6 6
12 12 12
18 18 18

34. AIM: Program to swap two numbers using Functions.


Code:

#include<stdio.h>
#include<conio.h>
int main()
{
void swap(int,int);
int a,b,r;

printf("enter value for a & b: "); scanf("%d%d", &a, &b);


swap(a,b);

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:

entervaluefora&b:4 5
after swapping the value for a & b : 5 4

35. AIM: Program to find square of a number using


Functions.
Code:

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

int main() {
int square(int);
int r,a;
printf("enter any no: ");
scanf("%d",&a);
r=square(a);
printf("square is : %d",r);
return 0;
}

int square(int x)
{
return x*x ;
}

Output:

enter any no: 5


square is : 25

36. AIM: Program to show Call by Value.


Code:

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

int main()
{
int a,b,swap();
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);
}
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

37. AIM: Program to show call by Reference.


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

int 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);
return 0;
}
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

38. AIM: Program to find factorial of a number using


recursion.
Code:

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

int main() {
int n;
printf("enter number: ");
scanf("%d",&n);
if(n<0)
printf("invalid number");
else
printf("%d!=%d",n,fact(n));
return 0;
}

int fact(int x) {
if(x==0)
return 1;
else
return (x*fact(x-1));
}

Output:

enter number: 5
5!=120

39. AIM: Program to show input and output of a string.


Code:

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

int main()
{
char a[50];
printf("enter any string: ");
gets(a);
puts(a);
return 0;
}

Output:

enter any string: hi everyone


hi everyone

40. AIM: Program to find whether a string is palindrome


or not.
Code:

#include<stdio.h>
#include<conio.h>
int main()
{
char s1[20],s2[20];
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");
return 0;
}
Output:

enter a string: abc


not a palindrome string

41. AIM: Program to print the roll number, name, age and
marks of a student using structures.
Code:

#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

42. AIM: Program to add two numbers using pointers.


Code:

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

int main()
{
int *p1,*p2,sum;
printf("enter two no’s: ");
scanf("%d%d",&*p1,&*p2);
sum=*p1+*p2;
printf("sum=%d",sum);
return 0;
}

Output:

enter two no’s: 10 20


sum=30

43. AIM: Program to subtract two number using pointers.


Code:

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

Output:

enter two no’s: 20 10


sum=10

44. AIM: Program to search an element in the array using


Linear Search.
Code:

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

int main()
{
int a[10], i, item, flag=0;

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

Output:

Enter the data in the array1 2 3 5 7 12 43 45 12 12


Enter the element to be searched12
Element Found at Position =5

45. AIM: Program to implement Linked List.


Code:

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

int main() {
int ch=0,i=0,cnt;

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 node *q=start;
while(q!=NULL)
{
q=q->next; c=c+1;
}
return c;
}

Output:

***********menu******** ****
1.insert
2.delete
3.display
4.count
5.exit
enter your choice : 1

enter your choice


1.insert at the beggning
2.insert at the middle
3.insert at the end1

enter information to be inserted in the beginning12


***********menu******** ****
1.insert
2.delete
3.display
4.count
5.exit
enter your choice :3

***************LINK LIST*****************

12
***********menu******** ****
1.insert
2.delete
3.display
4.count
5.exit
enter your choice : 1

enter your choice


1.insert at the beggning
2.insert at the middle
3.insert at the end1

enter information to be inserted in the beginning12


***********menu******** ****
1.insert
2.delete
3.display
4.count
5.exit
enter your choice :3

***************LINK LIST*****************

12
12
***********menu******** ****
1.insert
2.delete
3.display
4.count
5.exit
enter your choice :5

You might also like