0% found this document useful (0 votes)
242 views109 pages

C&DS LabManual

The document contains programs to calculate the sum of digits of a number, print the Fibonacci series, find prime numbers between 1 and n, calculate a series involving factorials and powers of x, find roots of a quadratic equation, calculate factorials recursively and iteratively, find the greatest common divisor of two numbers recursively and iteratively, and implement the Towers of Hanoi puzzle. The programs demonstrate basic programming concepts like loops, functions, recursion, and mathematical operations.

Uploaded by

mit08
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
242 views109 pages

C&DS LabManual

The document contains programs to calculate the sum of digits of a number, print the Fibonacci series, find prime numbers between 1 and n, calculate a series involving factorials and powers of x, find roots of a quadratic equation, calculate factorials recursively and iteratively, find the greatest common divisor of two numbers recursively and iteratively, and implement the Towers of Hanoi puzzle. The programs demonstrate basic programming concepts like loops, functions, recursion, and mathematical operations.

Uploaded by

mit08
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 109

Computer Programming Lab Manual

Program 1(a):-

Aim: A Program to find sum of individual digits of a given number.

#include<stdio.h>
#include<conio.h>
void main( )
{
int n,s=0,r;
clrscr( );
printf(“enter any number”);
scanf(“%d”,&n);
while(n>0)
{
r=n%10;
s=s+r;
n/=10;
}
printf(“sum=%d”,s);
getch( );
}

Input: enter any number


234
Output: 9

1
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual

Program 1(b):-

Aim: A Program to print Fibonacci series.

#include<stdio.h>
#include<conio.h>
void main( )
{
int a,b,c,n;
clrscr( );
printf("enter the value of n: \t");
scanf("%d",&n);
a=0;
b=1;
printf("%d\n%d\n",a,b);
c=a+b;
do
{
printf("%d ",c);
a=b;
b=c;
c=a+b;
}
while(c<=n);
getch( );
}

Input: enter the value of n


8
Output:0 1 1 2 3 5 8 13

2
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual

Program 1(c):-

Aim: A Program to print prime numbers between 1 and n.

void main( )
{
int i,j,n,;
clrscr( );
printf(“enter any number”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
for(j=2;j<i;j++)
{
if(i%j==0)
count++;
}
if(count==0)
printf(“%d\t”,i);
}
getch( );
}

Input: enter any number


4
Output: 2 3

3
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual

Program 2(a):-

Aim: A Program to calculate the following sum:

1-x2/2! + x4/4! - ---- n terms.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main( )
{
int i,n,fact,x;
float sum=1;
clrscr( );
printf("Enter the Value\n");
scanf("%d",&n);
printf("Enter the value of x:\n");
scanf("%d",&x);
for(i=2;i<=2*(n-1);i+=2)
{
fact*=i*(i-1);
sum*(-1)i=pow(x,i)/(fact,i);
}
printf("Result= %f",sum);
getch( );
}

Input: Enter the value


2
Enter the value of x
1
4
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
Output: Result=1.5

Program 2(b):-
Aim: A Program to find roots of a quadratic equation.

#include<stdio.h>
#include<conio.h>
void main( )
{
int a,b,c,d;
float r1,r2;
clrscr( );
printf(“enter a,b,c”);
scanf(“%d%d%d”,&a,&b,&c);
d=b*b-4*a*c;
if(d>0)
{
r1=((-b+sqrt(d))/(2*a));
r2=((-b-sqrt(d))/(2*a));
printf(“roots are %f,%f”,r1,r2);
}
else if (d==0)
{
r1=r2=(-b/(2*a));
printf(“roots are %f,%f”,r1,r2);
}
else
{
printf(“roots are imaginary”);
}
getch( );
}
Input: enter a,b,c
5
5
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
6
8
Output: roots are imaginary

Program 3(a):-

Aim: A Program to find factorial of a given number using recursion.

#include<stdio.h>
#include<conio.h>
int factorial(int);
void main( )
{
int n,k;
printf(“enter any number\n”);
scanf(“%d”,&n);
k=factorial(n);
printf(“factorial of %d is %d”,n,k);
getch( );
}
int factorial(int x)
{
int fact;
if(x==0||x==1)
return(1);
else
{
fact=(x*factorial(x-1));
return(fact);
}
}

Input: enter any number


4
6
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
Output: factorial of 4 is 24

Program 3(b):-

Aim: A Program to find factorial without using recursion.

#include<stdio.h>
#include<conio.h>
void main( )
{
int i,n,fact=1;
clrscr( );
printf(“enter any number\n”);
scanf(“%d”,&n); 2
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf(“factorial of %d is %d”,n,fact);
getch( );
}

Input: enter any number


4
Output:factorial of 4 is 24

7
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual

Program 4(a):-

Aim: A Program to find gcd of two numbers.

#include<stdio.h>
#include<conio.h>
void main( )
{
int l,m,n;
clrscr( );
printf("enter any two values");
scanf("%d%d",&m,&n);
do
{
l=n%m;
n=m;
m=l;
}
while(l!=0);
printf("gcd is %d",n);
getch( );
}

Input: enter any two values


2
3
Output: gcd is 1

8
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual

Program 4(b):-

Aim: A Program to find gcd of two numbers using recursion.

#include<stdio.h>
#include<conio.h>
int gcd(int a,int b);
void main( )
{
int a,b;
clrscr( );
printf("enter any two values");
scanf("%d%d",&a,&b);
printf("gcd is %d",gcd(a,b));
getch( );
}
int gcd(int a,int b)
{
int x,y;
x=a;
y=b;
if(y!=0)
{
x=gcd(y,x%y);
}
return(x);
}
Input: enter any two values
6
24
9
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
Outpuftgbbn b t: gcd is 6

Program 5(a):

Aim: A Program to implement towers of hanoi.

#include <stdio.h>
#include <conio.h>
#include <dos.h>
typedef unsigned int uint;
#define NUM_ELEMENTOS 3
int torres[NUM_ELEMENTOS][3];
void mostraTela()
{
int j, k, posx, posy;
int i;
clrscr( );
for(j = 0; j < 3; j++)
{
for(i = NUM_ELEMENTOS-1; i >= 0; i--)
{
posy = 20 - i;
posx = 20 + (j * 20);
for(k = 1; k <= torres[i][j]; k++)
{ gotoxy(posx-k, posy);
putch('-');
gotoxy(posx+k, posy);
putch('-');
}
gotoxy(posx, posy);
if(torres[i][j])
printf("%d", torres[i][j]); else

putch('|');

10
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
}
}
}

void move(uint src, uint dst)


{
int i, is, id;
for(i = NUM_ELEMENTOS-1; i >= 0; i--)
{ if(torres[i][src])
{
is = i;
break;
}
}

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


{
if(!torres[i][dst])
{
id = i;
break;
}
}
torres[id][dst] = torres[is][src];
torres[is][src] = 0;
mostraTela( );
gotoxy(1,1);
printf("Moveu peca %u da torre %u para a torre %u.", torres[id][dst], src,
dst);
getch( );
}
int podemover(uint peca, uint src, uint dst)
{
int i, is, id;

11
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
for(i = NUM_ELEMENTOS-1; i >= 0; i--)
{
if(torres[i][src])
{
is = i;

break;
}
}
for(i = NUM_ELEMENTOS-1; i >= 0; i--)
{
if(torres[i][dst]) {
id = i;
break;
}
}

return (torres[is][src] == peca)


&& ((torres[0][dst] == 0) || torres[id][dst] > torres[is]
[src]);
}

uint ondeesta(uint peca)


{
int i, j;
for(j = 0; j < 3; j++) {
for(i = NUM_ELEMENTOS-1; i >= 0; i--)
{
if(torres[i][j] == peca) {
return j;
}
}
}
return -1;
}

