CP Lab Manual
CP Lab Manual
of IT */
Week1a
Page:1 */
START
READ NUM
SUMDIGIT=0
DO WHILE NUM > 0
4.1 SUMDIGIT=SUMDIGIT + NUM MOD 10
4.2 NUM=NUM/10
5. END WHILE
6. PRINT NUM
7. END.
CODE:
/*Week1a.c */
#include <stdio.h>
#include <conio.h>
int main()
{
long num;
int sumdigit=0;
clrscr();
printf("Enter a positive integer : ");
scanf("%ld",&num);
while(num>0)
{
sumdigit = sumdigit + num % 10;
num = num / 10;
}
printf("Sum of individual digits = %d\n",sumdigit);
getch();
return (0);
}
Output:
Enter a positive integer : 23456
Sum of individual digits = 20
Page:2 */
Problem Definition:
A Fibonacci sequence is defined as follows:
The first and second terms in the sequence are 0 and 1. Subsequent terms are found by adding the
preceding two terms in the sequence. Write a C program to generate the first n terms of the sequence..
Understanding Problem:
Fibonacci series is:
0
1
1
2
3
5
8
Where current number is sum last two numbers
13
21
34
55
ALGORITHM:
1
2
3
4
5
6
7
8
START
FIRST=0
SECOND=0
PRINT FIRST, SECOND
INPUT N
FOR X = 2 TO N
6.1 CURRENT = FIRST + SECOND
6.2 PRINT CURRENT
6.3 FIRST=SECOND
6.4 SECOND=CURRENT
NEXT X
END.
CODE:
/*week1b.c*/
#include <stdio.h>
main()
{
long first,second,current;
int n,x;
clrscr();
printf("How many numbers of Fibonacci series? : ");
scanf("%d",&n);
first=0; second=1;
printf("%10ld%10ld",first,second);
for(x=2;x<=n;x++)
{
current=first+second;
printf("%10ld",current);
first=second;
second=current;
}
return (0);
}
Output:
How many numbers of Fibonacci series? : 20
0
1
1
2
3
5
8
377
610
987
1597
2584 4181
13
6765
21
34
55
89
144
233
Page:3 */
Problem Definition:
Write a C program to generate all prime numbers between 1 and n, where n is a value supplied
by the user
Understanding the problem and process logic:
A number is prime if it exactly divisible only by 1 and number itself. We can check the divisibility
from 1 to number to find total number of factors whether it is 2 or more.
Mathematicians suggest, if a number is not exactly divisible from 2 to integer value of square
root of number then it can be declared as prime. So to reduce the number of comparisons we
will check the divisibility from 2 to square root of the number.
We will set the prime as true first before checking divisibility. If any divider is found we can set
the prime false and terminate the loop. Checking the status of variable prime we can declare
whether it is prime or not.
One outer loop is required to feed the number within the range each time to be checked by inner
loop.
ALGORITHM
1
2
3
4
START
ACCEPT N
(RANGE)
PRINT PRIME NUMBERS FROM 1 TO N, N
FOR NUM = 2 TO N
4.1 PRIME=TRUE
4.2 FOR D = 2 TO SQRT(N)
4.2.1 IF N MOD D = 0 THEN
4.2.1.1 PRIME=FALSE
4.2.1.2 BREAK
4.2.2 ENDIF
4.3 NEXT D
4.4 IF PRIME=TRUE THEN
4.4.1 PRINT NUM
4.5 ENDIF
5 NEXT NUM
6 END
Page:4 */
13
43
79
113
163
199
251
293
349
397
443
491
557
601
647
701
757
821
863
929
983
17
47
83
127
167
211
257
307
353
401
449
499
563
607
653
709
761
823
877
937
991
Page:5 */
Problem Definition:
Write a C program to calculate the following sum:
sum= 1 x2/2! + x4/4! x6/6! + x8/8! x10/10!
Understanding Problem and Process Logic:
First the sum is to initialized 1 then loop will run from 2 to 10 in step 2. First, third and fifth time
the term is to subtracted and second and fourth time it is to be added. So if a variable c should
be initialized to 1 and will be incremented by 1 in each iteration. Checking the status of c MOD 2
(0 or 1), the term will added or subtracted. Within the loop power and factorial are to be
computed. For power we can use pow ( ) function and for factorial we can use an inner loop.
ALGORITHM:
1
2
3
4
5
START
SUM=1
INPUT X
C=1
FOR A = 2 TO 10 STEP 2
5.1 B=A
5.2 F=1
5.3 DO WHILE B >= 1
5.3.1 F=F*B
5.3.2 B=B-1
5.4 END WHILE
5.5 IF C MOD 2 = 0 THEN
5.5.1 SUM=SUM+POWER(X,A)/F
5.6 ELSE
5.6.1 SUM=SUM-POWER(X,A)/F
5.7 ENDIF
5.8 C=C+1
6 NEXT A
7 PRINT SUM
8 END.
Page:6 */
CODE:
/*Week2a.c*/
#include <stdio.h>
#include <math.h>
#include <conio.h>
main()
{
int a,b;
long int f;
int c;
float x,sum=1.00;
clrscr();
printf("Enter value of x : ");
scanf("%f",&x);
c=1;
for(a=2;a<=10;a+=2)
{
b=a;/* to find factorials of 2,4,6,8 and 10 */
f=1;
while(b>=1)
{
f*=b;
b--;
}
/* factorial of b is calculated in f */
if(c%2==0)
sum=sum+pow(x,a)/f; /* when factorial of 4 & 8 */
else
sum=sum-pow(x,a)/f; /* when factorial of 2, 6 & 10 */
c++;
}
printf("Sum = %.3f\n",sum);
return (0);
}
Output:
Enter value of x : 4
Sum = -0.686
START
INPUT A, B, C
DESCR=B*B - 4*A*C
IF DESCR < 0 THEN
4.1 PRINT ROOTS ARE IMAGINARY
5 ELSE
5.1 R1=(-B+SQRT(DESCR))/(2*A)
5.2 R2=(-B-SQRT(DESCR))/(2*A)
5.3 PRINT R1, R2
6 ENDIF
7 END.
CODE:
/*Week2b.c */
#include <stdio.h>
#include <math.h>
#include <conio.h>
main()
{
float a,b,c,descr,r1,r2;
clrscr();
printf("Enter values of a,b & c : ");
scanf("%f%f%f",&a,&b,&c);
descr=b*b-4*a*c;
if(descr<0)
printf("Roots are imaginary\n");
else
{
r1=(-b+sqrt(descr))/(2*a);
r2=(-b-sqrt(descr))/(2*a);
printf("Root1 = %.3f Root2 = %.3f\n",r1,r2);
}
return (0);
}
Output:
Enter values of a,b & c : 4 12 5
Root1 = -0.500 Root2 = -2.500
Page:7 */
Page:8 */
Problem Definition:
Write a C program that use both recursive and non recursive functions:
i. to find factorial of a given number
Understanding the problem and process logic:
Recursion is a process where a function calls itself. C language supports recursion.
If n! = n*(n-1)*(n-2)*..1
It can be written as
n! = n*(n-1)!
Means
Factorial(n) = n*factorial(n-1)
To find factorial by non-recursive function we will use loop to find
n! = n*(n-1)*(n-2)*..1
ALGORITHM ( USING RECURSION)
1. FACTORIAL1(N)
2. IF N= 0 OR N = 1 THEN
3.1. RETURN 1
3. ELSE
4.1 RETURN (N*FACTOIAL1(N-1))
4. ENDIF
ALGORITHM (USING NON-RECURSIVE FUNCTION )
1. FACTORIAL2(N)
2. F=1
3. FOR X = N TO 1 STEP 1
1.1 F=F*X
4. NEXT X
5. RETURN F
ALGORITHM OF MAIN FUNCTION
1.
2
3
4
5
START
INPUT N
PRINT FACTORIAL OF , N , BY RECURSIVE FUNCTION= , FACTORIAL1(N)
PRINT FACTORIAL OF , N , BY NON-RECURSIVE FUNCTION= , FACTORIAL1(N)
END.
Page:9 */
/*Week3i.c */
#include <stdio.h>
#include <conio.h>
/* recursive function*/
long int factorial(int n)
{
if(n<=1)
return (1);
else
return (n*factorial(n-1));
}
/* non-recursive function */
void FACTORIAL(int n)
{
long int f=1;
int k;
for(k=n;k>=1;k--)
f*=k;
printf("Factorial of %d (by non-recursive function)= %ld\n",n,f);
}
main()
{
int n;
clrscr();
printf("Enter a number : ");
scanf("%d",&n);
printf("Factorial of %d (by recursive function) = %ld\n",
n,factorial(n));
FACTORIAL(n); /* calling non-recursive function */
getch();
return (0);
}
Output:
Enter a number : 7
Factorial of 7 (by recursive function) = 5040
Factorial of 7 (by non-recursive function)= 5040
Page:10 */
Page:11 */
/*Week3ii.c */
#include <stdio.h>
#include <conio.h>
/* recursive function*/
int GCD(int dividend,int divider)
{
int rem=dividend%divider;
if(rem==0)
return (divider);
else
return GCD(divider, rem);
}
/* non-recursive function */
int gcd(int n1,int n2)
{
while(n1!=n2)
{
if(n1>n2)
n1=n1-n2;
else
n2=n2-n1;
}
return (n1);
}
main()
{
int n1,n2;
clrscr();
printf("Enter two numbers : ");
scanf("%d%d",&n1,&n2);
printf("GCD (using recursive function) = %d\n",GCD(n1,n2));
printf("gcd (using non-recursive function = %d\n",gcd(n1,n2));
getch();
return (0);
}
Output:
Enter two numbers : 230 45
GCD (using recursive function) = 5
GCD (using non-recursive function = 5
Page:12 */
Week3III
Write a C program to solve Tower of hanoi problem.
Tower of Hanoi
The Tower of Hanoi (sometimes referred to as the Tower of Brahma or the End of the World
Puzzle) was invented by the French mathematician, Edouard Lucas, in 1883. He was inspired
by a legend that tells of a Hindu temple where the pyramid puzzle might have been used for the
mental discipline of young priests. Legend says that at the beginning of time the priests in the
temple were given a stack of 64 gold disks, each one a little smaller than the one beneath it.
Their assignment was to transfer the 64 disks from one of the three poles to another, with
important conditions that disk could never be placed on top of a smaller one and it will be moved
one by one. The priests started working very efficiently, day and night.
This is one of the worst case complexity example of data structure algorithms. Total number of
movements will be 264 1. if one movement can be done in one second then also it will take
nearly 600 billion years to finish the task (End of the world as myth)
Earlier it was discussed that it can be solved by recursive function and it is very tough to
solve by non-recursive function without using goto statement of C/C++. Students of data
structure can take as opportunity to get it solved by non-recursive functions with the
help of the teacher.
Algorithm ( Recursive)
1. FUNCTION HANOI(DISKS, LEFT, RIGHT, MIDDLE)
2. IF DISKS > 0 THEN
2.1 HANOI(DISKS-1, LEFT, MIDDLE, RIGHT)
2.2 PRINT MOVE DISK , DISKS, FROM , LEFT, TO , RIGHT
2.3 HANOI(DISKS-1, MIDDLE, RIGHT, LEFT)
3 ENDIF
Process Logic:
In each recursive step disk number is changed and string values for LEFT, RIGHT
and MIDDLE are changed. Internally this function will use stack to keep the statements
in it in LIFO manner.
Page:14 */
The total distance traveled by vehicle in 't' seconds is given by distance=ut+1/2at 2 where
'u' and 'a' are the initial velocity (m/sec) and acclelaration (m/sec 2). Write C program to
find the distance traveled at regular intervals of time given the values of 'u' and 'a'. The
program should provide the flexibility to the user to select his own time intervals and
repeat the calculations for different values of 'u' and 'a'.
Algorithm:
1
2
3
4
5
6
7
8
9
10
START
T=3
(DEFAULT TIME INTERVAL IS 3 SECOND)
INPUT U,A
PRINT DO U WISH TO ENTER TIME ENTERVAL :
READ CH
IF CH = Y THEN
6.1 READ T
ENDIF
FOR INTERVALS = T TO 50 STEP T
8.1 DISTANCE=U*INTERVALS+0.5*A*INTERVALS*INTERVALS;
8.2 PRINT "DISTANCE TRAVELED , DISTANCE, METER IN , T SECONDS
NEXT INTERVAL
END.
CODE:
/*week4a.c*/
#include <stdio.h>
main()
{
float a,u,distance;
int intervals,t=3,x; /* default time interval is 3 seconds */
char ch;
printf("Enter velocity (m/sec) : ");
scanf("%f",&u);
printf("Enter acceleration (m/sec^2) : ");
scanf("%f",&a);
fflush(stdin);
printf("Do you want to give time intervals? : ");
ch=getchar();
Page:15 */
START
INPUT OPERAND1, OPERAND2
INPUT OPERATOR
DO CASE
CASE OPERATOR = +
5.1 RESULT = OPERAND1+OPERAND2
CASE OPERATOR = -
6.1 RESULT = OPERAND1-OPERAND2
CASE OPERATOR = *
7.1 RESULT = OPERAND1*OPERAND2
CASE OPERATOR = /
8.1 RESULT = OPERAND1/OPERAND2
CASE OPERATOR = %
9.1 RESULT = OPERAND1%OPERAND2
OTHERWISE
10.1
RESULT=NULL
ENDCASE
IF RESULT <> NULL THEN
12.1
PRINT RESULT IS, RESULT
ELSE
13.1
PRINT WRONG OPERATOR
ENDIF
END.
(MOD)
Page:17 */
/*Week4b.c */
#include <stdio.h>
main()
{
int operand1, operand2,result;
char operator;
clrscr();
printf("Enter two operands (numbers) : ");
scanf("%d%d",&operand1, &operand2);
fflush(stdin);
printf("Enter operator (+, -, /, * or % ) : ");
scanf("%c",&operator);
switch (operator)
{
case '+' : result
break;
case '-' : result
break;
case '/' : result
break;
case '*' : result
break;
case '%' : result
break;
default : result
}
= operand1 + operand2;
= operand1 - operand2;
= operand1 / operand2;
= operand1 * operand2;
= operand1 % operand2;
= NULL;
Page:19 */
Page:20 */
void addmat(a,b)
int a[3][3], b[3][3];
{
int c[3][3],row,col;
for(row=0;row<3;row++)
for(col=0;col<3;col++)
c[row][col] = a[row][col] + b[row][col];
puts("i. After addition:");
for(row=0;row<3;row++)
{
for(col=0;col<3;col++)
printf("%5d",c[row][col]);
printf("\n");
}
}
void mulmat(a,b)
int a[3][3], b[3][3];
{
int c[3][3],x,y,z;
for(x=0;x<3;x++)
for(y=0;y<3;y++)
{
c[x][y]=0;
for(z=0;z<3;z++)
c[x][y]=c[x][y]+a[x][z]*b[z][y];
}
puts("ii. After Multiplication:");
for(x=0;x<3;x++)
{
for(y=0;y<3;y++)
printf("%5d",c[x][y]);
printf("\n");
}
}
main()
{
int a[3][3], b[3][3], x,y;
printf("Enter data for matrix a:\n");
for(x=0;x<3;x++)
for(y=0;y<3;y++)
{
printf("Row %d Col %d : ", x+1, y+1); scanf("%d",&a[x][y]);
}
printf("Enter data for matrix b:\n");
Page:22 */
START
INPUT STR, POS, NCHARS
LAST=POS+NCHARS-1
DO WHILE STR[POS] != NULL
4.1. STR[POS]=STR[LAST]
4.2
POS=POS+1
4.3
LAST=LAST+1
5 ENDWHILE
6 PRINT STR
7 END.
Page:23 */
break;
Page:26 */
Write a C program that displays the position or index in the string S where the string t
begins, or -1 if S does not contain T.
Definition: Searching a sub-string t in string s
Process Logic:
Start comparing character by character of sub-string t with s from beginning to length of s-length
of t.
Comparison will terminate if any mismatch is there.
Comparison will end up to length of t
If number of compared characters (without mismatch) is equal to length of t then t is found at
position.
This algorithm is also known as Brute-Force Algorithm
Algorithm:
1
2
3
4
5
START
INPUT S, T
L1=LENGTH(S)
L2=LENGTH(T)
FOR I = 1 TO L1-L2
5.1 J=1
5.2 K=I
5.3 DO WHILE S[K] = T[J] AND J < L2
5.3.1 K=K+1
5.3.2 J=J+1
5.4 ENDWHILE
5.5 IF J = L2 THEN
5.5.1 FOUND=TRUE
5.6 ELSE
5.6.1 FOUND=FALSE
5.7 ENDIF
6 NEXT I
7 END.
Page:27 */
Page:28 */
*/
#include <stdio.h>
main()
{
char text[300];
int lines,words,nchars;
clrscr();
printf("\t\tEnter Text (^z to end):\n");
lines=words=nchars=0;
while((text[nchars]=getchar())!=EOF)
{
if(text[nchars]==' ' && text[nchars-1] != ' ')
if(text[nchars]=='\n') { lines++; words++; }
nchars++;
}
printf("\nNumber of lines = %d\n",lines+1);
printf("Number of words = %d\n",words+1);
printf("Number of characters = %d\n",nchars-1);
words++;
getch();
return (0);
}
Output:
Enter Text (^z to end):
There is a question in our lab exercise of CP lab i.e. to solve
Tower of Hanoi problem by non-recursive program.
Generally this problem is solved by recursive function only.
I am not getting the complete solution anywhere to solve it
non-recursively.
Can you solve Tower of Hanoi problem by looping statements
only (non-recursively)^Z
Number of lines = 7
Number of words = 55
Number of characters = 331
Page:29 */
1
A+B
A2+2AB+B2
A3+3A2B+3AB 2+B3
1
1
1
1
1
2
3
1
3
1
2
1
3
4
6
4
1
5 10 10
5
1
1
6 15 20 15
6
1
1
7 21 35 35 21
7
1
1
8 28 56 70 56 28
8
1
1
Page:30 */
Page:31 */
Page:32 */
Write a C program to read two numbers, x & n, and then compute the sum of geometric
progression:
1+x+x^2+x^3+......+x^n
For example: if n is 3 and x is 5, then program computes 1+5+25+125
Print x, n and the sum.
Perform error checking. For example, the formula does not make the sense for negative
exponents-if n is less than 0. Have your program print an error message if n<0, then go
back and read in the next pair of numbers of without computing the sum. are any value
of x also illegal? if so, test for them too.
ALGORITHM:
1
2
3
4
5
START
REPEAT:
SUM=1
INPUT X,N
IF N < 0 THEN
5.1 PRINT NEGATIVE EXPONENT!!!
5.2 GOTO REPEAT
6 ELSE
6.1 IF X = 0 THEN
6.1.1 PRINT ILLEGAL VALUE OF X
6.1.2 GOTO REPEAT
6.2 ELSE
6.2.1 FOR I = 1 TO N
6.2.2 SUM=SUM+POWER(X,I)
6.2.3 NEXT I
6.3 ENDIF
7 ENDIF
8 END.
Page:33 */
Page:34 */
5
6
7
8
9
START
INPUT NUMBIN (AS STRING)
LEN=LENGTH(NUMBIN)
DO WHILE NUMBIN[X] <> 1
(SCANNING FROM RIGHT AND SEARCHING 1)
4.1 IF NUMBIN[X] <> 0 AND NUMBIN[X] <> 1 THEN
4.1.1 PRINT NOT A BINARY NUMBER
4.1.2 EXIT
4.2 ENDIF
4.3 X=X-1
ENDWHILE
DO WHILE X >=1
6.1 IF NUMBIN[X] = 0 THEN
6.1.1 NUMBIN[X]=1
6.2 ELSE
6.2.1 NUMBIN[X]=0
6.3 ENDIF
6.4 X=X-1
ENDWHILE
PRINT 2S COMPLIMENT IS , NUMBIN
END.
Page:35 */
#include <stdio.h>
#include <conio.h>
#include <string.h>
main()
{
char numbin[50];
int len,x,y;
clrscr();
printf("Enter Binary digits : ");
scanf("%s",numbin);
len=strlen(numbin);
x=len-1;
while(numbin[x]!='1') /* scanning from right and reaching to 1 */
{
if(numbin[x] !='0' && numbin[x]!='1')
{
puts("Sorry, not binary number");
return (1);
}
x--;
}
x=x-1;
Page:36 */
START
INPUT ROMAN
(AS STRING)
SIZE=LENGTH(ROMAN);
FOR I = 1 TO SIZE
4.1 C=ROMAN[I]
4.2 F=ROMAN[I+1]
4.3 IF C AND NOT F THEN
4.3.1 RETURN TRUE
4.4 ENDIF
4.5 IF C='I' AND F='I' AND ROMAN[I-1]='I' AND ROMAN[I-2]='I' THEN
4.5.1 RETURN FALSE
4.6 ENDIF
4.7 IF C='X' AND F='X' AND ROMAN[I-1]='X' AND ROMAN[I-2]='X'
4.7.1 RETURN FALSE
4.8 ENDIF
4.9 IF(C='C' AND F='C' AND ROMAN[I-1]='C' AND ROMAN[I-2]='C'
4.9.1 RETURN FALSE
4.10 ENDIF
4.11 IF C='V' AND F='V' THEN
4.11.1 RETURN FALSE
4.12 ENDIF
4.13 IF C='L' AND F='L' THEN
4.13.1 RETURN FALSE
4.14 ENDIF
4.15 IF C='D' AND F='D' THEN
4.15.1 RETURN FALSE
4.16 ENDIF
4.17 IF C='I' AND (F <>'I' AND F<>'V' AND F<>'X' )THEN
4.17.1 RETURN FALSE
4.18 ENDIF
4.19 IF C='X' AND (F<>'I' AND F<>'V' AND F<>'X' AND F<>'L' AND F<>'C') THEN
4.19.1 RETURN FASLE
4.20 ENDIF
4.21 IF C='C' AND (F<>'I' AND F<>'V' AND F<>'X' AND F<>'L' AND F<>'C'
AND F<>'D' AND F<>'M') THEN
4.21.1 RETURN FA
4.22 ENDIF
5 NEXT I
6 RETURN TRUE
7 END.
START
INPUT ROMAN (AS STRING)
VALUE = 0
SIZE=LENGTH(ROMAN);
FOR I=1 TO SIZE
5.1 T=ROMAN[I];
5.2 F=ROMAN[I+1];
5.3 DO CASE (T)
5.3.1 CASE 'M':VALUE=VALUE+1000
5.3.2 CASE 'D':VALUE=VALUE+500
5.3.3 CASE 'C':
5.3.3.1 IF F='M' OR F='D' THEN
5.3.3.1.1 VALUE=VALUE-100
5.3.3.2 ELSE
5.3.3.2.1 VALUE=VALUE+100
5.3.3.3 ENDIF
5.3.4 CASE 'L':VALUE=VALUE+50
5.3.5 CASE 'X':
5.3.5.1 IF F='C' OR F='L' THEN
5.3.5.1.1 VALUE=VALUE-10;
5.3.5.2 ELSE
5.3.5.2.1 VALUE=VALUE+10;
5.3.5.3 ENDIF
5.3.6 CASE 'V':VALUE=VALUE+5
5.3.7 CASE 'I':
5.3.7.1 IF F='X' OR F='V' THEN
5.3.7.1.1 VALUE=VALUE-1;
5.3.7.2 ELSE
5.3.7.2.1 VALUE=VALUE-1;
5.3.7.3 ENDIF
5.4 ENDCASE
6 NEXT I
7 RETURN VALUE
8 END.
Page:37 */
Page:38 */
/* Week10b.c */
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
int checkroman(char roman[])
{
int i,size=strlen(roman);
char c,f;
for(i=0;i<size;i++)
{
c=roman[i];
f=roman[i+1];
if(c && !f) return (1);
if(c=='I' && f=='I' && roman[i-1]=='I' && roman[i-2]=='I')
return NULL;
if(c=='X' && f=='X' && roman[i-1]=='X' && roman[i-2]=='X')
return NULL;
if(c=='C' && f=='C' && roman[i-1]=='C' && roman[i-2]=='C')
return NULL;
if(c=='V' && f=='V') return NULL;
if(c=='L' && f=='L') return NULL;
if(c=='D' && f=='D') return NULL;
if(c=='I' && (f!='I' && f!='V' && f!='X')) return NULL;
if(c=='X' && (f!='I' && f!='V' && f!='X' && f!='L' && f!='C'))
return NULL;
if(c=='C' && (f!='I' && f!='V' && f!='X' && f!='L' && f!='C'
&& f!='D' && f!='M')) return NULL;
}
return (1);
}
int romantodecimal(char roman[])
{
int i,value = 0,size=strlen(roman);
char t,f;
for( i=0; i < size ; i++)
{
t=roman[i];
f=roman[i+1];
switch(t)
{
case 'M':value+=1000; break;
case 'D':value+=500; break;
case 'C':
if(f=='M'|| f=='D')
value-=100;
else
value+=100;
break;
Page:39 */
Page:41 */
Page:42 */
/* Week12a.c
Write a C program which copies one file to another */
#include <stdio.h>
#include <conio.h>
main()
{
FILE *fp1, *fp2;
char sfname[67], tfname[67],c;
clrscr();
printf("Enter source file : ");
scanf("%s",sfname);
printf("Enter target file : ");
scanf("%s",tfname);
fp1=fopen(sfname,"r");
if(!fp1)
{ printf("%s file does not exist.\n",sfname);
return (1);
}
fp2=fopen(tfname,"w");
while(!feof(fp1))
{
c=getc(fp1);
putc(c,fp2);
}
printf(File %s successfully copied to %s\n,sfname,tfname);
fclose(fp1);
fclose(fp2);
getch();
return (0);
}
Output:
Enter source file : week1a.c
Enter target file : itlab1.c
File week1a.c successfully copied to itlab1.c
/* displaying contents */
while(p<n)
/* reading first n characters */
{
str[p]=getc(fp);
p++;
}
str[p]='\0';
rewind(fp);
Page:44 */
p=0;
while(p<n)
/* writing in file */
{
c=str[p];
putc(c,fp);
p++;
}
rewind(fp);
5.
6.
7.
8.
START
INPUT ARRAY LIST
(WITH N ELEMENTS)
FOR I = 1 TO N-1
FOR J = 1 TO N-I
I. IF LIST[J] > LIST[J+1] THEN
1. SWAP(LIST[J],LIST[J+1])
II. ENDIF
NEXT J
NEXT I
PRINT SORTED ARRAY LIST
END
/* Week20-i.c */
#include <stdio.h>
#define MAX 10
void swapList(int *m,int *n)
{
int temp;
temp = *m;
*m = *n;
*n = temp;
}
Page:46 */
65
66
/*week20ii.c
Quick sort */
#include<stdio.h>
#include<conio.h>
int n;
void main()
{
long int a[100];
int i;
void qsort(long int [], int, int);
clrscr();
printf("\nEnter number of elements : ");
scanf("%d",&n);
printf("\nEnter elements :\n");
for(i=0;i<n;i++)
{
printf("Element %d ",i+1);
scanf("%ld",&a[i]);
}
qsort(a,0,n-1);
printf("\nNow quick sorted vector:\n");
for(i=0;i<n;i++)
printf(" %ld",a[i]);
printf("\n");
getch();
}
void qsort(long int a1[], int left, int right)
{
long int p,temp;
int i,j,k;
char ok;
if(right > left)
{
i=left;
j=right;
p=a1[left];
766
444
Page:47 */
34
766
1234
Page:48 */
{ --j; }
if (j < i)
ok = 't';
else
{
temp=a1[i];
a1[i] = a1[j];
a1[j] = temp;
}
} /*while*/
temp = a1[left];
a1[left] = a1[j];
a1[j] = temp;
qsort(a1,left,j-1);
qsort(a1,i,right);
}
}
Output:
Enter number of elements : 10
Enter elements :
Element 1 44
Element 2 65
Element 3 7
Element 4 457
Element 5 56
Element 6 8
Element 7 54
Element 8 24
Element 9 65
Element 10 33
Now quick sorted vector:
7 8 24 33 44 54 56 65 65 457
/Week21-I.c
Write C programs that implement the following sorting methods to sort
a given list of integers in ascending order:
: 10
444
55
66
77
82
34
5677
674
85
Page:49 */
777
888
890
900
Page:50 */
Page:51 */
Page:52 */
/* Week21-i.c
Write C programs that implement the following sorting methods to sort
a given list of integers in ascending order: i) Bubble sort */
99
111
/*Week13.c
linked list*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct LIST {
int data;
struct LIST *next;
};
typedef struct LIST node;
node *p;
void create(int num)
{ node *q, *t;
if(p==NULL)
{ p=(node *) malloc(sizeof(node));
p->data=num; p->next = NULL;
}
else
q=p;
while(q->next!=NULL) q=q->next;
t=(node *) malloc(sizeof(node));
t->data=num;
t->next = NULL;
q->next = t;
222
333
Page:53 */
Page:54 */
to create/insert/delete/exit : c
to create/insert/delete/exit : c
to create/insert/delete/exit : c
to create/insert/delete/exit : c
(
(
(
(
(
,
,
,
,
,
11 ) ;
2 ) ;
14 ) ;
17 ) ;
99 ) ;
clrscr( ) ;
d_display ( p ) ;
Page:56 */
Page:58 */
Page:59 */
Page:60 */
Page:61 */
Page:62 */
case 'o':
case 'O':
item = pop(stack);
if(status)
{
printf("\nThe popped item is %d \nAfter popping :",item);
display(stack);
}
else
printf("\nStack underflow on pop.\n");
break;
case 'q':
case 'Q':
quit = 1;
}
}while (!quit);
printf("\n Thank You \n");
getch();
}
Output:
Push/Pop/Quit (P/O/Q)
Enter choice : p
Enter choice : p
Enter choice : p
Enter choice : o
Page:63 */
Page:64 */
(
(
(
(
(
(
(
(
(
(
(
arr,
arr,
arr,
arr,
arr,
arr,
arr,
arr,
arr,
arr,
arr,
Page:65 */
= %d\n,item);
}
/* removes an element from the queue */
int delq ( int *arr, int *pfront, int *prear )
{
int data ;
if ( *pfront == -1 )
{
printf ( "\nQueue is Empty." ) ;
return NULL ;
}
data = arr[*pfront] ;
arr[*pfront] = 0 ;
if ( *pfront == *prear )
*pfront = *prear = -1 ;
else
( *pfront )++ ;
return
data ;
}
Output:
Inserted
Inserted
Inserted
Inserted
Inserted
Inserted
Inserted
Inserted
Inserted
Inserted
item
item
item
item
item
item
item
item
item
item
=
=
=
=
=
=
=
=
=
=
23
9
11
-10
25
16
17
22
19
30
Queue is full.
Item deleted: 23
Item deleted: 9
Item deleted: 11
/* week17-i.c: Program to convert an Infix form to Postfix form */
Page:66 */
Page:67 */
Page:69 */
*( p -> t ) = '\0' ;
}
/* returns the priority
int priority ( char c )
{
if ( c == '$' )
return 3 ;
if ( c == '*' || c
return 2 ;
else
{
if ( c == '+'
return 1
else
return 0
}
}
of an operator */
== '/' || c == '%' )
|| c == '-' )
;
;
abc*d/+e-
<stdio.h>
<conio.h>
<stdlib.h>
<math.h>
<ctype.h>
#define MAX 50
struct postfix
{
Page:70 */
Page:71 */
case '-' :
n3 = n2 - n1 ;
break ;
case '/' :
n3 = n2 / n1 ;
break ;
case '*' :
n3 = n2 * n1 ;
break ;
case '%' :
n3 = n2 % n1 ;
break ;
case '^' :
n3 = pow ( n2 , n1 ) ;
break ;
default :
printf ( "Unknown operator" ) ;
exit ( 1 ) ;
}
push ( p, n3 ) ;
}
p -> s++ ;
}
}
/* displays
void show (
{
p.nn =
printf
}
the result */
struct postfix p )
pop ( &p ) ;
( "Result is: %d", p.nn ) ;
Output:
Enter postfix expression to be evaluated: 5 5 5 5 * + +
Result is: 35
Page:72 */
Page:73 */
Page:74 */
Page:75 */
Page:77 */
Page:78 */
Pre-order traversal:
55 44 33 50 66 60 70
In-order traversal:
33 44 50 55 60 66 70
Post-order traversal:
33 50 44 60 70 66 55
Select choice : 1-insert 2-delete 3-traverse 4-exit:4
Okay - Bye
/* week22.c Computer program to implement the Lagrange interpolation */
#include <stdio.h>
#include <conio.h>
#define MAXPOINTS 21
double lagrpol(double x[], double y[], int n, double xbar);
main()
{
double x[MAXPOINTS], y[MAXPOINTS] ;
int n,i;
double xbar,ybar;
clrscr();
puts("LAGRANGE INTERPOLATION POLYNOMIAL");
printf("Enter degree of the polynomial: "); scanf("%d",&n);
printf("\nEnter data points:\n");
for (i=0; i<=n; i++)
{
printf("x[%d]=",i); scanf("%lf",&x[i]);
printf("y[%d]=",i); scanf("%lf",&y[i]);
}
printf("\nEnter xbar: ");
scanf("%lf",&xbar);
ybar=lagrpol(x,y,n,xbar);
printf("\nThe value of the polynomial at x = %lf is %lf\n"
,xbar,ybar);
getch();
return (0);
}
double lagrpol(double x[], double f[], int n, double xbar)
{
int i,j;
/*
#include <stdio.h>
#include <conio.h>
#define N 9
main()
{
Page:79 */
/*
:
:
:
:
:
:
:
:
:
1
2
3
4
5
6
7
8
9
5.5
7.0
9.6
11.5
12.6
14.4
17.6
19.5
20.5
#include <stdio.h>
#include <conio.h>
float f(float x)
{
Page:80 */
Page:81 */