Sei sulla pagina 1di 55

C-PROGRAMS

1. FIBONACCI
2. /* A Fibonacci Sequence is defined as follows: the first and
second terms in the sequence are 0 and 1. Subsequent
3. terms are found by adding the preceding two terms in the
sequence. Write a C program to generate
4. the first n terms of the sequence.
5. */
6.
7. #include
8.
9. void main()
10. {
11. int num1=0, num2=1,no,counter,fab;
12. clrscr();
13.
14. printf("<===========PROGRAM TO FIND THE FIBONACCI SERIES UP TO N
NO. IN SERIES=========>");
15. printf("\n\n\n\t\tENTER LENGTH OF SERIES (N) : ");
16. scanf("%d",&no);
17.
18. printf("\n\n\t\t\t<----FIBONACCI SERIES---->");
19. printf("\n\n\t\t%d %d",num1,num2);
20.
21. //LOOP WILL RUN FOR 2 TIME LESS IN SERIES AS THESE WAS PRINTED
IN ADVANCE
22. for(counter = 1; counter <= no-2; counter++)
23. {
24. fab=num1 + num2;
25. printf(" %d",fab);
26. num1=num2;
27. num2=fab;
28. }
29. getch();
}
30. PRIME NUMBER
31. /* Write a C program to generate all the prime numbers between 1
and n, where n is a value supplied by the user. */
32.
33. #include
34.
35. void main()
36. {
37. int no,counter,counter1,check;
38. clrscr();
39. printf("<-----------------------PRIME NO.
SERIES------------------------>");
40. printf("\n\n\n\t\t\tINPUT THE VALUE OF N: ");
41. scanf("%d",&no);
42. printf("\n\nTHE PRIME NO. SERIES B/W 1 TO %d : \n\n",no);
43.
44. for(counter = 1; counter <= no; counter++)
45. {

46.
check = 0;
47.
//THIS LOOP WILL CHECK A NO TO BE PRIME NO. OR NOT.
48.
49.
for(counter1 = counter-1; counter1 > 1 ; counter1--)
50.
if(counter%counter1 == 0)
51.
{
52.
check++;
// INCREMENT CHECK IF NO. IS NOT A PRIME NO.
53.
break;
54.
}
55.
if(check == 0)
56.
printf("%d\t",counter);
57. }
58. getch();
}
59. SUM OF DIGITS
60. /* Write a C program to find the sum of individual digits of a
positive integer.*/
61.
62. #include
63. #include
64.
65. void main()
66. {
67.
int num, k=1, sum=0;
68.
clrscr();
69.
printf("Enter the number whose digits are to be added:");
70.
scanf("%d",&num);
71. while(num!=0)
72. {
73.
k=num%10;
74.
sum=sum+k;
75.
k=num/10;
76.
num=k;
77. }
78. printf("Sum of the digits:%d",sum);
79. getch();
}
80. EQUATION
81. /* Write a C program to calculate the following Sum:
82.
Sum=1-x2/2! +x4/4!-x6/6!+x8/8!-x10/10!
83. */
84.
85. #include
86. #include
87.
88. void main()
89. {
90. int counter,f_coun;
91. float sum=0,x,power,fact;
92. clrscr();
93.
94. printf("<-----------------------PROGRAM FOR SUM OF EQ.
SERIES----------------------->");
95. printf("\n\n\tEQUATION SERIES : 1- X^2/2! + X^4/4! - X^6/6! +
X^8/8! - X^10/10!");
96.

97. printf("\n\n\n\tENTER VALUE OF X : ");


98. scanf("%f",&x);
99.
100. for(counter=0, power=0; power<=10; counter++,power=power+2)
101. {
102. fact=1;
103. //CALC FACTORIAL OF POWER VALUE
104. for(f_coun=power; f_coun>=1; f_coun--)
105.
fact *= f_coun;
106. //EQ. FOR SUM SERIES
107. sum=sum+(pow(-1,counter)*(pow(x,power)/fact));
108. }
109.
110. printf("SUM : %f",sum);
111. getch();
112.
}
113.ROOTS
114. /* Write a C program toe find the roots of a quadratic equation.
*/
115.
116. #include
117. #include
118. #include
119.
120. void main()
121. {
122. float a,b,c,root1,root2;
123. clrscr();
124. printf("\n Enter values of a,b,c for finding roots of a
quadratic eq:\n");
125. scanf("%f%f%f",&a,&b,&c);
126.
127. /*checking condition*/
128. if(b*b>4*a*c)
129. {
130. root1=-b+sqrt(b*b-4*a*c)/2*a;
131. root2=-b-sqrt(b*b-4*a*c)/2*a;
132. printf("\n*****ROOTS ARE*****\n");
133. printf("\n root1=%f\n root2=%f",root1,root2);
134. }
135. else
136. printf("\n Imaginary Roots.");
137. getch();
}
138.FACTORIAL
139. /* Write C programs that use both recursive and non-recursive
functions
140.
To find the factorial of a given integer.*/
141.
142. #include
143. #include
144.
145. unsigned int recr_factorial(int n);
146. unsigned int iter_factorial(int n);
147.

148. void main()


149. {
150.
int n,i;
151.
long fact;
152.
clrscr();
153.
printf("Enter the number: ");
154.
scanf("%d",&n);
155.
156.
if(n==0)
157.
printf("Factorial of 0 is 1\n");
158.
else
159.
{
160.
printf("Factorial of %d Using Recursive Function is
%d\n",n,recr_factorial(n));
161.
printf("Factorial of %d Using Non-Recursive Function is
%d\n",n,iter_factorial(n));
162.
}
163.
getch();
164. }
165.
166. /* Recursive Function*/
167. unsigned int recr_factorial(int n) {
168.
return n>=1 ? n * recr_factorial(n-1) : 1;
169. }
170.
171. /* Non-Recursive Function*/
172. unsigned int iter_factorial(int n) {
173.
int accu = 1;
174.
int i;
175.
for(i = 1; i <= n; i++) {
176.
accu *= i;
177.
}
178.
return accu;
}
179.GCD
180. /* Write C programs that use both recursive and non-recursive
functions
181.
To find the GCD (greatest common divisor) of two given
integers.*/
182.
183. #include
184. #include
185. #include
186.
187. unsigned int GcdRecursive(unsigned m, unsigned n);
188. unsigned int GcdNonRecursive(unsigned p,unsigned q);
189.
190. int main(void)
191. {
192.
int a,b,iGcd;
193.
clrscr();
194.
195.
printf("Enter the two numbers whose GCD is to be found: ");
196.
scanf("%d%d",&a,&b);
197.

198.
printf("GCD of %d and %d Using Recursive Function is
%d\n",a,b,GcdRecursive(a,b));
199.
printf("GCD of %d and %d Using Non-Recursive Function is
%d\n",a,b,GcdNonRecursive(a,b));
200.
201.
getch();
202. }
203.
204. /* Recursive Function*/
205. unsigned int GcdRecursive(unsigned m, unsigned n)
206. {
207. if(n>m)
208.
return GcdRecursive(n,m);
209. if(n==0)
210.
return m;
211. else
212.
return GcdRecursive(n,m%n);
213. }
214.
215. /* Non-Recursive Function*/
216. unsigned int GcdNonRecursive(unsigned p,unsigned q)
217. {
218. unsigned remainder;
219. remainder = p-(p/q*q);
220.
221. if(remainder==0)
222.
return q;
223. else
224.
GcdRecursive(q,remainder);
}
225.TOWER OF HANOI
226. /* Write C programs that use both recursive and non-recursive
functions
227.
To solve Towers of Hanoi problem.*/
228.
229. #include
230. #include
231.
232. /* Non-Recursive Function*/
233. void hanoiNonRecursion(int num,char sndl,char indl,char dndl)
234. {
235.
char
stkn[100],stksndl[100],stkindl[100],stkdndl[100],stkadd[100],temp;
236.
int top,add;
237.
top=NULL;
238.
239.
one:
240.
if(num==1)
241.
{
242.
printf("\nMove top disk from needle %c to needle %c
",sndl,dndl);
243.
goto four;
244.
}
245.
two:
246.
top=top+1;
247.
stkn[top]=num;

248.
stksndl[top]=sndl;
249.
stkindl[top]=indl;
250.
stkdndl[top]=dndl;
251.
stkadd[top]=3;
252.
num=num-1;
253.
sndl=sndl;
254.
temp=indl;
255.
indl=dndl;
256.
dndl=temp;
257.
258.
goto one;
259.
260.
three:
261.
printf("\nMove top disk from needle %c to needle %c
",sndl,dndl);
262.
top=top+1;
263.
stkn[top]=num;
264.
stksndl[top]=sndl;
265.
stkindl[top]=indl;
266.
stkdndl[top]=dndl;
267.
stkadd[top]=5;
268.
num=num-1;
269.
temp=sndl;
270.
sndl=indl;
271.
indl=temp;
272.
dndl=dndl;
273.
274.
goto one;
275.
276.
four:
277.
if(top==NULL)
278.
return;
279.
num=stkn[top];
280.
sndl=stksndl[top];
281.
indl=stkindl[top];
282.
dndl=stkdndl[top];
283.
add=stkadd[top];
284.
top=top-1;
285.
if(add==3)
286.
goto three;
287.
else if(add==5)
288.
goto four;
289. }
290.
291. /* Recursive Function*/
292. void hanoiRecursion( int num,char ndl1, char ndl2, char ndl3)
293. {
294.
if ( num == 1 ) {
295.
printf( "Move top disk from needle %c to needle %c.",
ndl1, ndl2 );
296.
return;
297.
}
298.
299.
hanoiRecursion( num - 1,ndl1, ndl3, ndl2 );
300.
printf( "Move top disk from needle %c to needle %c.", ndl1,
ndl2 );
301.
hanoiRecursion( num - 1,ndl3, ndl2, ndl1 );

302. }
303.
304. void main()
305. {
306.
int no;
307.
clrscr();
308.
printf("Enter the no. of disks to be transferred: ");
309.
scanf("%d",&no);
310.
311.
if(no<1)
312.
printf("\nThere's nothing to move.");
313.
else
314.
printf("Non-Recursive");
315.
hanoiNonRecursion(no,'A','B','C');
316.
printf("\nRecursive");
317.
hanoiRecursion(no,'A','B','C');
318.
319.
getch();
320. }
321.CHOICE
322. /* Write a C program, which takes two integer operands and one
operator form the user,
323.
performs the operation and then prints the result.
324.
(Consider the operators +,-,*, /, % and use Switch Statement)
325. */
326.
327. #include
328. #include
329.
330. void main()
331. {
332. int a,b,res,ch;
333. clrscr();
334. printf("\t
*********************");
335. printf("\n\tMENU\n");
336. printf("\t********************");
337. printf("\n\t(1)ADDITION");
338. printf("\n\t(2)SUBTRACTION");
339. printf("\n\t(3)MULTIPLICATION");
340. printf("\n\t(4)DIVISION");
341. printf("\n\t(5)REMAINDER");
342. printf("\n\t(0)EXIT");
343. printf("\n\t********************");
344. printf("\n\n\tEnter your choice:");
345. scanf("%d",&ch);
346.
347. if(ch<=5 & ch>0)
348. {
349. printf("Enter two numbers:\n");
350. scanf("%d%d",&a,&b);
351. }
352.
353. switch(ch)
354. {
355. case 1:
356. res=a+b;

357. printf("\n Addition:%d",res);


358. break;
359.
360. case 2:
361. res=a-b;
362. printf("\n Subtraction:%d",res);
363. break;
364.
365. case 3:
366. res=a*b;
367. printf("\n Multiplication:%d",res);
368. break;
369.
370. case 4:
371. res=a/b;
372. printf("\n Division:%d",res);
373. break;
374.
375. case 5:
376. res=a%b;
377. printf("\n Remainder:%d",res);
378. break;
379.
380. case 0:
381. printf("\n Choice Terminated");
382. exit();
383. break;
384.
385. default:
386. printf("\n Invalid Choice");
387. }
388. getch();
}
389.DIS TRAVELLED
390. /*
The total distance travelled by vehicle in 't' seconds is
given by distance = ut+1/2at2
391.
where 'u' and 'a' are the initial velocity (m/sec.) and
acceleration (m/sec2).
392.
Write C program to find the distance travelled at regular
intervals of time given
393.
the values of 'u' and 'a'. The program should provide the
flexibility to the user
394.
to select his own time intervals and repeat the
calculations for different values of 'u' and 'a'.
395. */
396.
397. #include
398. #include
399.
400. void main()
401. {
402. int tim_intrval, counter,time;
403. float accl, distance=0, velos;
404. clrscr();
405. printf("<===========PROGRAM FOR CALC TOTAL DISTANCE TRAVELED BY
A VECHIAL===========>");

406. printf("\n\n\n\t\t\tNO OF TIME INTERVALS : ");


407. scanf("%d",&tim_intrval);
408.
409. for(counter = 1; counter <= tim_intrval; counter++)
410. {
411.
printf("\n\t\t\tAT T%d TIME(sec) : ",counter);
412.
scanf("%d",&time);
413.
printf("\t\t\tVELOCITY AT %d sec (m/sec) : ",time);
414.
scanf("%f",&velos);
415.
printf("\t\t\tACCLERATION AT %d sec (m/sec^2): ",time);
416.
scanf("%f",&accl);
417.
distance += (velos*time + (accl*pow(time,2))/2);
418. }
419.
420. printf("\n\n\n\tTOTAL DISTANCE TRAVELLED BY VEHICLE IN %d
INTERVALS OF TIME : %f",tim_intrval,distance);
421. getch();
}
422.MATRICES
423. /* Write a C program that uses functions to perform the
following:
424.
i) Addition of Two Matrices
425.
ii) Multiplication of Two Matrices
426. */
427.
428. #include
429.
430. void main()
431. {
432. int ch,i,j,m,n,p,q,k,r1,c1,a[10][10],b[10][10],c[10][10];
433. clrscr();
434. printf("************************************");
435. printf("\n\t\tMENU");
436. printf("\n**********************************");
437. printf("\n[1]ADDITION OF TWO MATRICES");
438. printf("\n[2]MULTIPLICATION OF TWO MATRICES");
439. printf("\n[0]EXIT");
440. printf("\n**********************************");
441. printf("\n\tEnter your choice:\n");
442. scanf("%d",&ch);
443.
444. if(ch<=2 & ch>0)
445. {
446. printf("Valid Choice\n");
447. }
448.
449. switch(ch)
450. {
451.
case 1:
452.
printf("Input rows and columns of A & B Matrix:");
453.
scanf("%d%d",&r1,&c1);
454.
printf("Enter elements of matrix A:\n");
for(i=0;i

455.LARGEST&SMALLEST
456. /* Write a C program to find both the largest and smallest
number in a list of integers*/
457.
458.
459. main( )
460. {
461.
float largest(float a[ ], int n);
462.
float value[4] = {2.5,-4.75,1.2,3.67};
463.
printf("%f\n", largest(value,4));
464. }
465. float largest(float a[], int n)
466. {
467.
int i;
468.
float max;
469.
max = a[0];
470.
for(i = 1; i < n; i++)
471.
if(max < a[i])
472.
max = a[i];
473.
return(max);
}
474.INSERT SUBSTRING
475. /* Write a C program that uses functions to perform the
following operations:
476.
To insert a sub-string in to given main string from a given
position.
477. */
478.
479. #include
480. #include
481. #include
482.
483. void main()
484. {
485. char a[10];
486. char b[10];
487. char c[10];
488. int p=0,r=0,i=0;
489. int t=0;
490. int x,g,s,n,o;
491. clrscr();
492.
493. puts("Enter First String:");
494. gets(a);
495. puts("Enter Second String:");
496. gets(b);
497. printf("Enter the position where the item has to be inserted:
");
498. scanf("%d",&p);
499. r = strlen(a);
500. n = strlen(b);
501. i=0;
502.
503. // Copying the input string into another array
504. while(i <= r)
505. {

506. c[i]=a[i];
507. i++;
508. }
509. s = n+r;
510. o = p+n;
511.
512. // Adding the sub-string
for(i=p;i<="" }="">
513.PALINDROME
514. /* Write a C program to determine if the given string is a
palindrome or not */
515.
516. #include
517. #include
518.
519. enum Boolean{false,true};
520. enum Boolean IsPalindrome(char string[])
521. {
522. int left,right,len=strlen(string);
523. enum Boolean matched=true;
524. if(len==0)
525.
return 0;
526.
left=0;
527.
right=len-1;
528.
529.
/* Compare the first and last letter,second & second last & so
on */
while(left<right&&matched) %s="" 0;="" a="" char=""
clrscr();="" else="" getch();="" given=""
if(ispalindrome(string))="" if(string[left]!="string[right])"
if="" int="" is="" left++;="" main()="" matched;=""
matched="false;" not="" palindrome****\n");=""
palindrome\n",string);="" pre="" printf("****program=""
printf("enter="" printf("the="" return="" right--;=""
scanf("%s",string);="" string:");="" string="" string[40];=""
test="" the="" to="" {="" }<="" }=""></right&&matched)>
530.STRINGDELETE
531. /* Write a C program that uses functions to perform the
following operations:
532.
To delete n Characters from a given position in a given
string.
533. */
534.
535. #include
536. #include
537. #include
538.
539. void delchar(char *x,int a, int b);
540.
541. void main()
542. {
543.
char string[10];
544.
int n,pos,p;
545.
clrscr();
546.
547.
puts("Enter the string");

548.
gets(string);
549.
printf("Enter the position from where to delete");
550.
scanf("%d",&pos);
551.
printf("Enter the number of characters to be deleted");
552.
scanf("%d",&n);
553.
delchar(string, n,pos);
554.
getch();
555. }
556.
557. // Function to delete n characters
558. void delchar(char *x,int a, int b)
559. {
560.
if ((a+b-1) <= strlen(x))
561.
{
562.
strcpy(&x[b-1],&x[a+b-1]);
563.
puts(x);
564.
}
565. }
566.
567.COUNT
568. /* Write a C program to count the lines, words and characters in
a given text*/
569.
570. Program
571. #include
572. main()
573. {
574.
char line[81], ctr;
575.
int i,c,
576.
end = 0,
577.
characters = 0,
578.
words = 0,
579.
lines = 0;
580.
printf("KEY IN THE TEXT.\n");
581.
printf("GIVE ONE SPACE AFTER EACH WORD.\n");
582.
printf("WHEN COMPLETED, PRESS 'RETURN'.\n\n");
583.
while( end == 0)
584.
{
585.
/* Reading a line of text */
586.
c = 0;
587.
while((ctr=getchar()) != '\n')
588.
line[c++] = ctr;
589.
line[c] = '\0';
590.
/* counting the words in a line */
591.
if(line[0] == '\0')
592.
break ;
593.
else
594.
{
595.
words++;
596.
for(i=0; line[i] != '\0';i++)
597.
if(line[i] == ' ' ||
line[i] == '\t')
598.
words++;
599.
}
600.
/* counting lines and characters */
601.
lines = lines +1;

602.
characters = characters + strlen(line);
603.
}
604.
printf ("\n");
605.
printf("Number of lines = %d\n", lines);
606.
printf("Number of words = %d\n", words);
607.
printf("Number of characters = %d\n", characters);
608. }
609. Output
610. KEY IN THE TEXT.
611. GIVE ONE SPACE AFTER EACH WORD.
612. WHEN COMPLETED, PRESS 'RETURN'.
613. Admiration is a very short-lived passion.
614. Admiration involves a glorious obliquity of vision.
615. Always we like those who admire us but we do not
616. like those whom we admire.
617. Fools admire, but men of sense approve.
618. Number of lines = 5
619. Number of words = 36
Number of characters = 205
620.SEARCHSTRING
621. /* Write a C program that displays the position or index in the
string S
622.
where the string T begins, or - 1 if S doesn't contain T.
623. */
624.
625. #include
626. #include
627. #include
628.
629. void main()
630. {
631.
char s[30], t[20];
632.
char *found;
633.
clrscr();
634.
635. /* Entering the main string */
636.
puts("Enter the first string: ");
637.
gets(s);
638.
639. /* Entering the string whose position or index to be displayed
*/
640.
puts("Enter the string to be searched: ");
641.
gets(t);
642.
643. /*Searching string t in string s */
644.
found=strstr(s,t);
645.
if(found)
646.
printf("Second String is found in the First String at %d
position.\n",found-s);
647.
else
648.
printf("-1");
649.
getch();
650. }
651.PASCAL
652. /* Write a C program to generate Pascal's triangle. */
653.

654. #include
655. #include
656.
657. void main()
658. {
659. int bin,p,q,r,x;
660. clrscr();
661. bin=1;
662. q=0;
663.
664. printf("Rows you want to input:");
665. scanf("%d",&r);
666.
667. printf("\nPascal's Triangle:\n");
668.
669. while(q0;--p)
670.
printf(" ");
671.
for(x=0;x<=q;++x)
672.
{
673.
if((x==0)||(q==0))
674.
bin=1;
675.
else
676.
bin=(bin*(q-x+1))/x;
677.
printf("%6d",bin);
678.
}
679.
680. printf("\n");
681. ++q;
682. }
683. getch();
}
684.PYRAMID
685. /* Write a C program to construct a pyramid of numbers. */
686.
687. #include
688. #include
689.
690. void main()
691. {
692. int num,i,y,x=35;
693. clrscr();
694. printf("\nEnter the number to generate the pyramid:\n");
695. scanf("%d",&num);
696.
697. for(y=0;y<=num;y++)
698. {
699.
/*(x-coordinate,y-coordinate)*/
700.
gotoxy(x,y+1);
701.
702.
/*for displaying digits towards the left and right of zero*/
703.
for(i=0-y;i<=y;i++)
704.
705.
printf("%3d",abs(i));
706.
x=x-3;
707. }
708. getch();

}
709.SERIES
710. /*Write a C program to read in two numbers, x and n, and then
compute the sum of this geometric progression:
711. 1+x+x2+x3+.+xn For example: if n is 3 and x is 5, then the
program computes 1+5+25+125.
712. Print x, n, the sum Perform error checking. For example, the
formula does not make sense
713. for negative exponents - if n is less than 0. Have your program
print an error message if n<0,
714. then go back and read in the next pair of numbers of without
computing the sum.
715. Are any values of x also illegal ? If so, test for them too. */
716.
717. #include
718. #include
719. #include
720.
721. void main()
722. {
723. int s_sum,i,x,n;
724.
725. clrscr();
726. printf("Enter the values for x and n:");
727. scanf("%d %d",&x,&n);
728.
729. if(n<=0 || x<=0)
730. {
731.
printf("Value is not valid\n");
732. }
733. else
734. {
735.
printf("Value is valid\n");
736.
s_sum=1;
737.
for(i=1;i<=n;i++)
738.
{
739.
s_sum=s_sum+pow(x,i);
740.
}
741.
printf("Sum of series=%d\n",s_sum);
742. }
743. getch();
744. }
745.
746.COMPLEMENT
747. /* 2s complement of a number is obtained by scanning it from
right to left and complementing all
748. the bits after the first appearance of a 1. Thus 2s complement
of 11100 is 00100.
749. Write a C program to find the 2s complement of a binary
number.*/
750.
751. #include
752. #include
753.
754. void complement (char *a);
755. void main()

756. {
757. char a[16];
758. int i;
759. clrscr();
760. printf("Enter the binary number");
761. gets(a);
762. for(i=0;a[i]!='\0'; i++)
763. {
764.
if (a[i]!='0' && a[i]!='1')
765.
{
766.
printf("The number entered is not a binary number. Enter the
correct number");
767.
exit(0);
768.
}
769. }
770. complement(a);
771. getch();
772. }
773. void complement (char *a)
774. {
775. int l, i, c=0;
776. char b[16];
777. l=strlen(a);
778. for (i=l-1; i>=0; i--)
779. {
780.
if (a[i]=='0')
781.
b[i]='1';
782.
else
783.
b[i]='0';
784. }
785. for(i=l-1; i>=0; i--)
786. {
787. if(i==l-1)
788. {
789.
if (b[i]=='0')
790.
b[i]='1';
791.
else
792.
{
793.
b[i]='0';
794.
c=1;
795.
}
796. }
797. else
798. {
799.
if(c==1 && b[i]=='0')
800.
{
801.
b[i]='1';
802.
c=0;
803.
}
804. else if (c==1 && b[i]=='1')
805. {
806.
b[i]='0';
807.
c=1;
808. }
809. }
810. }
811. b[l]='\0';

812. printf("The 2's complement is %s", b);


813. }
814.
815.
816.ROM_DEC
817. /* Write a C program to convert a Roman numeral to its decimal
equivalent. */
818.
819. #include
820. #include
821. #include
822. #include
823.
824. void main()
825. {
826.
827. int *a,len,i,j,k;
828. char *rom;
829.
830. clrscr();
831.
832. printf("Enter the Roman Numeral:");
833. scanf("%s",rom);
834.
835. len=strlen(rom);
836.
837. for(i=0;i0;i--)
838. {
839.
if(a[i]>a[i-1])
840.
k=k-a[i-1];
else if(a[i]==a[i-1] || a[i]<="" decimal="" equivalent=""
getch();="" is:");="" k="k+a[i-1];" pre="" printf("%d",k);=""
printf("\nits="" }="">

841.COMPLEX
842. /* Write a C program that uses functions to perform the
following operations:
843.
i) Reading a complex number
844.
ii) Writing a complex number
845.
iii) Addition of two complex numbers
846.
iv) Multiplication of two complex numbers
847. (Note: represent complex number using a structure.) */
848.
849. #include
850. #include
851.
852. void arithmetic(int opern);
853.
854. struct comp
855. {
856.
double realpart;
857.
double imgpart;
858. };
859.

860. void main()


861. {
862.
int opern;
863.
clrscr();
864.
printf("\n\n \t\t\t***** MAIN MENU *****");
865.
printf("\n\n Select your option: \n 1 : ADD\n 2 :
MULTIPLY\n 0 : EXIT \n\n\t\t Enter your Option [ ]\b\b");
866.
867.
scanf("%d",&opern);
868.
869.
switch(opern)
870.
{
871.
case 0:
872.
exit(0);
873.
case 1:
874.
case 2:
875.
arithmetic(opern);
876.
default:
877.
main();
878.
}
879.
880. }
881.
882. void arithmetic(int opern)
883. {
884.
885.
struct comp w1, w2, w;
886.
887.
printf("\n Enter two Complex Numbers (x+iy):\n Real Part
of First Number:");
888.
scanf("%lf",&w1.realpart);
889.
printf("\n Imaginary Part of First Number:");
890.
scanf("%lf",&w1.imgpart);
891.
printf("\n Real Part of Second Number:");
892.
scanf("%lf",&w2.realpart);
893.
printf("\n Imaginary Part of Second Number:");
894.
scanf("%lf",&w2.imgpart);
895.
896.
897.
switch(opern)
898.
{
899.
900.
/*addition of complex number*/
901.
case 1:
902.
w.realpart = w1.realpart+w2.realpart;
903.
w.imgpart = w1.imgpart+w2.imgpart;
904.
break;
905.
906.
/*multiplication of complex number*/
907.
case 2:
908.
w.realpart=(w1.realpart*w2.realpart)(w1.imgpart*w2.imgpart);
909.
w.imgpart=(w1.realpart*w2.imgpart)+
(w1.imgpart*w2.realpart);
910.
break;
911.
}
912.

913.
914.
if (w.imgpart>0)
915.
printf("\n Answer = %lf+%lfi",w.realpart,w.imgpart);
916.
else
917.
printf("\n Answer = %lf%lfi",w.realpart,w.imgpart);
918.
getch();
919.
main();
920. }
921.
922.FILECOPY
923. /* Write a C program which copies one file to another.*/
924.
925. #include
926. #include
927. #include
928.
929. void main(int argc, char *argv[])
930. {
931. FILE *fs,*ft;
932. char ch;
933. clrscr();
934. if(argc!=3)
935. {
936.
puts("Invalid number of arguments.");
937.
exit(0);
938.
}
939. fs = fopen(argv[1],"r");
940. if(fs==NULL)
941. {
942. puts("Source file cannot be opened.");
943. exit(0);
944. }
945. ft = fopen(argv[2],"w");
946. if (ft==NULL)
947. {
948. puts("Target file cannot be opened.");
949. fclose(fs);
950. exit(0);
951. }
952. while(1)
953. {
954.
ch=fgetc(fs);
955.
if (ch==EOF)
956.
break;
957.
else
958.
fputc(ch,ft);
959.
}
960.
fclose(fs);
961.
fclose(ft);
962.
getch();
963. }
964.
965.
966.
967.FILEREV

968. /* Write a C program to reverse the first n characters in a


file.
969. (Note: The file name and n are specified on the command line.)*/
970.
971. #include
972. #include
973. #include
974. #include
975.
976. void main(int argc, char *argv[])
977. {
978.
char a[15];
979.
char s[20];
980.
char n;
981.
int k;
982.
int j=0;
983.
int i;
984.
int len;
985.
FILE *fp;
986.
987.
if(argc!=3)
988.
{
989.
puts("Improper number of arguments.");
990.
exit(0);
991.
}
992.
fp = fopen(argv[1],"r");
993.
if(fp == NULL)
994.
{
995.
puts("File cannot be opened.");
996.
exit(0);
997.
}
998.
999.
k=*argv[2]-48;
1000.
n = fread(a,1,k,fp);
1001.
a[n]='\0';
1002.
len=strlen(a);
1003.
for(i=len-1;i>=0;i--)
1004.
{
1005.
s[j]=a[i];
1006.
printf("%c",s[j]);
1007.
j=j+1;
1008.
}
1009.
s[j+1]='\0';
1010.
getch();
1011.
}
1012.
1013.
1014. CREATIONLINKED
1015.
/* Write a program to create a linear linked list
interactively and print out the list and the total number of
items in the list. */
1016.
1017.
1018.
#include
1019.
#include
1020.
#define
NULL 0

1021.
1022.
struct linked_list
1023.
{
1024.
int number;
1025.
struct linked_list *next;
1026.
};
1027.
typedef struct linked_list node; /* node type defined
*/
1028.
1029.
main()
1030.
{
1031.
node *head;
1032.
void create(node *p);
1033.
int count(node *p);
1034.
void print(node *p);
1035.
head = (node *)malloc(sizeof(node));
1036.
create(head);
1037.
printf("\n");
1038.
printf(head);
1039.
printf("\n");
1040.
printf("\nNumber of items = %d \n", count(head));
1041.
}
1042.
void create(node *list)
1043.
{
1044.
printf("Input a number\n");
1045.
printf("(type -999 at end): ");
1046.
scanf("%d", &list -> number); /* create current
node */
1047.
1048.
if(list->number == -999)
1049.
{
1050.
list->next = NULL;
1051.
}
1052.
1053.
else
/*create next node */
1054.
{
1055.
list->next = (node
*)malloc(sizeof(node));
1056.
create(list->next); */ Recursion occurs
*/
1057.
}
1058.
return;
1059.
}
1060.
void print(node *list)
1061.
{
1062.
if(list->next != NULL)
1063.
{
1064.
printf("%d-->",list ->number); /*
print current item */
1065.
1066.
if(list->next->next == NULL)
1067.
printf("%d", list->next>number);
1068.
1069.
print(list->next);
/* move to
next item */
1070.
}

1071.
return;
1072.
}
1073.
1074.
int count(node *list)
1075.
{
1076.
if(list->next == NULL)
1077.
return (0);
1078.
else
1079.
return(1+ count(list->next));
1080.
}
1081.
1082.
Output
1083.
1084.
Input a number
1085.
(type -999 to end); 60
1086.
Input a number
1087.
(type -999 to end); 20
1088.
Input a number
1089.
(type -999 to end); 10
1090.
Input a number
1091.
(type -999 to end); 40
1092.
Input a number
1093.
(type -999 to end); 30
1094.
Input a number
1095.
(type -999 to end); 50
1096.
Input a number
1097.
(type -999 to end); -999
1098.
1099.
60 -->20 -->10 -->40 -->30 -->50 --> -999
1100.
Number of items = 6
1101. DELETEFUNCTION
1102.
/* Write a function for deleting an item from linked
list */
1103.
1104.
1105.
node *delete(node *head)
1106.
{
1107.
node *find(node *p, int a);
1108.
int key;
/* item to be deleted */
1109.
node *n1;
/* pointer to node
preceding key node */
1110.
node *p;
/* temporary pointer */
1111.
printf("\n What is the item (number) to be
deleted?");
1112.
scanf("%d", &key);
1113.
if(head->number == key)
/* first node to
be deleted) */
1114.
{
1115.
p = head->next;
/* pointer to 2nd
node in list */
1116.
free(head);
/* release space
of key node */
1117.
head = p;
/* make head to
point to 1st node */
1118.
}

1119.
else
1120.
{
1121.
n1 = find(head, key);
1122.
if(n1 == NULL)
1123.
printf("\n key not found \n");
1124.
else
/*
delete key node */
1125.
{
1126.
p = n1->next->next;
/*
pointer to the node
1127.
following the keynode */
1128.
1129.
free(n1->next);
/*
free key node */
1130.
n1->next = p;
/*
establish link */
1131.
}
1132.
}
1133.
return(head);
1134.
}
/* USE FUNCTION find() HERE */
1135. INSERTFUNCTION
1136.
/* Write a function for inserting an item into a linked
list */
1137.
1138.
1139.
node *insert(node *head)
1140.
{
1141.
node *find(node *p, int a);
1142.
node *new;
/* pointer to new node */
1143.
node *n1;
/* pointer to node
preceding key node */
1144.
int key;
1145.
int x;
/* new item (number) to be
inserted */
1146.
1147.
printf("Value of new item?");
1148.
scanf("%d", &x);
1149.
printf("Value of key item ? (type -999 if last)
");
1150.
scanf("%d", &key);
1151.
1152.
if(head->number == key)
/* new
node is first */
1153.
{
1154.
new = (node *)malloc(size of(node));
1155.
new->number = x;
1156.
new->next = head;
1157.
head = new;
1158.
}
1159.
else
/* find key node and insert new
node */
1160.
{
/* before the key node */
1161.
n1 = find(head, key);
/* find key node
*/

1162.
1163.
if(n1 == NULL)
1164.
printf("\n key is not found \n");
1165.
else
/* insert new node */
1166.
{
1167.
new = (node *)malloc(sizeof(node));
1168.
new->number = x;
1169.
new->next = n1->next;
1170.
n1->next = new;
1171.
}
1172.
}
1173.
return(head);
1174.
}
1175.
node *find(node *lists, int key)
1176.
{
1177.
if(list->next->number == key)
/*
key found */
1178.
return(list);
1179.
else
1180.
1181.
if(list->next->next == NULL)
/* end */
1182.
return(NULL);
1183.
else
1184.
find(list->next, key);
}
1185. TRAVERSAL
1186.
/* Write a program for creation of sorted list from a
given list of numbers */
1187.
1188.
1189.
1190.
#include
1191.
#include
1192.
#define NULL 0
1193.
1194.
struct linked_list
1195.
{
1196.
int number;
1197.
struct linked_list *next;
1198.
};
1199.
typedef struct linked_list node;
1200.
1201.
main ()
1202.
{
1203.
int n;
1204.
node *head = NULL;
1205.
void print(node *p);
1206.
node *insert_Sort(node *p, int n);
1207.
1208.
printf("Input the list of numbers.\n");
1209.
printf("At end, type -999.\n");
1210.
scanf("%d",&n);
1211.
1212.
while(n != -999)
1213.
{

1214.
if(head == NULL)
/* create 'base'
node */
1215.
{
1216.
head = (node
*)malloc(sizeof(node));
1217.
head ->number = n;
1218.
head->next = NULL;
1219.
1220.
}
1221.
1222.
else
/* insert
next item */
1223.
{
1224.
head = insert_sort(head,n);
1225.
}
1226.
scanf("%d", &n);
1227.
}
1228.
printf("\n");
1229.
print(head);
1230.
print("\n");
1231.
}
1232.
node *insert_sort(node *list, int x)
1233.
{
1234.
node *p1, *p2, *p;
1235.
p1 = NULL;
1236.
p2 = list; /* p2 points to first node */
1237.
1238.
for( ; p2->number < x ; p2 = p2->next)
1239.
{
1240.
p1 = p2;
1241.
1242.
if(p2->next == NULL)
1243.
{
1244.
p2 = p2->next;
/* p2 set to NULL */
1245.
break;
/*
insert new node at end */
1246.
}
1247.
}
1248.
1249.
/* key node found */
1250.
p = (node *)malloc(sizeof(node)); /*
space for new node */
1251.
p->number = x;
/* place value in the
new node */
1252.
p->next = p2;
/* link new node to
key node */
1253.
if (p1 == NULL)
1254.
list = p;
/* new node becomes the first node */
1255.
else
1256.
p1->next =
p; /* new node inserted after 1st node */
1257.
return (list);
1258.
}
1259.
void print(node *list)
1260.
{

1261.
if (list == NULL)
1262.
printf("NULL");
1263.
else
1264.
{
1265.
printf("%d-->",list->number);
1266.
print(list->next);
1267.
}
1268.
return;
1269.
}
1270.
1271.
Output
1272.
Input the list of number.
1273.
At end, type -999.
1274.
80 70 50 40 60 -999
1275.
40-->50-->60-->70-->80 -->NULL
1276.
Input the list of number.
1277.
At end, type -999.
1278.
40 70 50 60 80 -999
40-->50-->60-->70-->80-->NULL
1279. DOUBLELINKEDLIST
1280.
/* Write a C program that uses functions to perform the
following operations on doubly linked list.:
1281.
i) Creation ii) Insertion
iii) Deletion
iv)
Traversal in both ways
1282.
*/
1283.
1284.
#include "stdio.h"
1285.
#include "alloc.h"
1286.
1287.
typedef struct dubll
1288.
{
1289.
int data;
1290.
struct dubll *leftlink,*rightlink;
1291.
}*DUBLL;
1292.
1293.
DUBLL high,temp_node,low,last,pntr;
1294.
int flag=0;
1295.
1296.
DUBLL NodeAlloc();
1297.
DUBLL Search(int,int);
1298.
1299.
void CreateItem();
1300.
void AppendItem();
1301.
void PrintItem();
1302.
void DeleteItem();
1303.
DUBLL Search(int item,int flag);
1304.
DUBLL NodeAlloc();
1305.
void InsertItem();
1306.
1307.
void main(void)
1308.
{
1309.
int choice,Item;
1310.
high=NULL;
1311.
while(1)
1312.
{
1313.
clrscr();

1314.
printf("\n \t\t\t***** M A I N
M E N U *****\n\n");
1315.
printf("\n 1: Create Linked List \n 2: Append a Node
to the List \n 3: Traverse the List \n 4: Delete a Node from the
List \n 5: Search a Node \n 6: Insert a Node to the List \n 7:
Close \n\n\t\t Enter your Option [ ]\b\b");
1316.
scanf("%d",&choice);
1317.
switch(choice)
1318.
{
1319.
case 1:
1320.
CreateItem();
1321.
puts("\nPress any key to go back to main
menu.");
1322.
getch();
1323.
break;
1324.
case 2:
1325.
AppendItem();
1326.
break;
1327.
case 3:
1328.
PrintItem();
1329.
puts("\nPress any key to go back to main
menu.");
1330.
getch();
1331.
break;
1332.
case 4:
1333.
DeleteItem();
1334.
break;
1335.
case 5:
1336.
printf("Find an Item: ");
1337.
scanf("%d",&Item);
1338.
temp_node=Search(Item,0);
1339.
if(temp_node)
1340.
{
1341.
puts("The item is available in the Linked
List.");
1342.
}
1343.
else
1344.
{
1345.
puts("The item is not found in the Linked
List.");
1346.
}
1347.
getch();
1348.
break;
1349.
case 6:
1350.
InsertItem();
1351.
break;
1352.
case 7:
1353.
exit();
1354.
default:
1355.
puts("Invalid choice.");
1356.
puts("\nPress any key to go back to main
menu.");
1357.
getch();
1358.
break;
1359.
}
1360.
}
1361.
}
1362.

1363.
/* Function to Create the list*/
1364.
void CreateItem()
1365.
{
1366.
if(high==NULL)
1367.
{
1368.
printf("\n --Creating the list--");
1369.
temp_node=NodeAlloc();
1370.
printf("\n Enter starting data (as integer
value) :");
1371.
scanf("%d",&temp_node->data);
1372.
high=temp_node;
1373.
}
1374.
else{ printf("\n List already created @ %d with %d as
data.",high,high->data);}
1375.
}
1376.
1377.
/* Function to Append items to the list*/
1378.
void AppendItem()
1379.
{
1380.
low=high;
1381.
if(high==NULL)
1382.
{
1383.
CreateItem();
1384.
}
1385.
else
1386.
{
1387.
temp_node=NodeAlloc();
1388.
printf("\n Enter Item (in integer) :");
1389.
scanf("%d",&temp_node->data);
1390.
temp_node->rightlink=NULL;
1391.
1392.
while(low->rightlink!=NULL)
1393.
low=low->rightlink;
1394.
low->rightlink=temp_node;
1395.
temp_node->leftlink=low;
1396.
last=low->rightlink;
1397.
1398.
}
1399.
}
1400.
1401.
/* Function to Traverse the list both ways and print the
data*/
1402.
void PrintItem()
1403.
{
1404.
DUBLL temp_node;
1405.
if(high==NULL)
1406.
{
1407.
printf("\n List is not available. Please create a
list first.");
1408.
getch();
1409.
CreateItem();
1410.
}
1411.
temp_node=high;
1412.
last=low->rightlink;
1413.
printf("\n--Printing The List In Forward
direction--\n");
1414.

1415.
while(temp_node!=NULL)
//In forward
direction
1416.
{
1417.
printf("\t %d",temp_node->data);
1418.
temp_node = temp_node->rightlink;
1419.
}
1420.
printf("\n");
1421.
printf("\n--Printing The List In Backward
direction--\n");
1422.
temp_node=high;
1423.
if(temp_node->rightlink==NULL)
{printf("%d",temp_node->data);return; }
1424.
while(last!=NULL)
//In backward
direction
1425.
{
1426.
printf("\t %d",last->data);
1427.
last = last->leftlink;
1428.
}
1429.
}
1430.
1431.
/* Function to Delete items of the list*/
1432.
void DeleteItem()
1433.
{
1434.
int value;
1435.
DUBLL temp_node;
1436.
if(high==NULL)
1437.
{
1438.
printf("\n List is not available. Please create a
list first.");
1439.
getch();
1440.
CreateItem();
1441.
}
1442.
printf("\n Item to delete :");
1443.
scanf("%d",&value);
1444.
pntr=Search(value,1);
1445.
pntr->leftlink->rightlink=pntr->rightlink;
1446.
pntr->rightlink->leftlink=pntr->leftlink;
1447.
temp_node=pntr;
1448.
free(temp_node);
1449.
}
1450.
1451.
/* Function to Search an item from the list*/
1452.
DUBLL Search(int item,int flag)
1453.
{
1454.
temp_node = high;
1455.
if(high==NULL)
1456.
{
1457.
printf("\n List is not available. Please create a
list first.");
1458.
getch();
1459.
CreateItem();
1460.
}
1461.
while(temp_node!=NULL)
1462.
{
1463.
if(temp_node->data==item )
1464.
{
1465.
if(flag==0)

1466.
{
1467.
return(1);
1468.
}
1469.
else
1470.
{
1471.
return(temp_node);
1472.
}
1473.
}
1474.
temp_node=temp_node->rightlink;
1475.
}
1476.
}
1477.
1478.
/* Function to Allocate nodes*/
1479.
DUBLL NodeAlloc()
1480.
{
1481.
DUBLL tmep_node;
1482.
tmep_node=malloc(sizeof(struct dubll));
1483.
if(tmep_node==NULL)
1484.
{
1485.
printf("\n No memory available. Node allocation
cannot be done.");
1486.
}
1487.
tmep_node->rightlink=tmep_node->leftlink=NULL;
1488.
return(tmep_node);
1489.
}
1490.
1491.
/* Function to Insert items in the middle of the list*/
1492.
void InsertItem()
1493.
{
1494.
int node;
1495.
DUBLL temp_node;
1496.
1497.
if(high==NULL)
1498.
{
1499.
printf("\n List is not available. Please create a
list first.");
1500.
getch();
1501.
CreateItem();
1502.
}
1503.
temp_node=NodeAlloc();
1504.
printf("Position At which node to be inserted: ___ &
New Item Value: ___ ");
1505.
scanf("%d",&node);
1506.
scanf("%d",&temp_node->data);
1507.
pntr=Search(node,1);
1508.
1509.
if(pntr->rightlink==NULL){printf("\n The operation is
not possible."); getch();return;}
1510.
temp_node->leftlink=pntr;
//creating link to new node
1511.
temp_node->rightlink=pntr->rightlink;
1512.
1513.
pntr->rightlink->leftlink=temp_node;
1514.
pntr->rightlink=temp_node;
1515.
1516.
printf("\n Item has been Inserted.");
1517.
getch();

}
1518. ST_ARR
1519.
/* Write C programs that implement stack (its
operations) using i) Arrays */
1520.
1521.
#include
1522.
#include
1523.
1524.
int st_arr[20];
1525.
int t=-1;
1526.
1527.
void push_ele(int ele);
1528.
int pop_ele();
1529.
void display_ele();
1530.
1531.
void main()
1532.
{
1533.
char choice,num1=0,num2=0;
1534.
while(1)
1535.
{
1536.
clrscr();
1537.
printf("======================================");
1538.
printf("\n\t\t MENU ");
1539.
printf("\n======================================");
1540.
printf("\n[1] Using Push Function");
1541.
printf("\n[2] Using Pop Function");
1542.
printf("\n[3] Elements present in Stack");
1543.
printf("\n[4] Exit\n");
1544.
printf("\n\tEnter your choice: ");
1545.
fflush(stdin);
1546.
scanf("%c",&choice);
1547.
1548.
switch(choice-'0')
1549.
{
1550.
1551.
case 1:
1552.
{
1553.
printf("\n\tElement to be pushed: ");
1554.
scanf("%d",&num1);
1555.
push_ele(num1);
1556.
break;
1557.
}
1558.
1559.
case 2:
1560.
{
1561.
num2=pop_ele(1);
1562.
printf("\n\tElement to be popped: %d\n\t",num2);
1563.
getch();
1564.
break;
1565.
}
1566.
1567.
case 3:
1568.
{
1569.
display_ele();
1570.
getch();
1571.
break;

1572.
}
1573.
1574.
case 4:
1575.
exit(1);
1576.
break;
1577.
1578.
default:
1579.
printf("\nYour choice is invalid.\n");
1580.
break;
1581.
}
1582.
}
1583.
}
1584.
1585.
/*Implementing the push() function. */
1586.
void push_ele(int ele)
1587.
{
1588.
if(t==99)
1589.
{
1590.
printf("STACK is Full.\n");
1591.
getch();
1592.
exit(1);
1593.
}
1594.
st_arr[++t]=ele;
1595.
}
1596.
1597.
/*Implementing the pop() function. */
1598.
int pop_ele()
1599.
{
1600.
int ele1;
1601.
if(t==-1)
1602.
{
1603.
printf("\n\tSTACK is Empty.\n");
1604.
getch();
1605.
exit(1);
1606.
}
1607.
return(st_arr[t--]);
1608.
}
1609.
1610.
/*Implementing display() function. */
1611.
void display_ele()
1612.
{
1613.
int k;
1614.
printf("\n\tElements present in the stack are:\n\t");
1615.
for(k=0;k<=t;k++)
1616.
printf("%d\t",st_arr[k]);
}
1617. ST_POINT
1618.
/* Write C programs that implement stack (its
operations) using ii) Pointers */
1619.
1620.
#include
1621.
#include
1622.
1623.
struct st_point
1624.
{
1625.
int ele;

1626.
struct st_point *l;
1627.
}
1628.
1629.
*t;
1630.
int i;
1631.
1632.
void push_ele(int j);
1633.
int pop_ele();
1634.
void display_ele();
1635.
1636.
void main()
1637.
{
1638.
char choice,num1=0,num2=0;
1639.
int i;
1640.
while(1)
1641.
{
1642.
clrscr();
1643.
printf("======================================");
1644.
printf("\n\t\t MENU ");
1645.
printf("\n======================================");
1646.
printf("\n[1] Using Push Function");
1647.
printf("\n[2] Using Pop Function");
1648.
printf("\n[3] Elements present in Stack");
1649.
printf("\n[4] Exit\n");
1650.
printf("\n\tEnter your choice: ");
1651.
fflush(stdin);
1652.
scanf("%c",&choice);
1653.
1654.
switch(choice-'0')
1655.
{
1656.
case 1:
1657.
{
1658.
printf("\n\tElement to be pushed:");
1659.
scanf("%d",&num1);
1660.
push_ele(num1);
1661.
break;
1662.
}
1663.
1664.
case 2:
1665.
{
1666.
num2=pop_ele(1);
1667.
printf("\n\tElement to be popped: %d\n\t",num2);
1668.
getch();
1669.
break;
1670.
}
1671.
1672.
case 3:
1673.
{
1674.
printf("\n\tElements present in the stack
are:\n\t");
1675.
display_ele();
1676.
getch();
1677.
break;
1678.
}
1679.
1680.
case 4:
1681.
exit(1);

1682.
break;
1683.
1684.
default:
1685.
printf("\nYour choice is invalid.\n");
1686.
break;
1687.
}
1688.
}
1689.
}
1690.
1691.
/*Inserting the elements using push function*/
1692.
void push_ele(int j)
1693.
{
1694.
struct st_point *m;
1695.
m=(struct st_point*)malloc(sizeof(struct st_point));
1696.
m->ele=j;
1697.
m->l=t;
1698.
t=m;
1699.
return;
1700.
}
1701.
1702.
/*Removing the elements using pop function*/
1703.
int pop_ele()
1704.
{
1705.
if(t==NULL)
1706.
{
1707.
printf("\n\STACK is Empty.");
1708.
getch();
1709.
exit(1);
1710.
}
1711.
else
1712.
{
1713.
int i=t->ele;
1714.
t=t->l;
1715.
return (i);
1716.
}
1717.
return 0;
1718.
}
1719.
1720.
/*Displaying the elements */
1721.
void display_ele()
1722.
{
1723.
struct st_point *pointer=NULL;
1724.
pointer=t;
1725.
while(pointer!=NULL)
1726.
{
1727.
printf("%d\t",pointer->ele);
1728.
pointer=pointer->l;
1729.
}
1730.
}
1731. QU_ARR
1732.
/* Write C programs that implement Queue (its
operations) using
i) Arrays */
1733.
1734.
#include
1735.
#include
1736.
#include

1737.
#define size 10
1738.
#define true 1
1739.
#define false 0
1740.
1741.
struct q_arr
1742.
{
1743.
int f,r;
1744.
int num;
1745.
int a[size];
1746.
};
1747.
1748.
void init(struct q_arr* queue);
1749.
int e_que(struct q_arr* queue);
1750.
int f_que(struct q_arr* queue);
1751.
int add_ele(struct q_arr* queue,int);
1752.
int rem_ele(struct q_arr* queue);
1753.
void display_ele(struct q_arr* queue);
1754.
1755.
/*main function*/
1756.
void main()
1757.
{
1758.
int ele,k;
1759.
int ch;
1760.
1761.
struct q_arr *queue = (struct
q_arr*)malloc(sizeof(struct q_arr));
1762.
init(queue);
1763.
1764.
while(1)
1765.
{
1766.
clrscr();
1767.
printf("\n\n****IMPLEMENTATION OF QUEUE USING
ARRAYS****\n");
1768.
printf("============================================
");
1769.
printf("\n\t\tMENU\n");
1770.
printf("============================================
");
1771.
printf("\n\t[1] To insert an element");
1772.
printf("\n\t[2] To remove an element");
1773.
printf("\n\t[3] To display all the elements");
1774.
printf("\n\t[4] Exit");
1775.
printf("\n\n\t Enter your choice: ");
1776.
scanf("%d",&ch);
1777.
1778.
switch(ch)
1779.
{
1780.
case 1:
1781.
{
1782.
printf("\nElement to be inserted:");
1783.
scanf("%d",&ele);
1784.
add_ele(queue,ele);
1785.
break;
1786.
}
1787.
1788.
case 2:
1789.
{

1790.
if(!e_que(queue))
1791.
{
1792.
k=rem_ele(queue);
1793.
printf("\n%d element is removed\n",k);
1794.
getch();
1795.
}
1796.
else
1797.
{
1798.
printf("\tQueue is Empty. No element can be
removed.");
1799.
getch();
1800.
}
1801.
break;
1802.
}
1803.
1804.
case 3:
1805.
{
1806.
display_ele(queue);
1807.
getch();
1808.
break;
1809.
}
1810.
1811.
case 4:
1812.
exit(0);
1813.
1814.
default:
1815.
printf("\tInvalid Choice.");
1816.
getch();
1817.
break;
1818.
}
1819.
}
1820.
}
1821.
/*end main*/
1822.
1823.
void init(struct q_arr* queue)
1824.
{
1825.
queue->f = 0;
1826.
queue->r = -1;
1827.
queue->num = 0;
1828.
}
1829.
1830.
/* Function to check is the queue is empty*/
1831.
int e_que(struct q_arr* queue)
1832.
{
1833.
if(queue->num==0)
1834.
return true;
1835.
return false;
1836.
}
1837.
1838.
/* Function to check if the queue is full*/
1839.
int f_que(struct q_arr* queue)
1840.
{
1841.
if(queue->num == size)
1842.
return true;
1843.
return false;
1844.
}
1845.

1846.
/* Function to add an element to the queue*/
1847.
int add_ele(struct q_arr* queue,int j)
1848.
{
1849.
if(f_que(queue))
1850.
return false;
1851.
1852.
if(queue->r == size - 1)
1853.
queue->r = -1;
1854.
queue->a[++queue->r] = j;
1855.
queue->num++;
1856.
return true;
1857.
}
1858.
1859.
/* Function to remove an element of the queue*/
1860.
int rem_ele(struct q_arr* queue)
1861.
{
1862.
int j;
1863.
if(e_que(queue))
1864.
return -9999;
1865.
j = queue->a[queue->f++];
1866.
if(queue->f == size)
1867.
queue->f = 0;
1868.
queue->num--;
1869.
return j;
1870.
}
1871.
1872.
/* Function to display the queue*/
1873.
void display_ele(struct q_arr* queue)
1874.
{
1875.
int j;
1876.
if(e_que(queue))
1877.
{
1878.
printf("Queue is Empty. No records to display.");
1879.
return;
1880.
}
1881.
printf("\nElements present in the Queue are: ");
1882.
for(j=queue->f;j<=queue->r;j++)
1883.
printf("%d\t",queue->a[j]);
1884.
printf("\n");
}
1885. QU_POINT
1886.
/* Write C programs that implement Queue (its
operations) using
ii) Pointers */
1887.
1888.
#define true 1
1889.
#define false 0
1890.
1891.
#include
1892.
#include
1893.
#include
1894.
1895.
struct q_point
1896.
{
1897.
int ele;
1898.
struct q_point* n;
1899.
};

1900.
1901.
struct q_point *f_ptr = NULL;
1902.
1903.
int e_que(void);
1904.
void add_ele(int);
1905.
int rem_ele(void);
1906.
void show_ele();
1907.
1908.
/*main function*/
1909.
void main()
1910.
{
1911.
int ele,choice,j;
1912.
while(1)
1913.
{
1914.
clrscr();
1915.
printf("\n\n****IMPLEMENTATION OF QUEUE USING
POINTERS****\n");
1916.
printf("============================================
==");
1917.
printf("\n\t\t MENU\n");
1918.
printf("============================================
==");
1919.
printf("\n\t[1] To insert an element");
1920.
printf("\n\t[2] To remove an element");
1921.
printf("\n\t[3] To display all the elements");
1922.
printf("\n\t[4] Exit");
1923.
printf("\n\n\tEnter your choice:");
1924.
scanf("%d", &choice);
1925.
1926.
switch(choice)
1927.
{
1928.
case 1:
1929.
{
1930.
printf("\n\tElement to be inserted:");
1931.
scanf("%d",&ele);
1932.
add_ele(ele);
1933.
getch();
1934.
break;
1935.
}
1936.
1937.
case 2:
1938.
{
1939.
if(!e_que())
1940.
{
1941.
j=rem_ele();
1942.
printf("\n\t%d is removed from the queue",j);
1943.
getch();
1944.
}
1945.
else
1946.
{
1947.
printf("\n\tQueue is Empty.");
1948.
getch();
1949.
}
1950.
break;
1951.
}
1952.
1953.
case 3:

1954.
show_ele();
1955.
getch();
1956.
break;
1957.
1958.
case 4:
1959.
exit(1);
1960.
break;
1961.
1962.
default:
1963.
printf("\n\tInvalid choice.");
1964.
getch();
1965.
break;
1966.
}
1967.
1968.
}
1969.
}
1970.
1971.
/* Function to check if the queue is empty*/
1972.
int e_que(void)
1973.
{
1974.
if(f_ptr==NULL)
1975.
return true;
1976.
return false;
1977.
}
1978.
1979.
/* Function to add an element to the queue*/
1980.
void add_ele(int ele)
1981.
{
1982.
struct q_point *queue = (struct
q_point*)malloc(sizeof(struct q_point));
1983.
queue->ele = ele;
1984.
queue->n = NULL;
1985.
if(f_ptr==NULL)
1986.
f_ptr = queue;
1987.
else
1988.
{
1989.
struct q_point* ptr;
1990.
ptr = f_ptr;
1991.
for(ptr=f_ptr ;ptr->n!=NULL; ptr=ptr->n);
1992.
ptr->n = queue;
1993.
}
1994.
}
1995.
1996.
/* Function to remove an element from the queue*/
1997.
int rem_ele()
1998.
{
1999.
struct q_point* queue=NULL;
2000.
if(e_que()==false)
2001.
{
2002.
int j = f_ptr->ele;
2003.
queue=f_ptr;
2004.
f_ptr = f_ptr->n;
2005.
free (queue);
2006.
return j;
2007.
}
2008.
else
2009.
{

2010.
printf("\n\tQueue is empty.");
2011.
return -9999;
2012.
}
2013.
}
2014.
2015.
/* Function to display the queue*/
2016.
void show_ele()
2017.
{
2018.
struct q_point *ptr=NULL;
2019.
ptr=f_ptr;
2020.
if(e_que())
2021.
{
2022.
printf("\n\tQUEUE is Empty.");
2023.
return;
2024.
}
2025.
else
2026.
{
2027.
printf("\n\tElements present in Queue are:\n\t");
2028.
while(ptr!=NULL)
2029.
{
2030.
printf("%d\t",ptr->ele);
2031.
ptr=ptr->n;
2032.
}
2033.
}
2034.
}
2035.
2036.
2037. IN_POST
2038.
/* Write a C program that uses Stack operations to
perform the following:
2039.
i) Converting infix expression into postfix
expression
2040.
ii) Evaluating the postfix expression */
2041.
2042.
#include
2043.
#include
2044.
2045.
int st[100];
2046.
int st_top=-1;
2047.
2048.
int cal(char post[]);
2049.
void in_post(char in[]);
2050.
void push_item(int it);
2051.
int pop_item();
2052.
int st_ISP(char t);
2053.
int st_ICP(char t);
2054.
2055.
/*main function*/
2056.
void main()
2057.
{
2058.
char in[100],post[100];
2059.
clrscr();
2060.
printf("\n\tEnter the Infix Expression: ");
2061.
gets(in);
2062.
in_post(in);
2063.
getch();

2064.
}
2065.
/*end main*/
2066.
2067.
void push_item(int it)
2068.
{
2069.
if(st_top==99)
2070.
{
2071.
printf("\n\n\t*STACK is Full*");
2072.
getch();
2073.
exit(1);
2074.
}
2075.
st[++st_top]=it;
2076.
}
2077.
2078.
int pop_item()
2079.
{
2080.
int it;
2081.
if(st_top==-1)
2082.
{
2083.
getch();
2084.
}
2085.
return(st[st_top--]);
2086.
}
2087.
2088.
/*Function for converting an infix expression to a
postfix expression. */
2089.
void in_post(char in[])
2090.
{
2091.
int x=0,y=0,z,result=0;
2092.
char a,c, post[100];
2093.
char t;
2094.
push_item('\0');
2095.
t=in[x];
2096.
while(t!='\0')
2097.
{
2098.
if(isalnum(t))
2099.
/*For checking whether the value in t is an alphabet
or number. */
2100.
{
2101.
post[y]=t;
2102.
y++;
2103.
}
2104.
else if(t=='(')
2105.
{
2106.
push_item('(');
2107.
}
2108.
else if(t==')')
2109.
{
2110.
while(st[st_top]!='(')
2111.
{
2112.
c=pop_item();
2113.
post[y]=c;
2114.
y++;
2115.
}
2116.
c=pop_item();
2117.
}
2118.
else

2119.
{
2120.
while(st_ISP(st[st_top])>=st_ICP(t))
2121.
{
2122.
c=pop_item();
2123.
post[y]=c;
2124.
y++;
2125.
}
2126.
push_item(t);
2127.
}
2128.
x++;
2129.
t=in[x];
2130.
}
2131.
2132.
while(st_top!=-1)
2133.
{
2134.
c=pop_item();
2135.
post[y]=c;
2136.
y++;
2137.
}
2138.
printf("\n\tThe Postfix Expression is:");
2139.
2140.
for(z=0;z0)
2141.
{
2142.
printf("Number of Operands are more than
Operators.");
2143.
exit(0);
2144.
}
2145.
else
2146.
{
2147.
y=pop_item();
2148.
return (y);
2149.
}
2150.
return 0;
2151.
}
2152.
2153. BINARYTREE
2154.
/* Write a C program that uses functions to perform the
following:
2155.
i) Creating a Binary Tree of integers
2156.
ii) Traversing the above binary tree in preorder,
inorder and postorder. */
2157.
2158.
#include
2159.
#include
2160.
#include
2161.
2162.
struct treenode
2163.
{
2164.
int ele;
2165.
struct treenode *l_child, *r_child;
2166.
};
2167.
2168.
struct treenode *insert_node(struct treenode *t,int a);
2169.
void TraverseInorder(struct treenode *t);
2170.
void TraversePreorder(struct treenode *t);
2171.
void TraversePostorder(struct treenode *t);

2172.
2173.
/*main function*/
2174.
void main()
2175.
{
2176.
struct treenode *root_node = NULL;
2177.
int num,value;
2178.
int choice;
2179.
2180.
clrscr();
2181.
2182.
printf("---------------------------------------------------\n");
2183.
printf("\t\t\tMENU\n");
2184.
printf("----------------------------------------------------\n");
2185.
printf("[1] Create a Binary Tree and Use Inorder
Traversal\n");
2186.
printf("[2] Create a Binary Tree and Use Preorder
Traversal\n");
2187.
printf("[3] Create a Binary Tree and Use Postorder
Traversal\n");
2188.
printf("----------------------------------------------------\n");
2189.
printf("Enter your choice:");
2190.
scanf("%d",&choice);
2191.
2192.
if(choice>0 & choice<=3)
2193.
{
2194.
printf("\nEnter the number of nodes:");
2195.
scanf("%d",&num);
2196.
2197.
while(num-- > 0)
2198.
{
2199.
printf("\n\nEnter the data value:");
2200.
scanf("%d",&value);
2201.
root_node = insert_node(root_node,value);
2202.
}
2203.
2204.
switch(choice)
2205.
{
2206.
case 1:
2207.
printf("\n\nBinary tree using Inorder
Traversal : ");
2208.
TraverseInorder(root_node);
2209.
getch();
2210.
break;
2211.
2212.
case 2:
2213.
printf("\n\nBinary tree using Preorder
Traversal : ");
2214.
TraversePreorder(root_node);
2215.
getch();
2216.
break;
2217.
2218.
case 3:
2219.
printf("\n\nBinary tree using Postorder
Traversal : ");

2220.
TraversePostorder(root_node);
2221.
getch();
2222.
break;
2223.
2224.
default:
2225.
printf("Invalid Choice");
2226.
break;
2227.
}
2228.
}
2229.
}
2230.
/*end main*/
2231.
2232.
/* Function to create a Binary Tree of integers data */
2233.
struct treenode *insert_node(struct treenode *t,int a)
2234.
{
2235.
struct treenode *temp_node1,*temp_node2;
2236.
if(t == NULL)
2237.
{
2238.
t = (struct treenode *) malloc(sizeof(struct
treenode));
2239.
if(t == NULL)
2240.
{
2241.
printf("Value cannot be allocated.\n");
2242.
exit(0);
2243.
}
2244.
t->ele = a;
2245.
t->l_child=t->r_child=NULL;
2246.
}
2247.
else
2248.
{
2249.
temp_node1 = t;
2250.
2251.
while(temp_node1 != NULL)
2252.
{
2253.
temp_node2 = temp_node1;
2254.
if( temp_node1 ->ele > a)
2255.
temp_node1 = temp_node1->l_child;
2256.
else
2257.
temp_node1 = temp_node1->r_child;
2258.
}
2259.
if( temp_node2->ele > a)
2260.
{
2261.
temp_node2->l_child = (struct
treenode*)malloc(sizeof(struct treenode));
2262.
temp_node2 = temp_node2->l_child;
2263.
if(temp_node2 == NULL)
2264.
{
2265.
printf("Value cannot be allocated.\n");
2266.
exit(0);
2267.
}
2268.
temp_node2->ele = a;
2269.
temp_node2->l_child=temp_node2->r_child = NULL;
2270.
}
2271.
else
2272.
{
2273.
temp_node2->r_child = (struct
treenode*)malloc(sizeof(struct treenode));

2274.
2275.
temp_node2 = temp_node2->r_child;
2276.
if(temp_node2 == NULL)
2277.
{
2278.
printf("Value cannot be allocated.\n");
2279.
exit(0);
2280.
}
2281.
temp_node2->ele = a;
2282.
temp_node2->l_child=temp_node2->r_child = NULL;
2283.
}
2284.
}
2285.
return(t);
2286.
}
2287.
2288.
/* Function for Traversing the binary tree in inorder.
*/
2289.
void TraverseInorder(struct treenode *t)
2290.
{
2291.
if(t != NULL)
2292.
{
2293.
TraverseInorder(t->l_child);
2294.
printf("%d\t",t->ele);
2295.
in_order(t->r_child);
2296.
}
2297.
}
2298.
2299.
/* Function for Traversing the binary tree in preorder.
*/
2300.
void TraversePreorder(struct treenode *t)
2301.
{
2302.
if(t != NULL)
2303.
{
2304.
printf("%d\t",t->ele);
2305.
TraversePreorder(t->l_child);
2306.
TraversePreorder(t->r_child);
2307.
}
2308.
}
2309.
2310.
/* Function for Traversing the binary tree in postorder.
*/
2311.
void TraversePostorder(struct treenode *t)
2312.
{
2313.
if(t != NULL)
2314.
{
2315.
TraversePostorder(t->l_child);
2316.
TraversePostorder(t->r_child);
2317.
printf("%d\t",t->ele);
2318.
}
2319.
}
2320. BINARY_SEARCH
2321.
/* Write C programs that use both recursive and non
recursive functions to perform
2322.
the following searching operations for a Key value in
a given list of integers :
2323.
ii) Binary search*/
2324.

2325.
#include
2326.
#define MAX_LEN 10
2327.
2328.
/* Non-Recursive function*/
2329.
void b_search_nonrecursive(int l[],int num,int ele)
2330.
{
2331.
int l1,i,j, flag = 0;
2332.
l1 = 0;
2333.
i = num-1;
2334.
while(l1 <= i)
2335.
{
2336.
j = (l1+i)/2;
2337.
if( l[j] == ele)
2338.
{
2339.
printf("\nThe element %d is present at position
%d in list\n",ele,j);
2340.
flag =1;
2341.
break;
2342.
}
2343.
else
2344.
if(l[j] < ele)
2345.
l1 = j+1;
2346.
else
2347.
i = j-1;
2348.
}
2349.
if( flag == 0)
2350.
printf("\nThe element %d is not present in the
list\n",ele);
2351.
}
2352.
2353.
/* Recursive function*/
2354.
int b_search_recursive(int l[],int arrayStart,int
arrayEnd,int a)
2355.
{
2356.
int m,pos;
2357.
if (arrayStart<=arrayEnd)
2358.
{
2359.
m=(arrayStart+arrayEnd)/2;
2360.
if (l[m]==a)
2361.
return m;
2362.
else if (a<="2" int="" l[],int="" l[max_len],=""
main()="" method");="" n)="" non-recursion="" num,=""
print_list(int="" printf("%d\t",l[i]);=""
printf("="=====================================================")
;"
printf("\n="===================================================="
);" printf("\n[1]="" printf("\n[2]="" printf("\n\nenter=""
printf("\n\t\t\tmenu");="" printf("\nenter="" read_list(int=""
recursion="" return="" scanf("%d",&ch);="" scanf("%d",&l[i]);=""
search="" the="" using="" void="" your="" {="" }="">0)
2363.
{
2364.
printf("\nEnter the number of elements : ");
2365.
scanf("%d",&num);
2366.
read_list(l,num);
2367.
printf("\nElements present in the list are:\n\n");
2368.
print_list(l,num);

2369.
printf("\n\nEnter the element you want to
search:\n\n");
2370.
scanf("%d",&ele);
2371.
2372.
2373.
switch(ch)
2374.
{
2375.
case 1:printf("\nRecursive method:\n");
2376.
pos=b_search_recursive(l,0,num,ele);
2377.
if(pos==-1)
2378.
{
2379.
printf("Element is not found");
2380.
}
2381.
else
2382.
{
2383.
printf("Element is found at %d
position",pos);
2384.
}
2385.
getch();
2386.
break;
2387.
2388.
case 2:printf("\nNon-Recursive method:\n");
2389.
b_search_nonrecursive(l,num,ele);
2390.
getch();
2391.
break;
2392.
}
2393.
}
2394.
getch();
2395.
}
2396.
2397.
2398. LINEAR_SEARCH
2399.
/* Write C programs that use both recursive and non
recursive functions
2400.
to perform the following searching operation for a Key
value in a given list of integers :
2401.
i) Linear search */
2402.
2403.
#include
2404.
#define MAX_LEN 10
2405.
2406.
void l_search_recursive(int l[],int num,int ele);
2407.
void l_search(int l[],int num,int ele);
2408.
void read_list(int l[],int num);
2409.
void print_list(int l[],int num);
2410.
2411.
void main()
2412.
{
2413.
int l[MAX_LEN], num, ele;
2414.
int ch;
2415.
2416.
clrscr();
2417.
2418.
printf("============================================
==========");
2419.
printf("\n\t\t\tMENU");

2420.
printf("\n==========================================
===========");
2421.
printf("\n[1] Linary Search using Recursion
method");
2422.
printf("\n[2] Linary Search using Non-Recursion
method");
2423.
printf("\n\nEnter your Choice:");
2424.
scanf("%d",&ch);
2425.
2426.
if(ch<=2 & ch>0)
2427.
{
2428.
printf("Enter the number of elements :");
2429.
scanf("%d",&num);
2430.
read_list(l,num);
2431.
printf("\nElements present in the list are:\n\n");
2432.
print_list(l,num);
2433.
printf("\n\nElement you want to search:\n\n");
2434.
scanf("%d",&ele);
2435.
2436.
switch(ch)
2437.
{
2438.
case 1:printf("\n**Recursion method**\n");
2439.
l_search_recursive(l,num,ele);
2440.
getch();
2441.
break;
2442.
2443.
case 2:printf("\n**Non-Recursion method**\n");
2444.
l_search_nonrecursive(l,num,ele);
2445.
getch();
2446.
break;
2447.
}
2448.
}
2449.
getch();
2450.
}
2451.
/*end main*/
2452.
2453.
/* Non-Recursive method*/
2454.
void l_search_nonrecursive(int l[],int num,int ele)
2455.
{
2456.
int j, f=0;
for(j=0;j<="" at="" break;="" ele)="" element=""
elements:\n");="" else="" f="1;" for(j="0;j<num;j++)"
found.",ele);="" getch();="" if((num="=0)" if(="" if(f="=0)"
in="" int="" is="" j;="" l[],int="" l[j]="=" l[num]="="
l_search(l,num-1,ele);="" l_search_recursive(int=""
list\n",ele);="" list\n",ele,j);="" list\n",ele,num);=""
method*="" not="" num)="" num,int="" position="" pre=""
present="" print_list(int="" printf("%d\t",l[j]);=""
printf("\nenter="" printf("\nthe="" printf("the=""
read_list(int="" recursive="" scanf("%d",&l[j]);="" the=""
void="" {="" }="">

2457. BUBBLE_SORT

2458.
/* Write C programs that implement the following sorting
methods to sort
2459.
a given list of integers in ascending order: i)
Bubble sort */
2460.
2461.
#include
2462.
#define MAX 10
2463.
2464.
void swapList(int *m,int *n)
2465.
{
2466.
int temp;
2467.
temp = *m;
2468.
*m = *n;
2469.
*n = temp;
2470.
}
2471.
2472.
// Function for Bubble Sort
2473.
void bub_sort(int list[], int n)
2474.
{
2475.
int i,j;
2476.
for(i=0;i<(n-1);i++)
2477.
for(j=0;j<(n-(i+1));j++)
2478.
if(list[j] > list[j+1])
2479.
swapList(&list[j],&list[j+1]);
2480.
}
2481.
2482.
void readlist(int list[],int n)
2483.
{
2484.
int j;
2485.
printf("\nEnter the elements: \n");
for(j=0;j<="" }="">
2486. QUICK_SORT
2487.
/* Write C program that implement the following sorting
methods
2488.
to sort a given list of integers in ascending order: ii)
Quick sort */
2489.
2490.
#include
2491.
#define MAX 10
2492.
2493.
void swap(int *m,int *n)
2494.
{
2495.
int temp;
2496.
temp = *m;
2497.
*m = *n;
2498.
*n = temp;
2499.
}
2500.
int get_key_position(int x,int y )
2501.
{
2502.
return((x+y) /2);
2503.
}
2504.
2505.
// Function for Quick Sort
2506.
void quicksort(int list[],int m,int n)
2507.
{

2508.
2509.
2510.
2511.
2512.
2513.
2514.
2515.
2516.
2517.
2518.
2519.
2520.
2521.
2522.
2523.
2524.
2525.
2526.
2527.
2528.
2529.
2530.
2531.
2532.
2533.
2534.
2535.

int key,i,j,k;
if( m < n)
{
k = get_key_position(m,n);
swap(&list[m],&list[k]);
key = list[m];
i = m+1;
j = n;
while(i <= j)
{
while((i <= n) && (list[i] <= key))
i++;
while((j >= m) && (list[j] > key))
j--;
if( i < j)
swap(&list[i],&list[j]);
}
swap(&list[m],&list[j]);
quicksort(list,m,j-1);
quicksort(list,j+1,n);
}

// Function to read the data


void read_data(int list[],int n)
{
int j;
printf("\n\nEnter the elements:\n");
for(j=0;j

2536. INSERT_SORT
2537.
/* Write C program that implement the following sorting
methods
2538.
to sort a given list of integers in ascending order: i)
Insertion sort */
2539.
2540.
#include
2541.
#include
2542.
2543.
void inst_sort(int []);
2544.
2545.
void main()
2546.
{
2547.
int num[5],count;
2548.
clrscr();
2549.
printf("\nEnter the Five Elements to sort:\n");
2550.
2551.
for (count=0;count<5;count++)
2552.
scanf("%d",&num[count]);
2553.
inst_sort(num);
2554.

2555.
2556.
2557.
2558.
2559.
2560.
2561.
2562.
2563.
2564.
2565.
2566.
2567.

printf("\n\nElements after sorting: \n");


for(count=0;count<5;count++)
printf("%d\n",num[count]);
getch();
}
// Function for Insertion Sorting
void inst_sort(int num[])
{
int i,j,k;
for(j=1;j<5;j++)
{
k=num[j];
for(i=j-1;i>=0 && k<="" }="">

2568. MERGE_SORT
2569.
/* Write C program that implement the following sorting
methods to sort a given list of integers in ascending order:
2570.
ii) Merge sort */
2571.
2572.
#include
2573.
#include
2574.
2575.
#define MAX_ARY 10
2576.
2577.
void merge_sort(int x[], int end, int start);
2578.
2579.
int main(void) {
2580.
int ary[MAX_ARY];
2581.
int j = 0;
2582.
2583.
printf("\n\nEnter the elements to be sorted: \n");
2584.
for(j=0;j<="" after="" array="" ary[j]);="" before=""
const="" end)="" end,="" end="" executing[j]="x[end"
executing[max_ary];="" for(j="0;" getch();="" if(end="="
if(executing[mrg1]="" if(mrg1="" if(mrg2="" implement="" int=""
j++)="" j="" j];="" max_ary;="" max_ary="" merge=""
merge_sort(ary,="" merge_sort(int="" merge_sort(x,=""
mergesort="" method="" mid);="" mid="0;" mrg1="0;" mrg2="0;"
printf("="" printf("\n");="" printf("after="" printf("before=""
return;="" scanf("%d",&ary[j]);="" size;="" size="start" sort*=""
sort="" start);="" start)="" to="" void="" x[],="" {="" }="">
executing[mrg2])
2585.
x[j + end] = executing[mrg2++];
2586.
else
2587.
x[j + end] = executing[mrg1++];
2588.
else
2589.
x[j + end] = executing[mrg2++];
2590.
else
2591.
x[j + end] = executing[mrg1++];
2592.
}
2593.
}
2594. LAGRANGE
2595.
/* Write C program to implement the Lagrange
interpolation.*/
2596.

2597.
#include
2598.
#include
2599.
#define MaxN 90
2600.
2601.
void main()
2602.
{
2603.
float arr_x[MaxN+1], arr_y[MaxN+1], numerator,
denominator, x, y=0;
2604.
int i, j, n;
2605.
clrscr();
2606.
printf("Enter the value of n: \n");
2607.
scanf("%d", &n);
2608.
printf("Enter the values of x and y: \n");
2609.
for(i=0; i<=n; i++)
2610.
scanf("%f%f", &arr_x[i], &arr_y[i]);
2611.
printf("Enter the value of x at which value of y is to
be calculated: ");
2612.
scanf("%f", &x);
2613.
for (i=0; i<=n; i++)
2614.
{
2615.
numerator=1;
2616.
denominator=1;
2617.
for (j=0; j<=n; j++)
2618.
if(j!=i)
2619.
{
2620.
numerator *= x-arr_x[j];
2621.
denominator *= arr_x[i]-arr_x[j];
2622.
}
2623.
y+=(numerator/denominator)*arr_y[i];
2624.
}
2625.
printf("When x=%4.1f y=%7.1f\n",x,y);
2626.
getch();
2627.
}
2628.
2629.
2630. NEWTON_G
2631.
/* Write C program to implement the Newton- Gregory
forward interpolation.*/
2632.
2633.
#include
2634.
#include
2635.
#define MaxN 100
2636.
#define Order_of_diff 4
2637.
2638.
void main ()
2639.
{
2640.
float arr_x[MaxN+1], arr_y[MaxN+1], numerator=1.0,
denominator=1.0, x, y, p, h, diff_table[MaxN+1][Order_of_diff+1];
2641.
int i,j,n,k;
2642.
clrscr();
2643.
2644.
printf("Enter the value of n \n");
2645.
scanf("%d",&n);
2646.
printf("Enter the values of x and y");
2647.
2648.
for(i=0; i<=n; i++)

2649.
scanf("%f%f", &arr_x[i], &arr_y[i]);
2650.
printf("Enter the value of x at which value of y is to
be calculated");
2651.
scanf("%f", &x);
2652.
h=arr_x[1]-arr_x[0];
2653.
2654.
for(i=0; i<=n-1; i++)
2655.
diff_table[i][1]=arr_y[i+1]-arr_y[i];/*Creating the
difference table and calculating first order differences*/
2656.
for(j=2; j<=Order_of_diff; j++)/*Calculating higher
order differences*/
2657.
for(i=0; i<=n-j; i++)
2658.
diff_table[i][j]=diff_table[i+1][j-1] - diff_table[i]
[j-1];
2659.
i=0;
2660.
2661.
while(!(arr_x[i]>x)) /* Finding x0 */
2662.
i++;
2663.
i--;
2664.
p=(x-arr_x[i])/h;
2665.
y=arr_y[i];
2666.
2667.
for (k=1; k<=Order_of_diff; k++)
2668.
{
2669.
numerator *=p-k+1;
2670.
denominator *=k;
2671.
y +=(numerator/denominator)*diff_table[i][k];
2672.
}
2673.
printf("When x=%6.1f, y=%6.2f\n",x, y);
2674.
getch();
2675.
}
2676.
2677.
2678. LINEAR_REGRESSION
2679.
/* Write C program to implement the linear regression
algorithm. */
2680.
2681.
#include
2682.
#include
2683.
#include
2684.
#include
2685.
2686.
float mean(float *a, int n);
2687.
void deviation(float *a, float mean, int n, float *d,
float *S);
2688.
2689.
void main()
2690.
{
2691.
float a[20],b[20],dx[20],dy[20];
2692.
float sy=0,sx=0,mean_x=0,mean_y=0,sum_xy=0;
2693.
float corr_coff=0,reg_coff_xy=0, reg_coff_yx=0;
2694.
char type_coff[7];
2695.
int n=0,i=0;
2696.
2697.
clrscr();
2698.

2699.
2700.
2701.

printf("Enter the value of n: ");


scanf("%d",&n);
printf("Enter the values of x and y:\n");
for(i=0;i<="" }="">

2702. SIMPSON
2703.
/* Write C program to implement Simpson method. */
2704.
2705.
#include
2706.
#include
2707.
#include
2708.
2709.
char postfix[80];
2710.
float stack[80];
2711.
char stack1[80];
2712.
int top=-1,top1=-1;
2713.
float eval(char postfix[], float x1);
2714.
void infix_postfix(char infix[]);
2715.
2716.
main()
2717.
{
2718.
float x0, xn, h, s,e1,e2, e3;
2719.
char exp[80], arr[80];
2720.
int i,n,l=0;
2721.
clrscr();
2722.
printf("\nEnter an expression: ");
2723.
gets(exp);
2724.
puts("Enter x0, xn and number of sub-intervals: ");
2725.
scanf("%f%f%d", &x0, &xn, &n);
2726.
h=(xn-x0)/n;
2727.
if(exp[0]=='l'&& exp[1]=='o'&& exp[2]=='g')
2728.
{
2729.
l=strlen(exp);
2730.
for(i=0;i=ICP(token))
2731.
{
2732.
ch=pop1();
2733.
/*Assigning the popped element into the postfix array.
*/
2734.
postfix[j]=ch;
2735.
j++;
2736.
}
2737.
push1(token);
2738.
}
2739.
i++;
2740.
token=infix[i];
2741.
}
2742.
while(top1!=0)
2743.
{
2744.
ch=pop1();
2745.
postfix[j]=ch;
2746.
j++;
2747.
}
2748.
postfix[j]='\0';
2749.
}
2750.
2751.
int ISPriority(char token)

2752.
{
2753.
switch(token)
2754.
{
2755.
case '(':return (0);
2756.
case ')':return (9);
2757.
case '+':return (7);
2758.
case '-':return (7);
2759.
case '*':return (8);
2760.
case '/':return (8);
2761.
case '?':return (0);
2762.
default: printf("Invalid expression");
2763.
break;
2764.
}
2765.
return 0;
2766.
}
2767.
/*Determining the priority of elements that are
approaching towards the stack. */
2768.
int ICP(char token)
2769.
{
2770.
switch(token)
2771.
{
2772.
case '(':return (10);
2773.
case ')':return (9);
2774.
case '+':return (7);
2775.
case '-':return (7);
2776.
case '*':return (8);
2777.
case '/':return (8);
2778.
case '\0':return (0);
2779.
default: printf("Invalid expression");
2780.
break;
2781.
}
2782.
return 0;
2783.
}
2784.
/*Calculating the result of expression, which is
converted in postfix notation. */
2785.
float eval(char p[], float x1)
2786.
{
2787.
float t1,t2,k,r;
2788.
int i=0,l;
2789.
l=strlen(p);
2790.
while(i0)
2791.
{
2792.
printf("You have entered the operands more than the
operators");
2793.
exit(0);
2794.
}
2795.
else
2796.
{
2797.
r=pop();
2798.
return (r);
2799.
}
2800.
return 0;
}

Potrebbero piacerti anche