12
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
void ranoi(uint n, uint src, uint dst)
{
uint i, j, jog = 0, stemp, dtemp, peca,auxtemp;
int sentido = 1;

if(src > 2 || dst > 2 || src == dst || n == 0)


return;

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


{
jog = (2 * jog) + 1;
}

if((n % 2) == 0)
sentido = -1;
peca = 1;
for(i = 0; i < jog; i++)
{
stemp = ondeesta(peca);
if(peca % 2)
{
dtemp = ((3 + stemp) + sentido) % 3;
}
else
{
if(podemover(peca, stemp, (stemp + 1)%3))
dtemp = (stemp + 1)%3;
else
dtemp = (stemp + 2)%3;
}
move(stemp, dtemp);
peca = (peca % n) + 1;
stemp = ondeesta(peca);
while(!podemover(peca, stemp, (stemp + 1)%3) && !podemover(peca,
stemp, (stemp + 2)%3))

13
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
{
peca = (peca % n) + 1;
stemp = ondeesta(peca);
}
}
}
int main( )
{
uint i, j, src = 0, dst = 1;

for(j = 0; j < 3; j++) {


for(i = 0; i < NUM_ELEMENTOS; i++) {
if(j == src)

torres[i][j] = NUM_ELEMENTOS - i;
else
torres[i][j] = 0;
}
}
mostraTela( );
gotoxy(1,1);
getch( );
ranoi(NUM_ELEMENTOS, src, dst);
gotoxy(1,1);
getch( );
return 0;
}

14
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual

Output:
-1- | |
--2-- | |
---3--- | |

| | |
--2-- | |
---3--- -1- |

| | |
| | |
---3--- -1- --2—

| | |
| | -1-
---3--- | --2—

| | |
| | -1-
| ---3--- --2--

| | |
| | |

15
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
-1- ---3--- --2—

| | |
| --2-- |
-1- ---3--- |

| -1- |
| --2-- |
| ---3--- |

Program 5(b):-

Aim: A Program to solve Towers of Hanoi using recursion

#include<stdio.h>
#include<stdlib.h>
#define N 3
int A[N], B[N], C[N];
void Hanoi(int,int*,int*,int*);
void PrintAll(void)
{
int i;

printf("A: ");
for(i=0;i<N;i++)printf(" %d ",A[i]);
printf("\n");

printf("B: ");

16
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
for(i=0;i<N;i++)printf(" %d ",B[i]);
printf("\n");

printf("C: ");
for(i=0;i<N;i++)printf(" %d ",C[i]);
printf("\n");

printf("-----------------------------------------\n");
return;
}
int Move(int *source, int *dest)
{
int i=0,j=0;

while((*(source + i)==0)&&(i<N))i++;
while((*(dest + j)==0)&&(j<N))j++;

*(dest+j-1) = *(source+i);
*(source + i) = 0;
PrintAll( );
return *(dest+j-1);
}
void Hanoi(int n,int *source, int *dest, int *spare)
{
int i;
if(n==1){
Move(source,dest);
return;
}

Hanoi(n-1,source,spare,dest);
Move(source,dest);
Hanoi(n-1,spare,dest,source);
return;

17
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
}
int main( )
{
int i;
for(i=0;i<N;i++)A[i]=i+1;
for(i=0;i<N;i++)B[i]=0;
for(i=0;i<N;i++)C[i]=0;
printf("Solution of Tower of Hanoi Problem with
%d Disks\n\n",N);
printf("Starting state:\n");
PrintAll( );
printf("\n\nSubsequent states:\n\n");
Hanoi(N,A,B,C);
return 0;
}

Output: Solution of Tower of Hanoi Problem with 3 Disks

Starting state:
A: 1 2 3
B: 0 0 0
C: 0 0 0
------------------------------------------
Subsequent states:
A: 0 2 3
B: 0 0 1
C: 0 0 0
------------------------------------------
A: 0 0 3
B: 0 0 1

18
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
C: 0 0 2
------------------------------------------
A: 0 0 3
B: 0 0 0
C: 0 1 2
------------------------------------------
A: 0 0 0
B: 0 0 3
C: 0 1 2
------------------------------------------
A: 0 0 1
B: 0 0 3
C: 0 0 2
------------------------------------------
A: 0 0 1
B: 0 2 3
C: 0 0 0
------------------------------------------
A: 0 0 0
B: 1 2 3
C: 0 0 0
------------------------------------------

Program 6(a):-

Aim: The total distance traveled by vehicle in ‘t’ seconds is given by


Distance=ut+1/2a*a where ‘u’ and ‘a’ are the intial velocity (m/sec) and
accleration (m/sec2).write a C program to find the distance traveled at
regular intervals of time given the values of ‘u’ and ‘a’.The program should
provide the flexibility to the user to select his own time intervals and
repeat the calculations for different values of ‘u’ and ‘a’.
Program:
#include<stdio.h>
#include<conio.h>
void main()
19
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
{
int u,a,t,i;
float d;
clrscr();
printf("Enter the value of u,a \n");
scanf("%d%d",&u,&a);
for(i=0;i<=50;i++)
{
t=i;
d=(u*t)+(0.5*a*t*t);
}
printf("%g",d);
getch();
}

Input: Enter the value of u,a


2
3
Output: 3850

Program 6(b):-

AIM: A Program to perform arithmetic operation using switch-case.


PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
int ch;
clrscr();
printf("Enter the value of a\t\n");
scanf("%d",&a);

20
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
printf("Enter the value of b \t \n");
scanf("%d",&b);
printf("***********");
printf("\n MENU \n");
printf("***********");
printf("\n 1.Addition \n");
printf("\n 2.Subtraction \n");
printf("\n 3.Multiplication \n");
printf("\n 4.Division \n");
printf(“\n 5.Exit\n”);
printf("\n Enter ur choice\t\n") ;
while(ch!=5)
{
scanf("%d",&ch);
switch(ch)
{
case 1:
c=a+b;
printf("The sum is: %d",c);
break;
case 2:
c=a-b;
printf("The difference is: %d",c);
break;
case 3:
c=a*b;
printf("The Product is: %d",c);
break;

case 4:
c=a/b;
printf("The quotient is: %d",c);
break;

21
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual

}
}
getch();
}

Input: Enter the value of a


4
Enter the value of b
5
***********
MENU
1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter u r choice
3
Output:The Product is:20

Program 7(a):-

Aim: A Program to print the maximum and minimum elements in a list of


elements.
#include<stdio.h>
#include<conio.h>
void main( )
{
int i,n,a[10],max,min;
clrscr( );
printf(“enter size of the array”);
scanf(“%d”,&n);
printf(“enter elements”);
for(i=0;i<n;i++)
22
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
scanf(“%d”,&a[i]);
max=a[0];
min=a[0];
for(i=1;i<n;i++)
{
if(max<a[i])
max=a[i];
if(min>a[i])
min=a[i];
}
printf(“the maximum element is %d,minimum element is
%d”,max,min);
getch( );
}
Input: enter size of the array
3
enter elements
1
4
3
Output: the maximum element is 4, minimum element is 1

Program 7(b):-

Aim: A Program to perform addition and multiplication of two matrices


using functions.

