C&DS LabManual
Transcript of C&DS LabManual
-
8/7/2019 C&DS LabManual
1/113
Computer Programming Lab Manual
Program 1(a):-
Aim: A Program to find sum of individual digits of a given number.
#include#includevoid 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 number234
Output:9
MARCONI INSTITUTE OF TECHNOLOGY
1
-
8/7/2019 C&DS LabManual
2/113
Computer Programming Lab Manual
Program 1(b):-
Aim: A Program to print Fibonacci series.
#include#includevoid 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
-
8/7/2019 C&DS LabManual
3/113
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
-
8/7/2019 C&DS LabManual
4/113
Computer Programming Lab Manual
Program 2(a):-
Aim: A Program to calculate the following sum:
1-x2/2! + x4/4! - ---- n terms.
#include#include#includevoid 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
-
8/7/2019 C&DS LabManual
5/113
Computer Programming Lab Manual
Input: Enter the value2
Enter the value of x1Output:Result=1.5
Program 2(b):-Aim: A Program to find roots of a quadratic equation.
#include#includevoid 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);}
MARCONI INSTITUTE OF TECHNOLOGY
5
-
8/7/2019 C&DS LabManual
6/113
Computer Programming Lab Manual
else{
printf(roots are imaginary);}
getch( );}Input: enter a,b,c
568
Output:roots are imaginary
Program 3(a):-Aim: A Program to find factorial of a given number using recursion.
#include#includeint 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);
MARCONI INSTITUTE OF TECHNOLOGY
6
-
8/7/2019 C&DS LabManual
7/113
Computer Programming Lab Manual
else{
fact=(x*factorial(x-1));return(fact);
}}
Input:enter any number4
Output:factorial of 4 is 24
Program 3(b):-
Aim: A Program to find factorial without using recursion.
#include#includevoid main( ){
int i,n,fact=1;clrscr( );printf(enter any number\n);scanf(%d,&n); 2for(i=1;i
-
8/7/2019 C&DS LabManual
8/113
Computer Programming Lab Manual
Input: enter any number4
Output:factorial of 4 is 24
Program 4(a):-
Aim: A Program to find gcd of two numbers.
#include#includevoid main( ){
int l,m,n;clrscr( );printf("enter any two values");scanf("%d%d",&m,&n);do{
l=n%m;n=m;m=l;
}
MARCONI INSTITUTE OF TECHNOLOGY
8
-
8/7/2019 C&DS LabManual
9/113
Computer Programming Lab Manual
while(l!=0);printf("gcd is %d",n);getch( );
}
Input: enter any two values23
Output: gcd is 1
Program 4(b):-
Aim: A Program to find gcd of two numbers using recursion.
#include#includeint 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( );
MARCONI INSTITUTE OF TECHNOLOGY
9
-
8/7/2019 C&DS LabManual
10/113
Computer Programming Lab Manual
}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
624 Outpuftgbbn b t: gcd is 6
Program 5(a):
Aim: A Program to implement towers of hanoi.
#include #include #include typedef unsigned int uint;#define NUM_ELEMENTOS 3int torres[NUM_ELEMENTOS][3];void mostraTela(){
int j, k, posx, posy;
int i;clrscr( );for(j = 0; j < 3; j++)
MARCONI INSTITUTE OF TECHNOLOGY
10
-
8/7/2019 C&DS LabManual
11/113
Computer Programming Lab Manual
{for(i = NUM_ELEMENTOS-1; i >= 0; i--)
{posy = 20 - i;
posx = 20 + (j * 20);for(k = 1; k = 0; i--)
{ if(torres[i][src]){
is = i;break;
}}
for(i = 0; i < NUM_ELEMENTOS; i++){
if(!torres[i][dst])
MARCONI INSTITUTE OF TECHNOLOGY
11
-
8/7/2019 C&DS LabManual
12/113
Computer Programming Lab Manual
{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;
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;
}}
MARCONI INSTITUTE OF TECHNOLOGY
12
-
8/7/2019 C&DS LabManual
13/113
Computer Programming Lab Manual
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;
}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;
MARCONI INSTITUTE OF TECHNOLOGY
13
-
8/7/2019 C&DS LabManual
14/113
Computer Programming Lab Manual
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;
elsedtemp = (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)){
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;
MARCONI INSTITUTE OF TECHNOLOGY
14
-
8/7/2019 C&DS LabManual
15/113
Computer Programming Lab Manual
elsetorres[i][j] = 0;
}}
mostraTela( );gotoxy(1,1);getch( );ranoi(NUM_ELEMENTOS, src, dst);gotoxy(1,1);getch( );return 0;
}
Output:-1- | |
--2-- | |
---3--- | |
| | |
--2-- | |
MARCONI INSTITUTE OF TECHNOLOGY
15
-
8/7/2019 C&DS LabManual
16/113
Computer Programming Lab Manual
---3--- -1- |
| | || | |
---3--- -1- --2
| | |
| | -1-
---3--- | --2
| | |
| | -1-
| ---3--- --2--
| | |
| | |
-1- ---3--- --2
| | || --2-- |
-1- ---3--- |
| -1- || --2-- |
| ---3--- |
Program 5(b):-
Aim: A Program to solveTowers of Hanoi using recursion
MARCONI INSTITUTE OF TECHNOLOGY
16
-
8/7/2019 C&DS LabManual
17/113
Computer Programming Lab Manual
#include#include#define N 3int A[N], B[N], C[N];
void Hanoi(int,int*,int*,int*);void PrintAll(void){
int i;
printf("A: ");for(i=0;i
-
8/7/2019 C&DS LabManual
18/113
Computer Programming Lab Manual
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;}int main( ){
int i;for(i=0;i
-
8/7/2019 C&DS LabManual
19/113
Computer Programming Lab Manual
Output: Solution of Tower of Hanoi Problem with 3 Disks
Starting state:A: 1 2 3B: 0 0 0C: 0 0 0------------------------------------------Subsequent states:A: 0 2 3B: 0 0 1
C: 0 0 0------------------------------------------A: 0 0 3B: 0 0 1C: 0 0 2------------------------------------------A: 0 0 3B: 0 0 0C: 0 1 2------------------------------------------A: 0 0 0B: 0 0 3C: 0 1 2------------------------------------------A: 0 0 1B: 0 0 3C: 0 0 2------------------------------------------A: 0 0 1B: 0 2 3C: 0 0 0
MARCONI INSTITUTE OF TECHNOLOGY
19
-
8/7/2019 C&DS LabManual
20/113
Computer Programming Lab Manual
------------------------------------------A: 0 0 0B: 1 2 3C: 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 atregular intervals of time given the values of u and a.The programshould provide the flexibility to the user to select his own timeintervals and repeat the calculations for different values of u and a.Program:
#include#includevoid main(){
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
-
8/7/2019 C&DS LabManual
21/113
Computer Programming Lab Manual
Input:Enter the value of u,a23
Output: 3850
Program 6(b):-
AIM: A Program to perform arithmetic operation using switch-case.PROGRAM:
#include#include
void main(){
int a,b,c;int ch;clrscr();printf("Enter the value of a\t\n");scanf("%d",&a);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);
MARCONI INSTITUTE OF TECHNOLOGY
21
-
8/7/2019 C&DS LabManual
22/113
Computer Programming Lab Manual
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;
}}getch();
}
Input: Enter the value of a4Enter the value of b5
***********
MARCONI INSTITUTE OF TECHNOLOGY
22
-
8/7/2019 C&DS LabManual
23/113
Computer Programming Lab Manual
MENU1.Addition2.Subtraction3.Multiplication
4.DivisionEnter u r choice3
Output:The Product is:20
Program 7(a):-
Aim: A Program to print the maximum and minimum elements in a listof
elements.#include#includevoid 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
-
8/7/2019 C&DS LabManual
24/113
Computer Programming Lab Manual
}printf(the maximum element is %d,minimum element is
%d,max,min);getch( );
} Input: enter size of the array3enter elements143
Output:the maximum element is 4, minimum element is 1
Program 7(b):-
Aim: A Program to perform addition and multiplication of twomatrices
using functions.
#include#include
void add( );void mul( );void main( ){
clrscr( );add( );mul( );getch( );
}
void add( ){
MARCONI INSTITUTE OF TECHNOLOGY
24
-
8/7/2019 C&DS LabManual
25/113
Computer Programming Lab Manual
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
-
8/7/2019 C&DS LabManual
26/113
Computer Programming Lab Manual
elseprintf(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
-
8/7/2019 C&DS LabManual
27/113
Computer Programming Lab Manual
{for(j=0;j
-
8/7/2019 C&DS LabManual
28/113
Computer Programming Lab Manual
5 6enter elements of second matrix
2 59 3
Output:the addition matrix is9 1112 8
the resultant matrix is24 2164 43
Program 8:-
Aim: A Program that uses functions to perform the followingoperations:
(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#include#includevoid insert( );void delet( );void main( )
MARCONI INSTITUTE OF TECHNOLOGY
28
-
8/7/2019 C&DS LabManual
29/113
Computer Programming Lab Manual
{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
-
8/7/2019 C&DS LabManual
30/113
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
-
8/7/2019 C&DS LabManual
31/113
Computer Programming Lab Manual
Program 9:-
Aim: A Program to check whether the given string is palindrome or
not.
#include
MARCONI INSTITUTE OF TECHNOLOGY
31
-
8/7/2019 C&DS LabManual
32/113
Computer Programming Lab Manual
#include#includevoid main( ){
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 stringmam
Output:given string is a palindrome
Program 10(a):-
MARCONI INSTITUTE OF TECHNOLOGY
32
-
8/7/2019 C&DS LabManual
33/113
Computer Programming Lab Manual
Aim: A Program to display the position in string S where the string Tbegins or -1 if S doesnt contain T.
#include#include
#includevoid main( ){
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);elseprintf("%d",-1);getch( );
}
Input:enter main stringabcdeenter the string to be foundcd
Output:the string is found at position 2
MARCONI INSTITUTE OF TECHNOLOGY
33
-
8/7/2019 C&DS LabManual
34/113
Computer Programming Lab Manual
Program 10(b):-Aim: A Program to count the lines, words and characters in the given
text.#include
#include void main( ){
char c,choice;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=97&&c
-
8/7/2019 C&DS LabManual
35/113
Computer Programming Lab Manual
no. of lines is 2
Program 11(a):-
Aim: A Program to print Pascal triangle#include#includevoid main( ){
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
-
8/7/2019 C&DS LabManual
36/113
Computer Programming Lab Manual
}
Input:Enter how many lines do you want3
Output: 11 2 1
1 3 3 1
MARCONI INSTITUTE OF TECHNOLOGY
36
-
8/7/2019 C&DS LabManual
37/113
Computer Programming Lab Manual
Program 11(b):-
Aim: A Program to print the sequence 12 3
4 5 67 8 9 10
#include
#includevoid main( ){
int i,j,n,k,l=1;clrscr( );printf(enter number of rows);scanf(%d,&n);for(i=1;i
-
8/7/2019 C&DS LabManual
38/113
Computer Programming Lab Manual
getch( );}
Input:enter number of rows4
Output: 12 3
4 5 67 8 9 10
Program 12:-
Aim: A Program to read two numbers x and n and then compute thesum of this geometrix progression.
1+x+x2+x3++xn.
#include#include#includevoid main( ){
int i,sum=0,x,n;printf("enter the values of x and n");scanf("%d%d",&x,&n);for(i=0;i
-
8/7/2019 C&DS LabManual
39/113
Computer Programming Lab Manual
}
Input: enter the values of x and n1 3
Output:result of the expression is 3
Program 13:-
Aim: A Program to find 2s complement of a binary number.
#include#includevoid 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=0;i--,j++){
if(a[i]!=1){
b[j]=a[i];}
else{
MARCONI INSTITUTE OF TECHNOLOGY
39
-
8/7/2019 C&DS LabManual
40/113
Computer Programming Lab Manual
b[j]=a[i];--i;++j;for(;i>=0;i--,j++)
{ if(a[i]==0)b[j]=1;elseb[j]=0;
}}
}
printf("the 2's complement is ");for(j=0;j
-
8/7/2019 C&DS LabManual
41/113
Computer Programming Lab Manual
Program 14:-
Aim: A Program to convert a Roman numeral to its decimal equivalent.
#include#include#include#includevoid main( ){
clrscr( );int *a,l,i,j,k;
MARCONI INSTITUTE OF TECHNOLOGY
41
-
8/7/2019 C&DS LabManual
42/113
Computer Programming Lab Manual
char *s;printf("Enter The Roman Number");scanf("%s",s);l=strlen(s);
for(i=0;i0;i--)
MARCONI INSTITUTE OF TECHNOLOGY
42
-
8/7/2019 C&DS LabManual
43/113
Computer Programming Lab Manual
{if(a[i]>a[i-1])
k=k-a[i-1];else if(a[i]==a[i-1] || a[i]
-
8/7/2019 C&DS LabManual
44/113
Computer Programming Lab Manual
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);
MARCONI INSTITUTE OF TECHNOLOGY
44
-
8/7/2019 C&DS LabManual
45/113
Computer Programming Lab Manual
c4.imag=(c1.imag*c2.real)+(c1.real*c2.imag);return(c4);
}
Input: enter first number 2 3enter second number 2 3
Output:addition is 4+i6multiplication is 5+i12
Program 16(a):-
Aim: A Program to copy contents of one file to another.
MARCONI INSTITUTE OF TECHNOLOGY
45
-
8/7/2019 C&DS LabManual
46/113
Computer Programming Lab Manual
#include#includevoid 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( );
} Output: the copied contents are
MARCONI INSTITUTE OF TECHNOLOGY
46
-
8/7/2019 C&DS LabManual
47/113
Computer Programming Lab Manual
Marconi Institute of technology.
Program 16(b):-
Aim: A Program to reverse the first n characters in a file.
#include#includevoid 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=0;i--){
putchar(str[i]);}
while((ch=getc(fp))!=EOF){
MARCONI INSTITUTE OF TECHNOLOGY
47
-
8/7/2019 C&DS LabManual
48/113
Computer Programming Lab Manual
putchar(ch);}getch( );
}
Output:enter n4
college of engineering and technology
Program 17:-
Aim: A Program that uses functions to perform the followingoperations on a singly linked list.
(a) Creation (b) Insertion (c) Deletion (d) traversal
#include#includevoid 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( ){
MARCONI INSTITUTE OF TECHNOLOGY
48
-
8/7/2019 C&DS LabManual
49/113
Computer Programming Lab Manual
int ch,ele;printf(1.create 2.insatend 3.insertion at position4.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 beinserted);
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;
MARCONI INSTITUTE OF TECHNOLOGY
49
-
8/7/2019 C&DS LabManual
50/113
Computer Programming Lab Manual
}}while(ch>=1&&chele=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;
MARCONI INSTITUTE OF TECHNOLOGY
50
-
8/7/2019 C&DS LabManual
51/113
Computer Programming Lab Manual
while(p->next!=NULL)p=p->next;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->next=NULL;
}elseprintf(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;
}
MARCONI INSTITUTE OF TECHNOLOGY
51
-
8/7/2019 C&DS LabManual
52/113
Computer Programming Lab Manual
elseprintf(insertion is not possible at this position);
}else
{ int count=0;struct node *p;p=start;while(p!=NULL){
p=p->next;count++;
}
if(posnext=start;start=temp;
}else{
p=start;for(i=1;inext;temp->next=p->next;p->next=temp;
}}elseprintf(insertion is not possible at this position);
}}void delatbeg()
MARCONI INSTITUTE OF TECHNOLOGY
52
-
8/7/2019 C&DS LabManual
53/113
Computer Programming Lab Manual
{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;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;
MARCONI INSTITUTE OF TECHNOLOGY
53
-
8/7/2019 C&DS LabManual
54/113
Computer Programming Lab Manual
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);
}elseprintf(deletion is not possible at this position);
}else{
p=start;while(p!=NULL){
p=p->next;count++;
}if(pos
-
8/7/2019 C&DS LabManual
55/113
Computer Programming Lab Manual
temp=start;start=start->next;free(temp);
}
else{for(i=1;inext;temp=p->next;p->next=p->next->next;free(temp);
}}
elseprintf(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;
}}
}
MARCONI INSTITUTE OF TECHNOLOGY
55
-
8/7/2019 C&DS LabManual
56/113
-
8/7/2019 C&DS LabManual
57/113
Computer Programming Lab Manual
struct node *prev;struct node *next;
};struct node *start=NULL;
void main(){int ch,ele;printf(1.create 2.insatend 3.insertion at position4.delatbeg 5.delatend 6.deletion at position 7.traversal8.traversalback);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 tobe
inserted);scanf(%d%d,&ele,&pos);insatpos(ele,pos);break;
case 4: delatbeg();break;
case 5: delatend();break;
MARCONI INSTITUTE OF TECHNOLOGY
57
-
8/7/2019 C&DS LabManual
58/113
Computer Programming Lab Manual
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&&chele=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));
MARCONI INSTITUTE OF TECHNOLOGY
58
-
8/7/2019 C&DS LabManual
59/113
Computer Programming Lab Manual
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;
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;
}elseprintf(insertion is not possible at this position);
}else if (start->next==NULL)
MARCONI INSTITUTE OF TECHNOLOGY
59
-
8/7/2019 C&DS LabManual
60/113
Computer Programming Lab Manual
{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;
}elseprintf(insertion is not possible at this position);
}else{
int count=0;struct node *p;p=start;while(p!=NULL){
p=p->next;count++;
}if(posnext=start;start->prev=temp;start=temp;
MARCONI INSTITUTE OF TECHNOLOGY
60
-
8/7/2019 C&DS LabManual
61/113
Computer Programming Lab Manual
start->prev=NULL;}else{
p=start;for(i=1;inext;temp->next=p->next;p->next->prev=temp;p->next=temp;temp->prev=p;
}}
elseprintf(insertion is not possible at this position);}
}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);
MARCONI INSTITUTE OF TECHNOLOGY
61
-
8/7/2019 C&DS LabManual
62/113
Computer Programming Lab Manual
}}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){
struct node *temp,*p;int count=0;if(start==NULL)printf(list is empty);else if (start->next==NULL){
if(pos==1){
MARCONI INSTITUTE OF TECHNOLOGY
62
-
8/7/2019 C&DS LabManual
63/113
Computer Programming Lab Manual
temp=start;start=NULL;free(temp);
}
elseprintf(deletion is not possible at this position);}else{
p=start;while(p!=NULL){
p=p->next;
count++;}if(posnext;start->prev=NULL;free(temp);
}else{
for(i=1;inext;temp=p->next;temp->next->prev=p;p->next=p->next->next;free(temp);
}}
MARCONI INSTITUTE OF TECHNOLOGY
63
-
8/7/2019 C&DS LabManual
64/113
Computer Programming Lab Manual
elseprintf(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;}
}}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;
}
MARCONI INSTITUTE OF TECHNOLOGY
64
-
8/7/2019 C&DS LabManual
65/113
Computer Programming Lab Manual
}}
Output:1.create2.insatend3.insertion at position4.delatbeg5.delatend6.deletion at position7.traversal
8.traversalbackenter your choice 1enter element to be inserted 11enter your choice 3enter element and position to be inserted 23 3enter your choice 1
MARCONI INSTITUTE OF TECHNOLOGY
65
-
8/7/2019 C&DS LabManual
66/113
Computer Programming Lab Manual
Program 19:-
Aim: A Program to implement stack using arrays.
#define max 20void 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);push(ele);
MARCONI INSTITUTE OF TECHNOLOGY
66
-
8/7/2019 C&DS LabManual
67/113
Computer Programming Lab Manual
break;case 2: ele=pop();
if(ele!=-1)printf(Deleted element is %d,ele);
break;case 3: display();break;
}}while(ch>=1&&ch=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);
MARCONI INSTITUTE OF TECHNOLOGY
67
-
8/7/2019 C&DS LabManual
68/113
Computer Programming Lab Manual
}}void display(){
if(top==-1)printf(Stack is empty);else{
for(i=0;i
-
8/7/2019 C&DS LabManual
69/113
Computer Programming Lab Manual
Program 20:-
Aim: A Program to implement stack using pointers.
#include#includevoid push( );void pop( );void display( );struct node{
int ele;struct node *next;
MARCONI INSTITUTE OF TECHNOLOGY
69
-
8/7/2019 C&DS LabManual
70/113
Computer Programming Lab Manual
}*start;void main( ){
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&&chele);temp->next=start;start=temp;
}void pop( )
MARCONI INSTITUTE OF TECHNOLOGY
70
-
8/7/2019 C&DS LabManual
71/113
Computer Programming Lab Manual
{struct node *temp;if(start==NULL){
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.Push2.Pop3.DisplayEnter ur choice1
MARCONI INSTITUTE OF TECHNOLOGY
71
-
8/7/2019 C&DS LabManual
72/113
Computer Programming Lab Manual
Enter element to be inserted11Enter ur choice2
Deleted element is 11Enter ur choice3List is empty
Program 21:-
Aim: A Program to implement queue using arrays.
#define max 20
MARCONI INSTITUTE OF TECHNOLOGY
72
-
8/7/2019 C&DS LabManual
73/113
Computer Programming Lab Manual
void enqueue(int);int dequeue();void display();int queue[max],f=0,r=-1;
void main(){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=max-1)
printf(queue is full);else
MARCONI INSTITUTE OF TECHNOLOGY
73
-
8/7/2019 C&DS LabManual
74/113
Computer Programming Lab Manual
{
queue[r]=ele;}
}int dequeue(){
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
-
8/7/2019 C&DS LabManual
75/113
Computer Programming Lab Manual
Output:1.Enqueue2.Dequeue3.DisplayEnter ur choice
1Enter element to be inserted11Enter ur choice2Deleted element is 11
Program 22:-
Aim: A Program to implement queue using pointers.
#include#includevoid 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);
MARCONI INSTITUTE OF TECHNOLOGY
75
-
8/7/2019 C&DS LabManual
76/113
Computer Programming Lab Manual
scanf(%d,&ch);switch(ch){
case 1: enqueue( );
break;case 2: dequeue( );break;
case 3: display( );break;
default: printf(Enter choice between 1 and 3);break;
}}
while(ch>=1&&chele);if(start==NULL){
start=temp;start->next=NULL;
}else{
p=start;while(p!=NULL){
p=p->next;}
MARCONI INSTITUTE OF TECHNOLOGY
76
-
8/7/2019 C&DS LabManual
77/113
Computer Programming Lab Manual
p->next=temp;temp->next=NULL;
}}
void dequeue(){struct node *temp;if(start==NULL){
printf(queue 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(queue is empty);}else{
while(p!=NULL){
printf(%d,p->ele);p=p->next;
}
MARCONI INSTITUTE OF TECHNOLOGY
77
-
8/7/2019 C&DS LabManual
78/113
Computer Programming Lab Manual
}}
Output: EnqueueDequeueDisplayEnter ur choice1Enter element to be inserted11Enter ur choice
2Deleted element is 11
MARCONI INSTITUTE OF TECHNOLOGY
78
-
8/7/2019 C&DS LabManual
79/113
Computer Programming Lab Manual
Program 23:-
Aim: A Program to convert the given infix expression to postfixexpression.
#include#include
#includevoid 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])
-
8/7/2019 C&DS LabManual
80/113
Computer Programming Lab Manual
printf(%c,pop());}push(inf[i]);
}
else if(inf[i]==()push(inf[i]);else if(inf[i]==)){
while((ch=pop())!=(){
printf(%c,ch);}
}
}while(top!=-1)printf(%c,pop());getch( );
}int opera(char ch){
if(ch==+||ch==-||ch==*||ch==/||ch==%)return(1);elsereturn(0);
}int prio(char ch){
switch(ch){
case +:case -: return(1);case *:case /:case %:return(2);
MARCONI INSTITUTE OF TECHNOLOGY
80
-
8/7/2019 C&DS LabManual
81/113
Computer Programming Lab Manual
default: return(0);}
}void push(char ch)
{ if(top>=max-1){
printf(stack is full);}else{
top++;stack[top]=ch;
}}char pop( ){
char ch;if(top==-1){
printf(stack is empty);exit(0);
}else{
ch=stack[top];top--;return(ch);
}}
Input: enter any infix expressiona+b-c
Output: ab+c-
MARCONI INSTITUTE OF TECHNOLOGY
81
-
8/7/2019 C&DS LabManual
82/113
Computer Programming Lab Manual
Program 24:-
Aim: A Program to evaluate the given postfix expression.
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]){
MARCONI INSTITUTE OF TECHNOLOGY
82
-
8/7/2019 C&DS LabManual
83/113
Computer Programming Lab Manual
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;}}
}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;
}
MARCONI INSTITUTE OF TECHNOLOGY
83
-
8/7/2019 C&DS LabManual
84/113
Computer Programming Lab Manual
}int pop(){
int ele;
if(top==-1){printf(stack is empty);return(-1);
}else{
ele=stack[top];top--;
return(ele);}}int opera(char ch){
if(ch==+||ch==-||ch==*||ch==/||ch==%)return(1);elsereturn(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.
MARCONI INSTITUTE OF TECHNOLOGY
84
-
8/7/2019 C&DS LabManual
85/113
Computer Programming Lab Manual
#include#include#include
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;clrscr( );printf("\n Enter the Number of Nodes \n");scanf("%d",&n);printf("\n Enter the Elements \n");for(i=1;i
-
8/7/2019 C&DS LabManual
86/113
Computer Programming Lab Manual
{
if(*r==NULL)
{ *r=(node *)malloc(sizeof(node));(*r)->lc=(*r)->rc=NULL ;(*r)->d=e;return;
}else{
if(ed)
insert(&((*r)->lc),e);elseinsert(&((*r)->rc),e);
}}
void inorder(node *r){
if(r!=NULL){
inorder(r->lc);printf("Inorder elements are: %d\n",r->d);inorder(r->rc);
}elsereturn;
}
void postorder(node *r){
if(r!=NULL)
MARCONI INSTITUTE OF TECHNOLOGY
86
-
8/7/2019 C&DS LabManual
87/113
Computer Programming Lab Manual
{postorder(r->lc);postorder(r->rc);printf("Postorder elements are: %d\n",r->d);
}elsereturn;
}void preorder(node *r){
if(r!=NULL){
printf("Preorder elements are: %d\n",r->d);
preorder(r->lc);preorder(r->rc);}elsereturn;
}
Input:Enter the Number of Nodes4Enter the Elements9101112
Output: Inorder elements are: 9Inorder elements are: 10Inorder elements are: 11
MARCONI INSTITUTE OF TECHNOLOGY
87
-
8/7/2019 C&DS LabManual
88/113
Computer Programming Lab Manual
Inorder elements are: 12Postorder elements are: 12Postorder elements are: 11Postorder elements are: 10
Postorder elements are: 9Preorder elements are: 9Preorder elements are: 10Preorder elements are: 11Preorder elements are: 12
Program 26:-
Aim: To write a program to implement linear search.
#include#includevoid main( ){
int a[10],i,n,search;printf(Enter no of elements \n);
MARCONI INSTITUTE OF TECHNOLOGY
88
-
8/7/2019 C&DS LabManual
89/113
Computer Programming Lab Manual
scanf(%d,&n);printf(Enter elements \n);for(i=0;i
-
8/7/2019 C&DS LabManual
90/113
Computer Programming Lab Manual
{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=0){
printf(Elements %d is found at %d position,
ele,pos+1);exit(0);}elseprintf(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
-
8/7/2019 C&DS LabManual
91/113
Computer Programming Lab Manual
}
Input:Enter size of the array4Enter elements56 90 34 76Enter element to be searched76
Output:76 is found at 4 position
Program 28:-
Aim: A program to implement Bubble sort.#include
MARCONI INSTITUTE OF TECHNOLOGY
91
-
8/7/2019 C&DS LabManual
92/113
Computer Programming Lab Manual
#includevoid 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
-
8/7/2019 C&DS LabManual
93/113
Computer Programming Lab Manual
Program 29:-
Aim: A program to implement Quick sort.
#include#includeint 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
-
8/7/2019 C&DS LabManual
94/113
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);if(l
-
8/7/2019 C&DS LabManual
95/113
Computer Programming Lab Manual
5Enter elements7 2 9 1 3
Output:After sorting
1 2 3 7 9
Program 30:-Aim: To write a program to implement Insertion sort.
#include#includevoid 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;ismall){
a[j]=a[j-i];j--;
}a[j]=small;
}printf(Sorted order is\n);
for(i=0;i
-
8/7/2019 C&DS LabManual
96/113
Computer Programming Lab Manual
getch();}Input:Enter size of an array
3
Enter elements2 7 3 Output:Sorted array is
2 3 7
Program 31:-
Aim: A program to implement merge sort.#include
#includevoid 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
-
8/7/2019 C&DS LabManual
97/113
Computer Programming Lab Manual
for(i=0;i
-
8/7/2019 C&DS LabManual
98/113
Computer Programming Lab Manual
k++;}*n=k;
}
Input: Enter size of first array3Enter elements of first array in sorted order1 2 3Enter size of second array4Enter elements of second array in sorted order4 5 6 7
Output: after sorting
1 2 3 4 5 6 7
Program 32:-
Aim: A Program to implement Lagrange Interpolation.
#include#includevoid 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
-
8/7/2019 C&DS LabManual
99/113
Computer Programming Lab Manual
scanf("%f",&t);sum=0;for(k=0;k
-
8/7/2019 C&DS LabManual
100/113
Computer Programming Lab Manual
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
-
8/7/2019 C&DS LabManual
101/113
Computer Programming Lab Manual
}printf("the interpolated value of %f is %f",a,sumy);getch( );
}
Output: enter number of pairs4enter values for x0.10.20.30.4enter values for y1.005
1.0201.0451.081enter the x value required0.16the interpolated value of 0.160000 is 1.011936
Program 34:-
Aim: A Program for linear regression.
#include#includevoid main( ){
int n,i,j;float x[20],y[20],sx=0,sy=0,sxy=0,a0,a1,sx2=0,mx,my;
MARCONI INSTITUTE OF TECHNOLOGY
101
-
8/7/2019 C&DS LabManual
102/113
Computer Programming Lab Manual
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
-
8/7/2019 C&DS LabManual
103/113
Computer Programming Lab Manual
Aim: A Program for polynomial regression.
#include
#include#includevoid 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");for(i=1;i
-
8/7/2019 C&DS LabManual
104/113
Computer Programming Lab Manual
}for(k=1;k
-
8/7/2019 C&DS LabManual
105/113
Computer Programming Lab Manual
#include#include#include
#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
-
8/7/2019 C&DS LabManual
106/113
Computer Programming Lab Manual
Program 36(b):-
Aim: A Program to implement Simpson method.#include
#include#include #definef(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
-
8/7/2019 C&DS LabManual
107/113
Computer Programming Lab Manual
10value=0.693150
MARCONI INSTITUTE OF TECHNOLOGY
107
-
8/7/2019 C&DS LabManual
108/113
Computer Programming Lab Manual
ADDITIONAL EXPERIMENTS
Program 1:-
Aim: A Program to evaluate the expression (a*x+b)/(a*x-b)
MARCONI INSTITUTE OF TECHNOLOGY
108
-
8/7/2019 C&DS LabManual
109/113
Computer Programming Lab Manual
#include#includevoid main( ){
int a,x,b;float res;printf(enter values of a,x,b\n);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 2 3 4
Output: 5
Program 2:-
MARCONI INSTITUTE OF TECHNOLOGY
109
-
8/7/2019 C&DS LabManual
110/113
Computer Programming Lab Manual
Aim: A Program to check whether a number is Armstrong or not.
#include#include
void main( ){
int r,n,sum=0,tn;printf(enter any number\n);scanf(%d,&n);while(n>0){
r=n%10;
sum =sum+r*r*r;n/=10;}if(tn==sum)printf(%d is armstrong,sum);elseprintf(%d is not Armstrong,sum)getch( );
}
Input:enter any number153
Output:153 is Armstrong
MARCONI INSTITUTE OF TECHNOLOGY
110
-
8/7/2019 C&DS LabManual
111/113
Computer Programming Lab Manual
Program 3:-
Aim: A Program to print values of an array in ascending order.#include
#includevoid 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
-
8/7/2019 C&DS LabManual
112/113
Computer Programming Lab Manual
Output:Ascending order is1 2 3 4
Program 4:-
Aim: A program to implement Selection sort.
#include#includevoid sel_sort(int x[ ], int n);void main( ){
int x[10],i,j,n;
printf(Enter the size of an array \n);scanf(%d,&n);printf(Enter elements :\n);for(i=0;i
-
8/7/2019 C&DS LabManual
113/113
Computer Programming Lab Manual
{small=x[j];index=j;
}
}x[index]=x[i];x[i]=small;
}}
Input:Enter size of an array5Enter elements2 7 3 9 1
Output:Sorted 0rder is1 2 3 7 9