#include<stdio.h>
#include<conio.h>
void add( );
void mul( );
void main( )
{
clrscr( );
23
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
add( );
mul( );
getch( );
}
void add( )
{
int i,j,m,n,p,q,a[10][10],b[10][10],c[10][10];
clrscr( );
printf(“enter order of first matrix”);
scanf(“%d%d”,&m,&n);
printf(“enter order of second matrix”);
scanf(“%d%d”,&p,&q);
if(m==p&&n==q)
{
printf(“enter elements of first matrix”);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf(“%d”,&a[i][j]);
printf(“enter elements of second matrix”);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf(“%d”,&b[i][j]);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf(“the addition matrix is\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“%d\t”,c[i][j]);

24
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
}
printf(“\n”);
}

}
else
printf(“addition is not possible”);
}
void mul( )
{
int i,j,k,m,n,p,q,a[10][10],b[10][10],c[10][10];
clrscr( );
printf(“enter order of first matrix”);
scanf(“%d%d”,&m,&n);
printf(“enter order of second matrix”);
scanf(“%d%d”,&p,&q);
if(n==p)
{
printf(“enter elements of first matrix”);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf(“%d”,&a[i][j]);
printf(“enter elements of second matrix”);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf(“%d”,&b[i][j]);
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<p;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}

25
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
}
}
printf(“the resultant matrix is\n”);
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{

printf(“%d\t”,c[i][j]);
}
printf(“\n”);
}
}
else
printf(“multiplication is not possible”);
}

Input: enter order of first matrix


2 2
enter order of second matrix
2 2
enter elements of first matrix
5 6
3 4
enter elements of second matrix
4 5
9 4
enter order of first matrix
2 2
enter order of second matrix

26
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
2 2
enter elements of first matrix
3 2
5 6
enter elements of second matrix
2 5
9 3
Output: the addition matrix is
9 11
12 8
the resultant matrix is
24 21
64 43

Program 8:-

Aim: A Program that uses functions to perform the following operations:


(i) To insert a sub string into given main string from a given position
(ii) To delete n characters from a given position in a given string.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void insert( );
void delet( );

27
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
void main( )
{
clrscr( );
insert( );
delet( );
getch( );
}
void insert( )
{
char str1[10],str2[10],str3[10];
int i,j,k,pos;
printf("enter main string");
scanf("%s",str1);
printf("enter position");
scanf("%d",&pos);
printf("enter sub string");
scanf("%s",str2);
for(i=0;i<pos;i++)
{
str3[i]=str1[i];
}
for(j=0;str2[j]!='\0';i++,j++)
{
str3[i]=str2[j];
}
for(k=pos;str1[k]!='\0';i++,k++)
{
str3[i]=str1[k];
}

str3[i]='\0';
printf("the resultant string is\n");
printf("%s",str3);
}
void delet( )

28
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
{
char str1[30],str2[10];
int i,j,pos,n,m;
printf("enter string");
scanf("%s",str1);
m=strlen(str1);
printf("enter position and no.of characters to be
deleted");
scanf("%d%d",&pos,&n);
for(j=pos-1;j<(pos+n-1);j++)
{
str1[j]='\0';
}
for(j=pos-1,i=(pos+n-1);i<m;i++,j++)
{
str1[j]=str1[i];
}
str1[j]='\0';
printf("resultant string is\n");
printf("%s",str1);
}

Input: enter main string: abcdef


enter position: 3
enter sub string: gg
Output: the resultant string is abcggdef
Input: enter sring: abcde
enter position and no. of characters to be deleted: 3 2
Output: resultant string is: abe

29
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual

Program 9:-

Aim: A Program to check whether the given string is palindrome or not.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main( )

30
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
{
char s1[10],s2[10];
clrscr( );
printf(“enter any string”);
scanf(“%s”,s1);
strcpy(s2,s1);
strrev(s2);
if(strcmp(s1,s2)==0)
printf(“given string is a palindrome”);
else
printf(“not palindrome”);
getch( );
}

Input: enter any string


mam
Output:given string is a palindrome

Program 10(a):-

Aim: A Program to display the position in string S where the string T


begins or -1 if S doesn’t contain T.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main( )
31
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
{
char s[20],t[20],*p;
clrscr( );
printf("enter main string");
scanf("%s",s);
printf("enter the string to be found");
scanf("%s",t);
p=strstr(s,t);
if(p!=NULL)
printf("the string is found at position %d",p-s);
else
printf("%d",-1);
getch( );
}

Input: enter main string


abcde
enter the string to be found
cd
Output: the string is found at position 2

Program 10(b):-
Aim: A Program to count the lines, words and characters in the given
text.
#include <stdio.h>
#include <conio.h>
void main( )
{
char c,choice;

32
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
char str[10];
int nc=0,nw=1,nl=0,count=0,i;
clrscr( );
printf("ENTER STRING:- ");
while ((c=getchar())!=EOF)
{
if(c==' '||(c>=65&&c<=90)||(c>=97&&c<=122))
nc++;
else if(c=='\n')
nl++;
if(c==' '||c=='\n')
nw++;
}
printf("no. of characters is %d",nc);
printf("no. of words is %d",nw);
printf("no. of lines is %d",nl);
fflush(stdin);
printf ("Do you want to continue?(y/n):- ");
scanf("%c",&choice);
getch( );
}
Input: ENTER STRING:-
abc def
ghi
Output: no. of characters is 10
no. of words is 3
no. of lines is 2

Program 11(a):-

Aim: A Program to print Pascal triangle


#include<stdio.h>
#include<conio.h>
void main( )
{

33
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
int a[10][10];
int i,j,c,n;
clrscr( );
printf("Enter how many lines do you want");
scanf("%d",&n);
a[1][1]=1;
printf("%5d",a[1][1]);
a[2][1]=1;a[2][2]=2;a[2][3]=1;
printf("%d %d %d",a[2][1],a[2][2],a[2][3]);
for(i=3;i<=n;i++)
{
a[i][1]=1;
printf("%d",a[i][1]);
j=2;c=3;
while(j<=i)
{
a[i][j]=a[i-1][c-1]+a[i-1][c-2];
printf("%5d",a[i][j]);
c=c+1;
j=j+1;
}
a[i][j]=1;
printf("%d", a[i][j]);
}
getch( );
}

Input: Enter how many lines do you want


3
Output: 1
1 2 1
1 3 3 1

34
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual

Program 11(b):-

Aim: A Program to print the sequence 1


2 3
4 5 6
7 8 9 10
35
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual

#include<stdio.h>
#include<conio.h>
void main( )
{
int i,j,n,k,l=1;
clrscr( );
printf(“enter number of rows”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=(n-i);j++)
printf(“ “);
for(k=1;k<=(2*i-1);k++)
{
if(k%2==0)
printf(“ “);
else
printf(“%d”,l++);
}
Printf(“\n”);
}
getch( );
}

Input:enter number of rows


4
Output: 1
2 3
4 5 6
7 8 9 10

36
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual

Program 12:-

Aim: A Program to read two numbers x and n and then compute the sum
of this geometrix progression.
1+x+x2+x3+…………+xn.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main( )
{
int i,sum=0,x,n;
printf("enter the values of x and n");
scanf("%d%d",&x,&n);
for(i=0;i<=n;i++)
{
sum=sum+pow(x,i);
}
printf("result of the expression is %d",sum);
getch( );
}

Input: enter the values of x and n


1 3
Output: result of the expression is 3

Program 13:-

Aim: A Program to find 2’s complement of a binary number.

37
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
#include<stdio.h>
#include<conio.h>
void main( )
{
int a[10],b[10],i,j,n;
clrscr( );
printf("enter no. of digits");
scanf("%d",&n);
printf("enter the binary number");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=n-1,j=0;i>=0;i--,j++)
{
if(a[i]!=1)
{
b[j]=a[i];
}
else
{
b[j]=a[i];
--i;
++j;
for(;i>=0;i--,j++)
{
if(a[i]==0)
b[j]=1;
else
b[j]=0;
}
}
}

printf("the 2's complement is ");


for(j=0;j<n;j++)
printf("%d",b[j]);
38
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
getch( );
}

Input:enter no. of digits


4
enter the binary number 1 0 0 1
Output: the 2's complement is 1110

Program 14:-

Aim: A Program to convert a Roman numeral to its decimal equivalent.

#include<stdio.h>
39
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main( )
{
clrscr( );
int *a,l,i,j,k;
char *s;
printf("Enter The Roman Number");
scanf("%s",s);
l=strlen(s);
for(i=0;i<l;i++)
{
if(s[i]=='I')
a[i]=1;
else if(s[i]=='V')
a[i]=5;
else if(s[i]=='X')
a[i]=10;
else if(s[i]=='L')
a[i]=50;
else if(s[i]=='C')
a[i]=100;
else if(s[i]=='D')
a[i]=500;
else if(s[i]=='M')
a[i]=1000;
else
{
printf("Wrong Input");
getch();
exit(0);
}
}

40
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual

k=a[l-1];

for(i=l-1;i>0;i--)
{
if(a[i]>a[i-1])

k=k-a[i-1];
else if(a[i]==a[i-1] || a[i]<a[i-1])
k=k+a[i-1];
}
printf("%d",k);
getch( );
}

Input: Enter The Roman Number


VI
Output: 6

Program 15:-

Aim: A Program to add and multiply two complex numbers (x+iy) and
(a+ib).

41
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual

#include<stdio.h>
#include<conio.h>
struct complex add(struct complex,struct complex);
struct complex mul(struct complex,struct complex);
struct complex
{
int real,imag;
};
void main( )
{
struct complex c1,c2,c3,c4;
printf("enter first number");
scanf("%d%d",&c1.real,&c1.imag);
printf("enter second number");
scanf("%d%d",&c2.real,&c2.imag);
c3=add(c1,c2);
c4=mul(c1,c2);
printf("addition is %d+i%d\n",c3.real,c3.imag);
printf("subtraction is %d+i%d",c4.real,c4.imag);
getch();
}
struct complex add(struct complex c1,struct complex c2)
{
struct complex c3;
c3.real=c1.real+c2.real;
c3.imag=c1.imag+c2.imag;
return(c3);
}
struct complex mul(struct complex c1,struct complex c2)
{

struct complex c4;


c4.real=(c1.real*c2.real)-(c1.imag*c2.imag);

42
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual

c4.imag=(c1.imag*c2.real)+(c1.real*c2.imag);
return(c4);
}

Input: enter first number 2 3


enter second number 2 3
Output: addition is 4+i6
multiplication is –5+i12

Program 16(a):-

43
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
Aim: A Program to copy contents of one file to another.

#include<stdio.h>
#include<conio.h>
void main( )
{
FILE *fs,*ft;
char ch;
fs=fopen(“file1.txt”,”r”);
if(fs==NULL)
{
printf(“cannot open file”);
exit(0);
}
ft=fopen(”file2.txt”,”w”);
if(ft==NULL)
{
printf(“cannot open file”);
exit(0);
}
while((ch=getc(fs))!=EOF)
{
putc(ch,ft);
}
fclose(ft);
printf(“ the copied contents are \n”);
ft=fopen(“file2.txt”, “r”);
while((ch=getc(ft))!=EOF)
{
putchar(ch);
}
fclose(fs);
fclose(ft);
getch( );
}

44
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
Output: the copied contents are
Marconi Institute of technology.

Program 16(b):-

Aim: A Program to reverse the first n characters in a file.

#include<stdio.h>
#include<conio.h>
void main( )
{
FILE *fp;
char ch,str[10];
int i,n;
clrscr( );
fp=fopen(“file1.txt”,”r”);
if(fp==NULL)
{
printf(“file cannot be opened”);
exit(0);
}
printf(“enter n”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
str[i]=getc(fp);
}
for(i=n-1;i>=0;i--)
{
putchar(str[i]);
}
while((ch=getc(fp))!=EOF)
{
putchar(ch);
45
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
}
getch( );
}

Output:enter n
4
college of engineering and technology

Program 17:-

Aim: A Program that uses functions to perform the following operations


on a singly linked list.
(a) Creation (b) Insertion (c) Deletion (d) traversal

#include<stdio.h>
#include<conio.h>
void create(int);
void insatend(int);
void insatpos(int,int);
void delatbeg( );
void delatend( );
void delatpos(int);
void traversal( );
struct node
{
int ele;
struct node *next;
};
struct node *start=NULL;
void main( )
{
int ch,ele;
printf(“1.create 2.insatend 3.insertion at position

46
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
4.delatbeg 5.delatend 6.deletion at position 7.traversal”);
do
{
printf(“enter ur choice”);
scanf(“%d”,&ch);
switch(ch)
{
case 1: printf(“enter element to be inserted”);
scanf(“%d”,&ele);
create(ele);
break;
case 2: printf(“enter element to be inserted”);
scanf(“%d”,&ele);
insatend(ele);
break;

case 3: printf(“enter element and position to be


inserted”);
scanf(“%d%d”,&ele,&pos);
insatpos(ele,pos);
break;
case 4: delatbeg();
break;
case 5: delatend();
break;
case 6: printf(“enter position to be deleted”);
scanf(“%d”,&pos);
delatpos(pos);
break;
case 7: traversal();
break;
}
}
while(ch>=1&&ch<=7);

47
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
getch( );
}
void create(int ele)
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->ele=ele;
if(start==NULL)
{
start=temp;
start->next=NULL;
}
else
{
temp->next=start;
start=temp;
}
}
void insatend(int ele)
{
struct node *temp,*p;
temp=(struct node*)malloc(sizeof(struct node));
temp->ele=ele;
if(start==NULL)
{
start=temp;
start->next=NULL;
}
else
{
p=start;
while(p->next!=NULL)
p=p->next;
p->next=temp;
temp->next=NULL;

48
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
}
}
void insatpos(int ele,int pos)
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->ele=ele;
if(start==NULL)
{
if(pos==1)
{
start=temp;
start->next=NULL;
}
else
printf(“insertion is not possible at this position”);
}
else if (start->next==NULL)
{
if(pos==1)
{
temp->next=start;
start->next=NULL;
start=temp;
}
else if(pos==2)
{
start->next=temp;
temp->next=NULL;
}
else
printf(“insertion is not possible at this position”);
}
else
{

49
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
int count=0;
struct node *p;
p=start;
while(p!=NULL)
{
p=p->next;
count++;
}
if(pos<=count+1)
{
if(pos==1)
{
temp->next=start;
start=temp;
}
else
{
p=start;
for(i=1;i<pos-1;i++)
p=p->next;
temp->next=p->next;
p->next=temp;
}
}
else
printf(“insertion is not possible at this position”);
}
}
void delatbeg()
{
struct node *temp;
if(start==NULL)
printf(“list is empty”);
else if(start->next==NULL)
{

50
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
temp=start;
start=NULL;
free(temp);
}
else
{
temp=start;
start=start->next;
free(temp);
}
}
void delatend()
{
struct node *temp;
if(start==NULL)
printf(“list is empty”);
else if(start->next==NULL)
{
temp=start;
start=NULL;
free(temp);
}
else
{
struct node *p;
p=start;
while(p->next->next!=NULL)
p=p->next;
temp=p->next;
p->next=NULL;
free(temp);
}
}
void delatpos(int pos)
{

51
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
struct node *temp,*p;
int count=0;
if(start==NULL)
printf(“list is empty”);
else if (start->next==NULL)
{
if(pos==1)
{
temp=start;
start=NULL;
free(temp);
}
else
printf(“deletion is not possible at this position”);
}
else
{
p=start;
while(p!=NULL)
{
p=p->next;
count++;
}
if(pos<=count)
{
if(pos==1)
{
temp=start;
start=start->next;
free(temp);
}
else
{
for(i=1;i<pos-1;i++)
p=p->next;

52
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
temp=p->next;
p->next=p->next->next;
free(temp);
}
}
else
printf(“deletion is not possible at this position”);
}
}
void traversal()
{
struct node*p;
if(start==NULL)
printf(“list is empty”);
else
{
p=start;
while(p!=NULL)
{
printf(“%d\t”,p->ele);
p=p->next;
}
}
}

Output:1.create
2.insatend
3.insertion at position
4.delatbeg
5.delatend
6.deletion at position
7.traversal
enter your choice 1
enter element to be inserted 11

53
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
enter your choice 3
enter element and position to be inserted 23 3
enter your choice 1

Program 18:-

Aim: A Program that uses functions to perform the following operations


on a doubly linked list.
(a) Creation (b) Insertion (c) Deletion (d) traversal in both ways

#include<stdio.h>
#include<conio.h>
void create(int);
void insatend(int);
void insatpos(int,int);
void delatbeg();
void delatend();
void delatpos(int);
void traversal();
void traversalback();
struct node
{
int ele;
struct node *prev;
struct node *next;
};
struct node *start=NULL;
void main()
{
int ch,ele;
printf(“1.create 2.insatend 3.insertion at position
4.delatbeg 5.delatend 6.deletion at position 7.traversal
8.traversalback”);

54
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
do
{
printf(“enter ur choice”);
scanf(“%d”,&ch);
switch(ch)
{
case 1: printf(“enter element to be inserted”);
scanf(“%d”,&ele);
create(ele);
break;
case 2: printf(“enter element to be inserted”);
scanf(“%d”,&ele);
insatend(ele);
break;
case 3: printf(“enter element and position to be
inserted”);
scanf(“%d%d”,&ele,&pos);
insatpos(ele,pos);
break;
case 4: delatbeg();
break;
case 5: delatend();
break;
case 6: printf(“enter position to be deleted”);
scanf(“%d”,&pos);
delatpos(pos);
break;
case 7: traversal();
break;
case 8: traversalback();
break;
}
}
while(ch>=1&&ch<=8);
}

55
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
void create(int ele)
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->ele=ele;
if(start==NULL)
{
start=temp;
start->prev=NULL;
start->next=NULL;
}
else
{
temp->next=start;
temp->prev=NULL;
start=temp;
}
}
void insatend(int ele)
{
struct node *temp,*p;
temp=(struct node*)malloc(sizeof(struct node));
temp->ele=ele;
if(start==NULL)
{
start=temp;
start->prev=NULL;
start->next=NULL;
}
else
{
p=start;
while(p->next!=NULL)
p=p->next;
temp->prev=p;

56
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
p->next=temp;
temp->next=NULL;
}
}
void insatpos(int ele,int pos)
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->ele=ele;
if(start==NULL)
{
if(pos==1)
{
start=temp;
start->prev=NULL;
start->next=NULL;
}
else
printf(“insertion is not possible at this position”);
}
else if (start->next==NULL)
{
if(pos==1)
{
temp->next=start;
start->prev=temp;
start=temp;
start->prev=NULL;
}
else if(pos==2)
{
start->next=temp;
temp->prev=start;
temp->next=NULL;
}

57
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
else
printf(“insertion is not possible at this position”);
}
else
{
int count=0;
struct node *p;
p=start;
while(p!=NULL)
{
p=p->next;
count++;
}
if(pos<=count+1)
{
if(pos==1)
{
temp->next=start;
start->prev=temp;
start=temp;
start->prev=NULL;
}
else
{
p=start;
for(i=1;i<pos-1;i++)
p=p->next;
temp->next=p->next;
p->next->prev=temp;
p->next=temp;
temp->prev=p;
}
}
else
printf(“insertion is not possible at this position”);

58
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
}
}
void delatbeg()
{
struct node *temp;
if(start==NULL)
printf(“list is empty”);
else if(start->next==NULL)
{
temp=start;
start=NULL;
free(temp);
}
else
{
temp=start;
start=start->next;
start->prev=NULL;
free(temp);
}
}
void delatend()
{
struct node *temp;
if(start==NULL)
printf(“list is empty”);
else if(start->next==NULL)
{
temp=start;
start=NULL;
free(temp);
}
else
{
struct node *p;

59
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
p=start;
while(p->next->next!=NULL)
p=p->next;
temp=p->next;
p->next=NULL;
free(temp);
}
}
void delatpos(int pos)
{
struct node *temp,*p;
int count=0;
if(start==NULL)
printf(“list is empty”);
else if (start->next==NULL)
{
if(pos==1)
{
temp=start;
start=NULL;
free(temp);
}
else
printf(“deletion is not possible at this position”);
}
else
{
p=start;
while(p!=NULL)
{
p=p->next;
count++;
}
if(pos<=count)
{

60
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
if(pos==1)
{
temp=start;
start=start->next;
start->prev=NULL;
free(temp);
}
else
{
for(i=1;i<pos-1;i++)
p=p->next;
temp=p->next;
temp->next->prev=p;
p->next=p->next->next;
free(temp);
}
}
else
printf(“deletion is not possible at this position”);
}
}
void traversal()
{
struct node*p;
if(start==NULL)
printf(“list is empty”);
else
{
p=start;
while(p!=NULL)
{
printf(“%d\t”,p->ele);
p=p->next;
}
}

61
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
}
void traversalback()
{
struct node *p;
if(start==NULL)
printf(“list is empty”);
else
{
p=start;
while(p->next!=NULL)
p=p->next;
while(p!=NULL)
{
printf(“%d\t”,p->ele);
p=p->prev;
}
}
}

Output:1.create
2.insatend
3.insertion at position
4.delatbeg
5.delatend
6.deletion at position
7.traversal
8.traversalback
enter your choice 1
enter element to be inserted 11
enter your choice 3
enter element and position to be inserted 23 3
enter your choice 1

62
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual

Program 19:-

Aim: A Program to implement stack using arrays.

#define max 20
void push(int);
int pop();
void display();
int stack[max],top=-1;
void main()
{
int ch,ele;
printf(“1.Push\n2.Pop\n3.Display”);
do
{
printf(“Enter ur choice”);
scanf(“%d”,&ch);
switch(ch)
{
case 1: printf(“Enter element to be inserted”);
scanf(“%d”,&ele);

63
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
push(ele);
break;
case 2: ele=pop();
if(ele!=-1)
printf(“Deleted element is %d”,ele);
break;
case 3: display();
break;
}
}while(ch>=1&&ch<=3);
getch();
}
void push(int ele)
{
if(top>=max-1)
printf(“stack is full”);
else
{
top++;

stack[top]=ele;
}
int pop()
{
int ele;
if(top==-1)
{
printf(“Stack is empty”);
return(-1);
}
else
{
ele=stack[top];
top--;
return(ele);

64
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
}
}
void display()
{
if(top==-1)
printf(“Stack is empty”);
else
{
for(i=0;i<=top;i++)
printf(“%d” stack[i]);
}
}

Output: 1.Push
2.Pop
3.Display
Enter ur choice
1
Enter element to be inserted
11
Enter ur choice
2
Deleted element is 11
Enter ur choice
3
Stack is empty

65
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual

Program 20:-

Aim: A Program to implement stack using pointers.

#include<stdio.h>
#include<conio.h>
void push( );
void pop( );
void display( );
struct node
{
int ele;
struct node *next;
}*start;
void main( )
66
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
{
int ch;
start=NULL;
do
{
printf(“1.Push\n 2.Pop\n 3.Display\n”);
printf(“Enter ur choice”);
scanf(“%d”,&ch);
switch(ch)
{
case 1: push() ;
break;
case 2: pop( );
break;
case 3: display( );
break;
default: printf(“Enter choice”);
break;
}
}
while(ch>=1&&ch<=3);
}
void push( )
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
printf(“Enter element to be inserted”);
scanf(“%d”,&temp->ele);
temp->next=start;
start=temp;
}
void pop( )
{
struct node *temp;
if(start==NULL)

67
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
{
printf(“list is empty”);
else
{
temp=start;
printf(“Deleted element is %d”,temp->ele);
start=start->next;
free(temp);
}
}
void display( )
{
struct node *p;
p=start;
if(start==NULL)
printf(“List is empty”);
else
{
while(p!=NULL)
{
printf(“%d”,p->ele);
p=p->next;
}
}
}

Output: 1.Push
2.Pop
3.Display
Enter ur choice
1
Enter element to be inserted
11
Enter ur choice
2

68
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
Deleted element is 11
Enter ur choice
3
List is empty

Program 21:-

Aim: A Program to implement queue using arrays.

#define max 20
void enqueue(int);
int dequeue();
void display();
int queue[max],f=0,r=-1;
void main()

69
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
{
int ele, ch;
do
{
printf(“1.Enqueue\n 2.Dequeue \n3.Display\n”);
printf(“Enter ur choice”);
scanf(“%d”, &ch);
switch(ch)
{
case 1: printf(“Enter element to be inserted”);
scanf(“%d”,&ele);
enqueue(ele);
break;
case 2: ele=dequeue();
if(ele!=-1)
printf(“Deleted element is %d”,ele);
break;
case 3: display();
break;
}
}
while(ch>=1&&ch<=3);
}
void enqueue(int ele)
{
r++;
if(r>=max-1)
printf(“queue is full”);
else
{

queue[r]=ele;
}
}
int dequeue()

70
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
{
int ele;
if(f>r)
{
printf(“queue is empty”);
}

else
{
ele=queue[f];
f++;
return(ele);
}
}
void display()
{
int i;
if(f>r)
{
printf(“queue is empty”);
}
else
{
for(i=f;i<=r;i++)
printf(“%d “,queue[i]);
}
}

Output:1.Enqueue
2.Dequeue
3.Display
Enter ur choice
1
Enter element to be inserted
11

71
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
Enter ur choice
2
Deleted element is 11

Program 22:-

Aim: A Program to implement queue using pointers.

#include<stdio.h>
#include<conio.h>
void enqueue( );
void dequeue( );
void display( );
struct node
{
int ele;
struct node *next;
}*start;
void main()
{
int ch;
start=NULL;
do
{
printf(“1.Enqueue 2.Dequeue 3.Display\n”);
printf(“enter ur choice”);
scanf(“%d”,&ch);
switch(ch)
{
case 1: enqueue( );
break;
case 2: dequeue( );
break;
case 3: display( );
break;
72
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
default: printf(“Enter choice between 1 and 3”);
break;
}
}
while(ch>=1&&ch<=3);
getch( );
}
void enqueue()
{
struct node *temp,*p;
temp=(struct node*)malloc(sizeof(struct node));
printf(“Enter element to be inserted”);
scanf(“%d”,&temp->ele);
if(start==NULL)
{
start=temp;
start->next=NULL;
}
else
{
p=start;
while(p!=NULL)
{
p=p->next;
}
p->next=temp;
temp->next=NULL;
}
}
void dequeue()
{
struct node *temp;
if(start==NULL)
{
printf(“queue is empty”);

73
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
}
else
{
temp=start;
printf(“Deleted element is %d”,temp->ele);
start=start->next;
free(temp);
}
}
void display()
{
struct node *p;
p=start;
if(start==NULL)
{
printf(“queue is empty”);
}
else
{
while(p!=NULL)
{
printf(“%d”,p->ele);
p=p->next;
}
}
}

Output: Enqueue
Dequeue
Display
Enter ur choice
1
Enter element to be inserted

74
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
11
Enter ur choice
2
Deleted element is 11

Program 23:-

Aim: A Program to convert the given infix expression to postfix


expression.

#include<stdio.h>

75
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
#include<conio.h>
#include<ctype.h>
void push(char);
char pop( );
int opera(char);
int prio(char);
char stack[20];
int top=-1;
void main( )
{
char inf[10],ch;
int i;
printf(“enter infix expression”);
scanf(“%s”,inf);
for(i=0;inf[i]!=’\0’;i++)
{
if(isalnum(inf[i]))
printf(“%c”,inf[i]);
else if(opera(inf[i])
{
while(prio(inf[i])<=prio(stack[top]))
{
printf(“%c”,pop());
}
push(inf[i]);
}
else if(inf[i]==’(‘)
push(inf[i]);
else if(inf[i]==’)’)
{
while((ch=pop())!=’(‘)
{
printf(“%c”,ch);
}
}

76
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
}
while(top!=-1)
printf(“%c”,pop());
getch( );
}
int opera(char ch)
{
if(ch==’+’||ch==’-‘||ch==’*’||ch==’/’||ch==’%’)
return(1);
else
return(0);
}
int prio(char ch)
{
switch(ch)
{
case ‘+’:
case ‘-‘: return(1);
case ‘*’:
case ‘/’:
case ‘%’:return(2);
default: return(0);
}
}
void push(char ch)
{
if(top>=max-1)
{
printf(“stack is full”);
}
else
{
top++;
stack[top]=ch;
}

77
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
}
char pop( )
{
char ch;
if(top==-1)
{
printf(“stack is empty”);
exit(0);
}
else
{
ch=stack[top];
top--;
return(ch);
}
}

Input: enter any infix expression


a+b-c
Output: ab+c-

Program 24:-

Aim: A Program to evaluate the given postfix expression.

78
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
void push(int);
int pop();
int opera(char);
int stack[10],top=-1;
void main()
{
int i,x,y;
char post[10];
printf(“enter any postfix expression”);
scanf(“%s”,post);
for(i=0;post[i]!=’\0’;i++)
{
if(isdigit(post[i])
{
push(post[i]-48);
}
else if(opera(post[i])
{
x=pop();
y=pop();
switch(post[i])
{
case ‘+’: push(y+x);
break;
case ’-‘: push(y-x);
break;
case ’*’: push(y*x);
break;
case ‘/’: push(y/x);
break;
case ‘%’: push(y%x);
break;
}
}
}

79
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
if(top==0)
{
printf(“result=%d”,pop());
}
getch();
}
void push(int ele)
{
if(top>=max-1)
{
printf(“stack is full”);
}
else
{
top++;
stack[top]=ele;
}
}
int pop()
{
int ele;
if(top==-1)
{
printf(“stack is empty”);
return(-1);
}
else
{
ele=stack[top];
top--;
return(ele);
}
}
int opera(char ch)
{

80
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
if(ch==’+’||ch==’-‘||ch==’*’||ch==’/’||ch==’%’)
return(1);
else
return(0);
}
Input: enter any infix expression
12+3-
Output: 0

Program 25:-

Aim: A Program that uses functions to perform the following:


(i)Creating a Binary Tree of integers.
(ii)Traversing the above tree in preorder,inorder and postorder.

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
typedef struct bt
{
struct bt *lc;
int d;
struct bt *rc;
}node;

void insert(node **,int);


void inorder(node *);
void postorder(node *);
void preorder(node *);
void main( )
{
node *root=NULL;
int e,n,i;

81
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
clrscr( );
printf("\n Enter the Number of Nodes \n");
scanf("%d",&n);
printf("\n Enter the Elements \n");
for(i=1;i<=n;i++)
{
scanf("%d",&e);
insert(&root,e);
}
inorder(root);
postorder(root);
preorder(root);
}

void insert(node **r,int e)


{

if(*r==NULL)
{
*r=(node *)malloc(sizeof(node));
(*r)->lc=(*r)->rc=NULL ;
(*r)->d=e;
return;
}
else
{
if(e<(*r)->d)
insert(&((*r)->lc),e);
else
insert(&((*r)->rc),e);
}
}

void inorder(node *r)

82
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
{
if(r!=NULL)
{
inorder(r->lc);
printf("Inorder elements are: %d\n",r->d);
inorder(r->rc);
}
else
return;
}

void postorder(node *r)


{
if(r!=NULL)
{
postorder(r->lc);
postorder(r->rc);
printf("Postorder elements are: %d\n",r->d);
}
else
return;
}
void preorder(node *r)
{
if(r!=NULL)
{
printf("Preorder elements are: %d\n",r->d);
preorder(r->lc);
preorder(r->rc);
}
else
return;
}

83
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual

Input: Enter the Number of Nodes


4
Enter the Elements
9
10
11
12

Output: Inorder elements are: 9


Inorder elements are: 10
Inorder elements are: 11
Inorder elements are: 12
Postorder elements are: 12
Postorder elements are: 11
Postorder elements are: 10
Postorder elements are: 9
Preorder elements are: 9
Preorder elements are: 10
Preorder elements are: 11
Preorder elements are: 12

84
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual

Program 26:-

Aim: To write a program to implement linear search.

#include<stdio.h>
#include<conio.h>
void main( )
{
int a[10],i,n,search;
printf(“Enter no of elements \n”);
scanf(“%d”,&n);
printf(“Enter elements \n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
printf(“Enter element to be searched\n”);
scanf(“%d”,&search);
for(i=0;i<n;i++)
{
if(a[i]==search)
{
printf(“ %d is found at %d
position”,search,i+1);
exit();
}
}
printf(“element %d is not found”, search);
getch( );
}

Input: Enter no. of elements


4
Enter elements
5 9 3 7
Enter element to be searched
85
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
7
Output: 7 is found at 4 position

Program 27:-

Aim: A program to implement binary search.

int bin_search(int a[ ], int n, int ele);


void main( )
{
int a[10],i,n,ele,pos;
printf(“enter the size of an array \n”);
scanf(“%d”,&n);
printf(“enter elements :\n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
printf(“Enter element to be searched\n”);
scanf(“%d”,&ele);
pos=bin-search(a,n,ele);
if(pos>=0)
{
printf(“Elements %d is found at %d position”, ele,pos+1);
exit(0);
}
else
printf(“Element is not found”);
getch();
}
int bin-search(int a[ ],int n,int ele)
{
int low,high,mid;
low=0;
high=n-1;
while(low<=high)
{
86
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
mid=(low+high)/2;
if(ele==a[mid])
high=mid-1;
else
low=mid+1;
}
return(-1);
}

Input: Enter size of the array


4
Enter elements
56 90 34 76
Enter element to be searched
76
Output: 76 is found at 4 position

87
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual

Program 28:-

Aim: A program to implement Bubble sort.


#include<stdio.h>
#include<conio.h>
void main( )
{
int a[10],i,j,n,temp;
printf(“enter the size of an array \n”);
scanf(“%d”,&n);
printf(“enter elements :\n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf(“Sorted order is”);
for(i=0;i<n;i++)
printf(“%d\n”,a[i]);
getch( );
}
Input: Enter size of an array
3
Enter elements
5 9 4

88
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
Output: Sorted order is
4 5 9

Program 29:-

Aim: A program to implement Quick sort.

#include<stdio.h>
#include<conio.h>
int partition(int m,int n);
void quick_sort(int p,int q);
int a[10];
void main( )
{
int i,size,p=0,q;
printf(“Enter size of array\n”);
scanf(“%d”,&size);
q=size-1;
printf(“Enter elements:”);
for(i=0;i,size;i++)
scanf(“%d”,&a[i]);
quick_sort(p,q);
printf(“After sorting “);
for(i=0;i,size;i++)
scanf(“%d ”,&a[i]);
getch( );
}
void quick_sort(int p,int q)
{
int j;
if(p<q)
{
j=partition(p,q+1);
quick_sort(p,j-1);
quick_sort(j+1,q);
}
89
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
}
int(partition(int m,int n)
{
int key,l,r,temp;
key=a[m];
l=m;
r=n;

do
{
do
{
l++;
}
while(a[l]<key);
do
{
r--;
}
while(a[r]>key);
if(l<r)
{
temp=a[l];
a[l]=a[r];
a[r]=temp;
}
}
while(l<r);
a[m]=a[r];
a[r]=key;
return( r);
}

Input: Enter size of an array

90
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
5
Enter elements
7 2 9 1 3
Output: After sorting
1 2 3 7 9

Program 30:-
Aim: To write a program to implement Insertion sort.
#include<stdio.h>
#include<conio.h>
void main( )
{
int a[10],i,j,n,small;
clrscr( );
printf(“enter the size of an array \n”);
scanf(“%d”,&n);
printf(“enter elements :\n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
for(i=0;i<n;i++)
{
small=a[i];
j=i;
while((j>0)&&a[j-i]>small)
{
a[j]=a[j-i];
j--;
}
a[j]=small;
}
printf(“Sorted order is\n”);
for(i=0;i<n;i++)
printf(“%d ”,a[i]);
getch();
}
91
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
Input: Enter size of an array
3
Enter elements
2 7 3
Output: Sorted array is
2 3 7

Program 31:-

Aim: A program to implement merge sort.


#include<stdio.h>
#include<conio.h>
void merge_sort(int a[ ],int b[ ],int c[ ], int n1, int n2,int *n);
void main( )
{
int a[10],b[10],c[20],n1,n2,n,i;
clrscr( );
printf(“Enter size of first array:”);
scanf(“%d”,&n1);
printf(“Enter elements of first array in sorted order \n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
printf(“Enter size of second array:”);
scanf(“%d”,&n2);
printf(“Enter elements of second array in sorted order \n”);
for(i=0;i<n2;i++)
scanf(“%d”,&b[i]);
merge_sort(a,b,c,n1,n2,&n);
printf(“After sorting\n”);
for(i=0;i<n;i++)
printf(“%d ”,c[i]);
for(i=0;i<n;i++)
printf(“%d ”,c[i]);
getch( );
}
92
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
void merge_sort(int a[],int b[],int c[], int n1, int n2,int *n)
{
int i=0;j=0;k=0;
while((i<n1)&&(j<n2))
{
if(a[i]<b[j])
{
c[k]=a[i];
i++;
}
else

{
c[k]=b[j];

j++;
}
k++;
}
while(i<n1)
{
c[k]=a[i];
i++;
K++;
}
while(j<n2)
{
c[k]=b[j];
j++;
k++;
}
*n=k;
}
Input: Enter size of first array

93
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
3
Enter elements of first array in sorted order
1 2 3
Enter size of second array
4
Enter elements of second array in sorted order
4 5 6 7
Output: after sorting
1 2 3 4 5 6 7

Program 32:-

Aim: A Program to implement Lagrange Interpolation.

#include<stdio.h>
#include<conio.h>
void main( )
{
int n,i,m,j;
float x[10],y[10],t,sum,tk,k;
printf("enter number of values");
scanf("%d",&n);
printf("enter values for x and y");
for(i=0;i<n;i++)
{
scanf("%f%f",&x[i],&y[i]);
}
printf("enter the required interpolation value");
scanf("%f",&t);
sum=0;
for(k=0;k<n;k++)
{
tk=1;
for(j=0;j<n;j++)
{
94
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
if(j!=k)
{
tk=tk*((t-x[j])/(x[k]-x[j]));
}
}
sum=sum+(tk*y[k]);
}
printf("x=%f,y=%f",t,sum);
getch( );
}

Output:enter number of values3


enter values for x and y1 1 2 4 5 10
enter the required interpolation value3
x=3.000000,y=6.500000

Program 33:-

Aim: A Program to implement Newton-Gregory forward interpolation


method.

#include<stdio.h>
#include<conio.h>
void main( )
{
int n,i,k,j;
float x[20],y[20],deltay[20]
[20],a,h,p,pvalue,factvalue,term,sumy=0;
printf("enter number of pairs\n");
scanf("%d",&n);
printf("enter values for x\n");
for(i=0;i<n;i++)
scanf("%f",&x[i]);
printf("enter values for y\n");
for(i=0;i<n;i++)
95
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
scanf("%f",&y[i]);
printf("enter the x value required\n");
scanf("%f",&a);
h=x[1]-x[0];
p=(a-x[0])/h;
for(i=0;i<n;i++)
deltay[i][0]=y[i];
for(i=0;i<n;i++)
deltay[i][1]=deltay[i+1][0]-deltay[i][0];
k=n-2;
for(i=2;i<n;i++)
{
k=k-1;
for(j=0;j<=k;j++)
deltay[j][i]=deltay[j+1][i-1]-deltay[j][i-1];
}
pvalue=1;
factvalue=1;
sumy=y[0];
for(i=1;i<n;i++)
{
pvalue=pvalue*(p-i+1);
factvalue=factvalue*i;
term=(pvalue*deltay[0][i]);
sumy=sumy+term;
}
printf("the interpolated value of %f is %f",a,sumy);
getch( );
}

Output: enter number of pairs


4
enter values for x
0.1
0.2

96
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
0.3
0.4
enter values for y
1.005
1.020
1.045
1.081
enter the x value required
0.16
the interpolated value of 0.160000 is 1.011936

Program 34:-

Aim: A Program for linear regression.

#include<stdio.h>
#include<conio.h>
void main( )
{
int n,i,j;
float x[20],y[20],sx=0,sy=0,sxy=0,a0,a1,sx2=0,mx,my;
clrscr( );
printf("how many sets of data you want to enter");
scanf("%d",&n);
printf("enter x,y values in sets\n");
for(i=0;i<n;i++)
scanf("%f%f",&x[i],&y[i]);
for(i=0;i<n;i++)
{
sx=sx+x[i];
sy=sy+y[i];
sxy=sxy+(x[i]*y[i]);
sx2=sx2+(x[i]*x[i]);
97
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
}
a0=(sy*sx2-sx*sxy)/(n*sx2-sx*sx);
a1=(n*sxy-sx*sy)/(n*sx2-sx*sx);
printf("equation is y=%f+%fx",a0,a1);
printf("enter for which vlaue u want to know y");
scanf("%f",&mx);
my=a0+a1*mx;
printf("%f",my);
getch( );
}

Output: how many sets of data you want to enter5


enter x, y values in sets
13 34 46 67 78
equation is y=2.026316+0.850877x
enter for which value u want to know y4
5.429824

Program 35:-

Aim: A Program for polynomial regression.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main( )
{
int n,i,j,k,m;
float x[20],y[20],c[20][20],u=0,a[20];
clrscr( );
printf("enter n,m\n");
scanf("%d%d",&n,&m);
printf("enter the data\n");
98
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
for(i=1;i<=n;i++)
scanf("%f%f",&x[i],&y[i]);
for(j=1;j<=m+1;j++)
{
for(k=1;k<=m+1;k++)
{
c[j][k]=0;
for(i=1;i<=n;i++)
{
c[j][k]=c[j][k]+pow(x[i],(j+k-2));
}
}
}
for(j=1;j<=m+1;j++)
{
c[j][m+2]=0;
for(i=1;i<=n;i++)
{
c[j][m+2]+=(y[i]*pow(x[i],(j-1)));
}
}
for(k=1;k<=m+1;k++)
{
for(i=1;i<=m+1;i++)
{
if(i!=k)
{
u=c[i][k]/c[k][k];
for(j=k;j<=m+2;j++)
c[i][j]-=(u*c[k][j]);
}
}
}
for(i=1;i<=m+1;i++)
a[i]=c[i][m+2]/c[i][i];

99
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
for(i=1;i<=m+1;i++)
printf("a[%d]=%f\n",i-1,a[i]);
getch( );
}

Output: enter n, m
42
enter the data
24
3.5 3.8
5.2 5
66
a[0]=5.970579
a[1]=-1.486140
a[2]=0.248912

Program 36(a):-

Aim: A Program for trapezoidal method.

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) (1/(1+(x*x)))
void main( )
{
float a,b,sum=0,x,value,h;
int n,i;
clrscr( );
printf("enter a,b,n values\n");
scanf("%f%f%d",&a,&b,&n);
h=(b-a)/n;
x=a;
sum=f(a);
for(i=1;i<n;i++)
{
100
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
x=x+h;
sum+=(2+f(x));
}
sum+=f(b);
value=(sum*h)/2;
printf("the value is %f",value);
getch( );
}

Output: enter a,b,n values


0
1
10
the value is 1.329991

Program 36(b):-

Aim: A Program to implement Simpson method.


#include <stdio.h>
#include<conio.h>
#include <math.h>
#define f(x) (1/(1+x))
void main( )
{
int i,n;
float a,b,sum=0,x,value,h;
int n,i;
clrscr( );
printf("enter a,b,n value\n");
scanf("%f%f%d",&a,&b,&n);
h=(b-a)/n;
x=a;
sum=f(a);
for(i=1;i<n;i++)
101
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
{
x=x+h;
if(i%2==0)
sum+=(2*f(x));
else
sum+=(4*f(x));
}
sum+=f(b);
value=sum+(h/3);
printf("the value is %f",value);
getch( );
}
Output: enter a,b,n values
0
1
10
value=0.693150

102
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual

103
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual

ADDITIONAL EXPERIMENTS

Program 1:-

Aim: A Program to evaluate the expression (a*x+b)/(a*x-b)

#include<stdio.h>
#include<conio.h>
void main( )
{

int a,x,b;
float res;
printf(“enter values of a,x,b\n”);
104
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
scanf(“%d%d%d”,&a,&x,&b);
res=(float)(a*x+b)/(a*x-b);
printf(“result=%f”,res);
getch( );
}

Input: enter values of a,x,b


234
Output: 5

Program 2:-

Aim: A Program to check whether a number is Armstrong or not.

#include<stdio.h>
#include<conio.h>
void main( )
{

int r,n,sum=0,tn;
printf(“enter any number\n”);
scanf(%d”,&n);
105
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
while(n>0)
{
r=n%10;
sum =sum+r*r*r;
n/=10;
}
if(tn==sum)
printf(“%d is armstrong”,sum);
else
printf(“%d is not Armstrong”,sum)
getch( );
}

Input:enter any number


153
Output:153 is Armstrong

Program 3:-

Aim: A Program to print values of an array in ascending order.


#include<stdio.h>
#include<conio.h>
void main( )
{
int i,a[10],n,temp;
printf(“enter size of the array\n”);
scanf(“%d”,&n);
printf(“enter elements\n”);
for(i=0;i<n;i++)

106
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
scanf(“%d”,&a[i]);
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(“Ascending order is\n”);
for(i=0;i<n;i++)
printf(“%d “,a[i]);
getch( );
}
Input:enter size of the array
4
enter elements
4321
Output:Ascending order is
1234

Program 4:-

Aim: A program to implement Selection sort.

#include<stdio.h>
#include<conio.h>
void sel_sort(int x[ ], int n);
void main( )
{
int x[10],i,j,n;

107
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
printf(“Enter the size of an array \n”);
scanf(“%d”,&n);
printf(“Enter elements :\n”);
for(i=0;i<n;i++)
scanf(“%d”,&x[i]);
sel_sort(x,n);
printf(“Sorted order is \n”);
for(i=0;i<n;i++)
printf(“%d ”,x[i]);
getch( );
}
void sel_sort(int x[ ], int n)
{
int i,j,index,small;
for(i=0;i<n;i++)
{
small=x[i];
index=i;
for(j=i+1;j<n;j++)
{
if(x[j]<small)
{
small=x[j];
index=j;
}
}
x[index]=x[i];
x[i]=small;
}
}

108
MARCONI INSTITUTE OF TECHNOLOGY
Computer Programming Lab Manual
Input: Enter size of an array
5
Enter elements
2 7 3 9 1
Output: Sorted 0rder is
1 2 3 7 9

109
MARCONI INSTITUTE OF TECHNOLOGY

You might also like