R23 CP LAB MANUAL
R23 CP LAB MANUAL
OF
ENGINEERING AND TECHNOLOGY
NH5,BYPASS ROAD,GUDUR.
(AUTONOMOUS)
DEPARTMENT
OF
COMPUTER SCIENCE AND ENGINEERING
LAB MANUAL
OF
COMPUTER PROGRAMMING
23CS102
AUDISANKARA COLLEGE
OF
ENGINEERING AND TECHNOLOGY
NH5,BYPASS ROAD,GUDUR.
(AUTONOMOUS)
DEPARTMENT
OF
COMPUTER SCIENCE AND ENGINEERING
COMPUTER PROGRAMMING
(23CS102)
LAB MANUAL
I B.Tech. I Sem P C
3 1.5
Computer Programming Lab (23CS102)
(Common to All Branches of Engineering)
Course Outcomes:
At the end of the course, the student will be able to
CO1: Read, Understand and Trace the execution of programs written in C language.
CO2: Select the right control structure for solving the problem.
CO3: Develop C program which utilize memory efficiently using programming constructs
like pointers.
CO4: Develop, Debug and Execute programs to demonstrate the applications of arrays, functions,
basic concepts of pointer in C.
CO5: Develop C programs to handle File handling operations.
List of Experiments/Tasks
***
***
Description
DOS stands for Disk Operating System. Some of the important DOS commands are: DIR,
DIR/P, DIR/P/W, MD, CD, COPY CON, TYPE, COPY, DEL, RD etc.,
DIR: DIR command is used to display the directory contents of the existing drive.
Syntax: DIR
Example: DIR
Syntax: DIR / P
Example: DIR / P
DIR/P/W: DIR/P command is used to display the directory contents of the existing drive
page and width wise.
Syntax: DIR /P/W
Example: DIR /P/W
COPY CON: COPY CON command is used to create a file with the specified name
for storing data.
Syntax: COPY CON FileName
Example: COPY CON cse
After typing data into the specified file press Ctrl + Z for save and exit from the file.
DEL: DEL command is used to delete the specified file from the current directory.
Syntax: DEL FileName
Example: DEL ece
Algorithm:
Step 1: START
Step 2: INPUT two values as X and Y
Step 3: WRITE X and Y Values
Step 4: Temp ← X
X←Y
Y ← Temp
Step 5: WRITE X and Y Values
Step 6: STOP
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int X,Y,Temp;
clrscr();
printf("\n Enter Two Values =");
scanf("%d%d",&X,&Y);
printf("\n Before Swapping Values Are = %d %d",X,Y);
Temp=X;
X=Y;
Y=Temp;
printf("\n After Swapping Values Are = %d %d",X,Y);
}
Result:
Algorithm:
Step 1: START
Step 2: INPUT three values as X,Y and Z
Step 3: Sum ← X + Y + Z
Step 4: Avg ← Sum / 3
Step 5: WRITE Sum , Avg Values
Step 6: STOP
Flowchart:
START
INPUT X, Y and Z
Sum ← X + Y + Z
Avg ← Sum / 3
STOP
Algorithm:
Step 1: START
Step 2: INPUT Fahrenheit value as F
Step 3: C ← (F – 32 ) * (5 / 9 )
Step 4: WRITE C Value
Step 5: INPUT Celsius value as C
Step 6: F ← ( 9 / 5 ) * C + 32
Step 7: WRITE F Value
Step 6: STOP
Flowchart:
START
INPUT F
C ← (F – 32 ) * (5 / 9 )
WRITE C
INPUT C
F ← ( 9 / 5 ) * C + 32
WRITE F
STOP
Algorithm:
Step 1: START
Step 2: INPUT three values as P, T and R
Step 3: SI ← ( P * T * R ) / 100
Step 4: WRITE SI Value
Step 5: STOP
Flowchart:
START
INPUT P, T and R
SI ← ( P * T * R ) / 100
WRITE SI
STOP
Algorithm:
Step 1: START
Step 2: INPUT Velocity as V, Time as T
Step 3: Dis ← V * T
Step 4: WRITE Dis Value
Step 5: STOP
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
float Dis,V,T;
clrscr();
printf("\n Enter Velocity of Vehicle =");
scanf("%f",&V);
printf("\n Enter Time Travelled =");
scanf("%f",&T);
Dis=V*T;
printf("\n Distance Travelled =%.2f KM",Dis);
}
Result
Algorithm:
Step 1: START
Step 2: INPUT A,B,C,D,E,F and G Values
Step 3: R1 ← A + B * C + ( D * E ) + F * G
WRITE R1 value
Step 4: R2 ← A / B * C – B + A * D / 3
WRITE R2 value
Step 5: R3 ← A++ + B - --A
WRITE R3 value
Step 6: INPUT i value
Step 7: J ← ( i++ ) + ( ++i )
WRITE J value
Step 8: STOP
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int A,B,C,D,E,F,G,i,J,R1,R2,R3;
clrscr();
printf("\n Enter A,B,C,D,E,F and G Values =");
scanf("%d%d%d%d%d%d%d",&A,&B,&C,&D,&E,&F,&G);
R1=A+B*C+(D*E)+F*G;
printf("\n Expression Result 1 = %d",R1);
R2=A/B*C-B+A*D/3;
printf("\n Expression Result 2 = %d",R2);
R3=A+++B---A;
printf("\n Expression Result 3 = %d",R3);
Expression Result 1 = 28
Expression Result 2 = -3
Expression Result 3 = 4
Enter i Value = 7
Expression Result 4 = 14
Algorithm:
Step 1: START
Step 2: INPUT N value
Step 3: b ← 0.0001
Step 4: REPEAT FOR a ← 0 TO N-1 DO STEPS BY b
IF a*a > N THEN
a←a–b
BREAK
ENDIF
ENDREPEAT
Step 5: WRITE a value
Step 6: STOP
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
double a,b,N;
clrscr();
printf("\n Enter A Value = ");
scanf("%lf",&N);
b=0.0001;
for(a=0;a<N;a=a+b)
{
if(a*a > N)
{
a=a-b;
break;
}
}
printf("\n Square Root Value = %.2lf",a);
}
Result
Enter A Value = 25
Algorithm:
Step 1: START
Step 2: INPUT three values as X, Y and Z
Step 3: Max ← ( X > Y && X > Z ) ? X : ( Y > Z ? Y : Z )
Step 4: WRITE Max value
Step 5: STOP
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int X,Y,Z,Max;
clrscr();
printf("\n Enter Three Numbers =");
scanf("%d%d%d",&X,&Y,&Z);
Max=(X>Y&&X>Z)?X:(Y>Z?Y:Z);
printf("\n Maximum Value = %d",Max);
}
Result
Maximum Value = 45
Algorithm:
Step 1: START
Step 2: INPUT five subject marks as S1, S2, S3, S4 and S5
Step 3: SUM ← S1 + S2 + S3 + S4 + S5
AVG ← (float) (SUM / 5)
Step 4: WRITE SUM and AVG values
Step 5: STOP
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int S1,S2,S3,S4,S5,SUM;
float AVG;
clrscr();
printf("\n Enter 5 Subject Marks =");
scanf("%d%d%d%d%d",&S1,&S2,&S3,&S4,&S5);
SUM=S1+S2+S3+S4+S5;
AVG=(float)(SUM/5);
printf("\n Total Marks = %d",SUM);
printf("\n Average Marks = %.2f",AVG);
}
Result
Algorithm:
Step 1: START
Step 2: INPUT four values as P, Q, R and S
Step 3: Max ← P
Min ← P
Step 4: IF Q > Max THEN
Max ← Q
ELSE
IF Q < Min THEN
Min ← Q
ENDIF
ENDIF
Step 5: IF R > Max THEN
Max ← R
ELSE
IF R < Min THEN
Min ← R
ENDIF
ENDIF
Step 6: IF S > Max THEN
Max ← S
ELSE
IF S < Min THEN
Min ← S
ENDIF
ENDIF
Step 7: WRITE Max and Min values
Step 8: STOP
#include<stdio.h>
#include<conio.h>
void main()
{
int P,Q,R,S,Max,Min;
clrscr();
printf("\n Enter Four Values =");
scanf("%d%d%d%d",&P,&Q,&R,&S);
Max=P;
Min=P;
if(Q>Max)
Max=Q;
else if(Q<Min)
Min=Q;
if(R>Max)
Max=R;
else if(R<Min)
Min=R;
if(S>Max)
Max=S;
else if(S<Min)
Min=S;
printf("\n Maximum Number = %d",Max);
printf("\n Minimum Number = %d",Min);
Result
Maximum Number = 92
Minimum Number = 75
Algorithm:
Step 1: START
Step 2: INPUT Number of Units as Units
Step 3: IF Units ≤ 50 THEN
Amount ← Units * 5.40
SCharge ← 30.00
ELSEIF Units > 50 AND Units ≤ 100 THEN
Amount ← Units * 7.65
SCharge ← 40.00
ELSEIF Units > 100 AND Units ≤ 300 THEN
Amount ← Units * 9.25
SCharge ← 75.00
ELSE
Amount ← Units * 10.65
SCharge ← 150.00
ENDIF
Step 4: Bill ← Amount + SCharge
WRITE Bill value
Step 5: STOP
#include<stdio.h>
#include<conio.h>
void main()
{
int Units;
float Amount,SCharge,Bill;
clrscr();
printf("\n Enter Number of Units Consumed =");
scanf("%d",&Units);
if(Units<=50)
{
Amount=Units*5.40;
SCharge=30.00;
}
else if(Units>50&&Units<=100)
{
Amount=Units*7.65;
SCharge=40.00;
}
else if(Units>100&&Units<=300)
{
Amount=Units*9.25;
SCharge=75.00;
}
else
{
Amount=Units*10.65;
SCharge=150.00;
}
Bill=Amount+SCharge;
printf("\n Bill Amount = %.2f Rs",Bill);
}
Result
Algorithm:
Step 1: START
Step 2: INPUT Two Values as X and Y
Step 3: REPEAT WHILE TRUE
INPUT Option as ch
SWITCH ch
CASE ' + ' THEN
Z←X+Y
WRITE Z value
BREAK
ENDCASE
CASE ' - ' THEN Z
← X - Y WRITE
Z value BREAK
ENDCASE
CASE ' * ' THEN
Z←X*Y
WRITE Z value
BREAK
ENDCASE
CASE ' / ' THEN Z
← X / Y WRITE
Z value BREAK
ENDCASE
CASE ' % ' THEN
Z ← X MOD Y
WRITE Z value
BREAK
ENDCASE
CASE ' ^ ' THEN
EXIT
ENDCASE
ENDSWITCH
ENDREPEAT
Step 4: STOP
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
int X,Y,Z;
clrscr();
printf("\n Enter Two Values =");
scanf("%d%d",&X,&Y);
while(1)
{
printf("\n +: ADDITION\n -: SUBTRACTION\n");
printf(" *: MULTIPLICATION\n /: DIVISION\n");
printf(" %: REMAINDER\n ^: EXIT\n");
printf("\n Enter Your Option =");
scanf(" %c",&ch);
switch(ch)
{
case '+':Z=X+Y;
printf("\n Addition Result = %d",Z);
break;
case '-':Z=X-Y;
printf("\n Subtraction Result = %d",Z);
break;
case '*':Z=X*Y;
printf("\n Multiplication Result = %d",Z);
break;
case '/':Z=X/Y;
printf("\n Division Result = %d",Z);
break;
case '%':Z=X%Y;
printf("\n Remainder Result = %d",Z);
break;
case '^':exit();
}
}
}
+: ADDITION
-: SUBTRACTION
*: MULTIPLICATION
/: DIVISION
%: REMAINDER
^: EXIT\n");
Addition Result = 9
+: ADDITION
-: SUBTRACTION
*: MULTIPLICATION
/: DIVISION
%: REMAINDER
^: EXIT\n");
Multiplication Result = 14
Algorithm:
Step 1: START
Step 2: INPUT Year values as P
Step 3: IF (P MOD 4 = 0) AND ( ( P MOD 400 = 0 ) OR ( P MOD 100 ≠ 0 ) ) THEN
WRITE P as Leap Year
ELSE
WRITE P as Not a Leap Year
ENDIF
Step 4: STOP
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int P;
clrscr();
printf("\n Enter Year Value =");
scanf("%d",&P);
if((P%4==0)&&((P%400==0)||(P%100!=0)))
printf("\n %d Is A Leap Year",P);
else
printf("\n %d Is Not A Leap Year",P);
}
Result
Algorithm:
Step 1: START
Step 2: INPUT a value as N
Step 3: Fact ← 1
i←1
Step 4: REPEAT WHILE i ≤ N
Fact ← Fact * i
i←i+1
ENDREPEAT
Step 5: WRITE Fact value
Step 6: STOP
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int N,i,Fact;
clrscr();
printf("\n Enter A Number =");
scanf("%d",&N);
Fact=1; i=1;
while(i<=N)
{
Fact=Fact*i;
i=i+1;
}
printf("\n Factorial Value = %d",Fact);
}
Result
Enter A Number = 5
Algorithm:
Step 1: START
Step 2: INPUT N value
Step 3: Flag ← 0
Step 4: REPEAT FOR i ← 2 TO N-1 DO STEPS BY 1
IF N MOD i = 0 THEN
Flag ← 1
BREAK
ENDIF
ENDREPEAT
Step 5: IF Flag = 0 THEN
WRITE N as a Prime Number
ELSE
WRITE N as not a Prime Number
ENDIF
Step 6: STOP
Program:
void main()
{
int N,i,Flag;
clrscr();
printf("\n Enter A Number =");
scanf("%d",&N);
Flag=0;
for(i=2;i<N;i++)
{
if(N%i==0)
{
Flag=1;
break;
}
}
if(Flag==0)
printf("\n %d Is A Prime Number",N);
else
printf("\n %d Is Not A Prime Number",N);
}
Result
Enter A Number = 17
17 Is A Prime Number
Algorithm:
Step 1: START
Step 2: INPUT a value as N
Step 3: Temp ← N
Rev ← 0
Step 4: REPEAT WHILE N > 0
K ← N MOD 10
Rev ← Rev * 10 + K
N ← N / 10
ENDREPEAT
Step 5: IF Temp = Rev THEN
WRITE Temp as a Palindrome
ELSE
WRITE Temp as not a Palindrome
ENDREPEAT
Step 6: STOP
Program:
void main()
{
int N,K,Temp,Rev;
clrscr();
printf("\n Enter A Number =");
scanf("%d",&N);
Temp=N;
Rev=0;
while(N>0)
{
K=N%10;
Rev=Rev*10+K;
N=N/10;
}
if(Temp==Rev)
printf("\n %d Is A Palindrome",Temp);
else
printf("\n %d Is Not A Palindrome",Temp);
}
Result
Algorithm:
Step 1: START
Step 2: INPUT number of lines as N
Step 3: REPEAT FOR i ← 1 TO N DO STEPS BY 1
REPEAT FOR j ← 1 TO 20 DO STEPS BY 1
WRITE ‘ ‘
ENDREPEAT
REPEAT FOR k ← 1 TO i DO STEPS BY 1
WRITE i
ENDREPEAT
ENDREPEAT
Step 4: STOP
Program:
void main()
{
int N,i,j,k;
clrscr();
printf("\n Enter How Many Lines =");
scanf("%d",&N);
printf("\n Pyramid Format = \n\n");
for(i=1;i<=N;i++)
{
printf("\n");
for(j=1;j<=20-i;j++)
printf(" ");
for(k=1;k<=i;k++)
printf(" %d",i);
}
}
Result
Algorithm:
Step 1: START
Step 2: INPUT Number of values as N
Step 3: REPEAT FOR i ← 1 TO N DO STEPS BY 1
INPUT K[ i ] value
ENDREPEAT
Step 4: Max ← K[1]
Min ← K[1]
Step 5: REPEAT FOR i ← 2 TO N DO STEPS BY 1
IF K[ i ] > Max THEN
Max ← K[ i ]
ENDIF
IF K[ i ] < Min THEN
Min ← K[ i ]
ENDIF
ENDREPEAT
Step 6: WRITE Max and Min values
Step 7: STOP
#include<stdio.h>
#include<conio.h>
void main()
{
int K[50],Max,Min,i,N;
clrscr();
printf("\n Enter How Many Elements = ");
scanf("%d",&N);
printf("\n Enter %d Values = ",N);
for( i = 1 ; i < = N ; i++ )
scanf("%d",&K[i]);
Max = K[1];
Min = K[1];
for( i = 2 ; i < = N ; i++ )
{
if ( K[i] > Max )
Max = K[i];
if ( K[i] < Min )
Min = K[i];
}
printf("\n Maximum Value = %d",Max);
printf("\n Minimum Value = %d",Min);
}
Result
Enter 5 Values = 10 25 79 8 34
Maximum Value = 79
Minimum Value = 8
Algorithm:
Step 1: START
Step 2: INPUT Number of Elements as N
Step 3: REPEAT FOR i ← 1 TO N DO STEPS BY 1
INPUT K[ i ] value
ENDREPEAT
Step 4: INPUT Search element as Key
Step 5: Flag ← -1
Step 6: REPEAT FOR i ← 1 TO N DO STEPS BY 1
IF K[ i ] = Key THEN
Flag ← i
BREAK
ENDIF
ENDREPEAT
Step 7: IF Flag = -1)
WRITE ‘Search Key Found’
ELSE
WRITE ‘Search Key Not Found’
ENDFIF
Step 8: STOP
#include<stdio.h>
#include<conio.h>
void main()
{
int K[50],i,N,Key,Flag;
clrscr();
printf("\n Enter How Many Elements = ");
scanf("%d",&N);
printf("\n Enter %d Values = ",N);
for( i = 1 ; i < = N ; i++ )
scanf("%d",&K[i]);
printf("\n Enter An Element To Search = ");
scanf("%d",&Key);
Flag = -1 ;
for ( i = 1 ; i < = N ; i++ )
{
if ( K[i] = = Key )
{
Flag = i;
break;
}
}
if ( Flag = = -1 )
printf("\n Search Key Not Found");
else
printf("\n Search Key Found At Index Position = %d",Flag);
}
Result
Enter 5 Values = 10 25 79 18 34
Algorithm:
Step 1: START
Step 2: INPUT Number of Elements as N
Step 3: REPEAT FOR i ← 1 TO N DO STEPS BY 1
INPUT K[ i ] value
ENDREPEAT
Step 4: REPEAT FOR i ← 1 TO N DO STEPS BY 1
WRITE K[ i ] value
ENDREPEAT
Step 5: REPEAT FOR i ← 1, j ← N TO i < j DO STEPS BY 1 , -1
Temp ← K[ i ]
K[ i ] ← K[ j ]
K[ j ] ← Temp
ENDREPEAT
Step 6: REPEAT FOR i ← 1 TO N DO STEPS BY 1
WRITE K[ i ] value
ENDREPEAT
Step 7: STOP
#include<stdio.h>
#include<conio.h>
void main()
{
int K[50],Temp,N,i,j;
clrscr();
printf("\n Enter How Many Elements = ");
scanf("%d",&N);
printf("\n Enter %d Values = ",N);
for ( i = 1 ; i < = N ; i++ )
scanf("%d",&K[i]);
printf("\n Actual Array Elements = ");
for ( i = 1 ; i < = N ; i++ )
printf(" %d",K[i]);
for ( i = 1 , j = N ; i < j ; i++ , j-- )
{
Temp = K[i];
K[i] = K[j];
K[j] = Temp;
}
printf("\n Array Elements In Reverse Order = ");
for ( i = 1 ; i < = N ; i++ )
printf(" %d",K[i]);
}
Result
Enter 5 Values = 10 25 79 18 34
Algorithm:
Step 1: START
Step 2: INPUT Number of Digits as N
Step 3: REPEAT FOR i ← 0 TO N-1 DO STEPS BY 1
INPUT X[ i ] value
ENDREPEAT
Step 4: REPEAT FOR i ← N-1 TO 0 DO STEPS BY -1
k ← X[ i ]
j ← i
IF k = 0 THEN
Y[ j ] ← k
ELSE
Y[ j ] ← k
REPEAT FOR p ← j - 1 TO 0 DO STEPS BY -1
IF X[ p ] = 0 THEN
Y[ p ] ← 1
ELSE
Y[ p ] ← 0
ENDIF
ENDREPEAT
GOTO T
ENDIF
ENDREPEAT
Step 5: T: REPEAT FOR i ← 0 TO N-1 DO STEPS BY 1
WRITE Y[ i ] value
ENDREPEAT
Step 6: STOP
#include<stdio.h>
#include<conio.h>
void main()
{
int N,i,j,k,p,X[10],Y[10];
clrscr();
printf("\n Enter How Many Digits = ");
scanf("%d",&N);
printf("\n Enter %d Binary numbers (0 or 1) = ",N);
for ( i = 0 ; i < N ; i++ )
scanf("%d",&X[i]);
for ( i = N-1 ; i > = 0 ; i-- )
{
k = X[i];
j=i;
if ( k = = 0 )
Y[j] = k;
else
{
Y[j] = k;
for ( p = j-1 ; p > = 0 ; p-- )
{
if ( X[p] = = 0 )
Y[p] = 1;
else
Y[p] = 0;
}
goto T;
}
}
T: printf("\n 2'S COMPLEMENT OF GIVEN NUMBER IS = ");
for ( i = 0 ; i < N ; i++ )
printf(" %d",Y[i]);
}
Result
Algorithm:
Step 1: START
Step 2: INPUT Number of Elements as N
Step 3: REPEAT FOR i ← 0 TO N-1 DO STEPS BY 1
INPUT K[ i ] value
ENDREPEAT
Step 4: REPEAT FOR i ← 0 TO N-1 DO STEPS BY 1
WRITE K[ i ] value
ENDREPEAT
Step 5: REPEAT FOR i ← 0 TO N-2 DO STEPS BY 1
REPEAT FOR j ← i + 1 TO N-1 DO STEPS BY 1
IF K[ i ] = K[ j ] THEN
REPEAT FOR p ← j TO N-2 DO STEPS BY 1
K[ p ] ← K[ p + 1 ]
ENDREPEAT
N←N+1
ENDIF
ENDREPEAT
ENDREPEAT
Step 6: REPEAT FOR i ← 0 TO N-1 DO STEPS BY 1
WRITE K[ i ] value
ENDREPEAT
Step 7: STOP
#include<stdio.h>
#include<conio.h>
void main()
{
int K[50],i,j,p,N;
clrscr();
printf("\n Enter How Many Values = ");
scanf("%d",&N);
printf("\n Enter %d Elements = ",N);
for ( i = 0 ; i < N ; i++ )
scanf("%d",&K[i]);
printf("\n Actual Array Elements = \n");
for ( i = 0 ; i < N ; i++ )
printf(" %d",K[i]);
for ( i = 0 ; i < = N-2 ; i++ )
{
for ( j = i+1 ; j < = N-1 ; j++ )
{
if ( K[i] = = K[j] )
{
for ( p = j ; p < = N-2 ; p++ )
K[p] = K[p+1];
N = N-1;
}
}
}
printf("\n After Removing Duplicate Elements Array Elements = \n");
for ( i = 0 ; i < N ; i++ )
printf(" %d",K[i]);
}
Result
Enter 6 Elements = 10 12 10 25 12 10
Algorithm:
Step 1: START
Step 2: INPUT R1, C1, R2 and C2 values
Step 3: IF R1 = R2 AND C1 = C2 THEN
REPEAT FOR i ← 0 TO R1 - 1 DO STEPS BY 1
REPEAT FOR j ← 0 TO C1 - 1 DO STEPS BY 1
INPUT X[ i ][ j ] value
ENDREPEAT
ENDREPEAT
REPEAT FOR i ← 0 TO R2 - 1 DO STEPS BY 1
REPEAT FOR j ← 0 TO C2 - 1 DO STEPS BY 1
INPUT Y[ i ][ j ] value
ENDREPEAT
ENDREPEAT
REPEAT FOR i ← 0 TO R1 - 1 DO STEPS BY 1
REPEAT FOR j ← 0 TO C1 - 1 DO STEPS BY 1
Z[ i ][ j ] ← X[ i ][ j ] + Y[ i ][ j ]
ENDREPEAT
ENDREPEAT
REPEAT FOR i ← 0 TO R1 - 1 DO STEPS BY 1
REPEAT FOR j ← 0 TO C1 - 1 DO STEPS BY 1
WRITE Z[ i ][ j ] value
ENDREPEAT
ENDREPEAT
ELSE
WRITE ‘ Matrix Addition Not Possible ‘
ENDIF
Step 4: STOP
#include<stdio.h>
#include<conio.h>
void main()
{
int X[10][10],Y[10][10],Z[10][10],i,j,R1,C1,R2,C2;
clrscr();
printf("\n Enter Number of Rows of Matrix 1 =");
scanf("%d",&R1);
printf("\n Enter Number of Columns of Matrix 1 =");
scanf("%d",&C1);
printf("\n Enter Number of Rows of Matrix 2 =");
scanf("%d",&R2);
printf("\n Enter Number of Columns of Matrix 2 =");
scanf("%d",&C2);
if ( R1 = = R2 && C1 = = C2 )
{
printf("\n Enter Matrix 1 Elements =");
for ( i = 0 ; i < R1 ; i++ )
{
for ( j = 0 ; j < C1 ; j++ )
scanf("%d",&X[i][j]);
}
printf("\n Enter Matrix 2 Elements =");
for ( i = 0 ; i < R2 ; i++ )
{
for ( j = 0 ; j <C2 ; j++ )
scanf("%d",&Y[i][j]);
}
for ( i = 0 ; i < R1 ; i++ )
{
for ( j = 0 ; j < C1 ; j++ )
Z[i][j] = X[i][j] + Y[i][j];
}
printf("\n Addition Matrix Elements =\n");
for ( i = 0 ; i < R1 ; i++ )
{
printf("\n");
for ( j = 0 ; j <C1 ; j++ )
printf(" %d",Z[i][j]);
}
}
else
printf("\n Matrix Addition Not Possible");
}
Algorithm:
Step 1: START
Step 2: INPUT R1, C1, R2 and C2 values
Step 3: IF C1 = R2 THEN
REPEAT FOR i ← 0 TO R1 - 1 DO STEPS BY 1
REPEAT FOR j ← 0 TO C1 - 1 DO STEPS BY 1
INPUT X[ i ][ j ] value
ENDREPEAT
ENDREPEAT
REPEAT FOR i ← 0 TO R2 - 1 DO STEPS BY 1
REPEAT FOR j ← 0 TO C2 - 1 DO STEPS BY 1
INPUT Y[ i ][ j ] value
ENDREPEAT
ENDREPEAT
REPEAT FOR i ← 0 TO R1 - 1 DO STEPS BY 1
REPEAT FOR j ← 0 TO C2 - 1 DO STEPS BY 1
Z[ i ][ j ] ← 0
REPEAT FOR k ← 0 TO C1 - 1 DO STEPS BY 1
Z[ i ][ j ] ← Z[ i ][ j ] + X[ i ][ k ] * Y[ k ][ j ]
ENDREPEAT
ENDREPEAT
ENDREPEAT
REPEAT FOR i ← 0 TO R1 - 1 DO STEPS BY 1
REPEAT FOR j ← 0 TO C2 - 1 DO STEPS BY 1
WRITE Z[ i ][ j ] value
ENDREPEAT
ENDREPEAT
ELSE
WRITE ‘ Matrix Multiplication Not Possible ‘
ENDIF
Step 4: STOP
void main()
{
int X[10][10],Y[10][10],Z[10][10],i,j,k,R1,C1,R2,C2;
printf("\n Enter Number of Rows of Matrix 1 =");
scanf("%d",&R1);
printf("\n Enter Number of Columns of Matrix 1 =");
scanf("%d",&C1);
printf("\n Enter Number of Rows of Matrix 2 =");
scanf("%d",&R2);
printf("\n Enter Number of Columns of Matrix 2 =");
scanf("%d",&C2);
if ( C1 = = R2 )
{
printf("\n Enter Matrix 1 Elements =");
for ( i = 0 ; i < R1 ; i++ )
{
for ( j = 0 ; j < C1 ; j++ )
scanf("%d",&X[i][j]);
}
printf("\n Enter Matrix 2 Elements =");
for ( i = 0 ; i < R2 ; i++ )
{
for ( j = 0 ; j <C2 ; j++ )
scanf("%d",&Y[i][j]);
}
for ( i = 0 ; i < R1 ; i++ )
{
for ( j = 0 ; j < C2 ; j++ )
{
Z[i][j] = 0;
for ( k = 0 ; k < C1 ; k++ )
Z[i][j] = Z[i][j] + X[i][k] * Y[k][j];
}
}
printf("\n Multiplication Matrix Elements =\n");
for ( i = 0 ; i < R1 ; i++ )
{
printf("\n");
for ( j = 0 ; j < C2 ; j++ )
printf(" %d",Z[i][j]);
}
}
else
printf("\n Matrix Multiplication Not Possible");
}
Algorithm:
Step 1: START
Step 2: INPUT Number of elements as N
Step 3: REPEAT FOR i ← 1 TO N DO STEPS BY 1
INPUT K[ i ] value
ENDREPEAT
Step 4: REPEAT FOR i ← 1 TO N DO STEPS BY 1
WRITE K[ i ] value
ENDREPEAT
Step 5: REPEAT FOR i ← 1 TO N - 1 DO STEPS BY 1
j←1
REPEAT WHILE j ≤ N - i
IF K[ j ] > K[ j + 1 ] THEN
Temp ← K[ j ]
K[j] ← K[ j + 1 ]
K[ j + 1 ] ← Temp
ENDIF
j← j+1
ENDREPEAT
ENDREPEAT
Step 6: REPEAT FOR i ← 1 TO N DO STEPS BY 1
WRITE K[ i ] value
ENDREPEAT
Step 7: STOP
#include<stdio.h>
#include<conio.h>
void main()
{
int K[25],i,j,Temp,N;
clrscr();
printf("\n Ente How Many Elements = ");
scanf("%d",&N);
printf("\n Enter %d Values = ",N);
for ( i = 1 ; i < = N ; i++ )
scanf("%d",&K[i]);
printf("\n\n Before Sorting Array Elements = ");
for ( i = 1 ; i < = N ; i++ )
printf(" %d",K[i]);
for ( i = 1 ; i < N ; i++ )
{
j = 1;
while ( j < = N-i )
{
if ( K[j] > K[j+1] )
{
Temp = K[j];
K[j] = K[j+1];
K[j+1] = Temp;
}
j = j + 1;
}
}
printf("\n\n After Sorting Array Elements = ");
for ( i = 1 ; i < = N ; i++ )
printf(" %d",K[i]);
}
Result
Enter 5 Values = 10 25 12 72 38
Algorithm:
Step 1: START
Step 2: INPUT First String as S1 and Second String as S2
Step 3: REPEAT FOR i ← 0 TO S1[ i ] ≠ ‘\0’ DO STEPS BY 1
ENDREPEAT
Step 4: REPEAT FOR j ← 0 TO S2[ j ] ≠ ‘\0’ DO STEPS BY 1
S1 [ i + j ] ← S2 [ j ]
ENDREPEAT
Step 5: S1[ i + j ] ← ‘\0’
Step 6: WRITE S1 as result string
Step 7: STOP
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
char S1[50],S2[50];
int i,j;
clrscr();
printf("\n Enter First String =");
gets(S1);
printf("\n Enter Second String =");
gets(S2);
for ( i = 0 ; S1[i] ! = '\0' ; i++ ) ;
for ( j = 0 ; S2[j] ! = '\0' ; j++ )
S1[i+j] = S2[j];
S1[i+j] = '\0' ;
printf("\n Result String = %s",S1);
}
Result
Algorithm:
Step 1: START
Step 2: INPUT A String as S1
Step 3: WRITE S1
Step 4: CALL strrev(S1)
Step 5: WRITE S1
Step 6: STOP
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char S1[50];
clrscr();
printf("\n Enter A String =");
gets(S1);
printf("\n Original String = %s",S1);
strrev(S1);
printf("\n Reverse String = %s",S1);
}
Result
Algorithm:
Step 1: START
Step 2: INPUT A String as S1
Step 3: WRITE S1
Step 4: REPEAT FOR N ← 0 TO S1[ N ] ≠ 0 DO STEPS BY 1
ENDREPEAT
Step 5: REPEAT FOR i ← 0 , j ← N - 1 TO i < j DO STEPS BY 1 , -1
Temp ← S1 [ i ]
S1 [ i ] ← S1 [ j ]
S1 [ j ] ← Temp
ENDREPEAT
Step 6: WRITE S1
Step 7: STOP
Program:
void main()
{
char S1[50],Temp;
int i,j,N;
clrscr();
printf("\n Enter A String =");
gets(S1);
printf("\n Original String = %s",S1);
for ( N = 0 ; S1[N] ! = '\0' ; N++ ) ;
for ( i = 0 , j = N-1 ; i < j ; i++ , j-- )
{
Temp = S1[i];
S1[i] = S1[j];
S1[j] = Temp;
}
printf("\n Reverse String = %s",S1);
}
Result
Algorithm:
Step 1: START
Step 2: INPUT Number of values as N
Step 3: Allocate memory for a pointer variable K with malloc ( ) Function
Step 4: REPEAT FOR i ← 0 TO N-1 DO STEPS BY 1
INPUT K + i
ENDREPEAT
Step 5: Sum ← 0
Step 6: REPEAT FOR i ← 0 TO N-1 DO STEPS BY 1
Sum ← Sum + * ( K + i )
ENDREPEAT
Step 7: WRITE Sum value
Step 8: STOP
Program:
#include<stdio.h>
#include<alloc.h>
void main()
{
int *K , i , N , Sum ;
clrscr();
printf("\n Enter How Many Values = ");
scanf("%d", &N);
K = ( int * ) malloc ( N * sizeof ( int ) ) ;
printf("\n Enter %d Elements =",N);
for ( i = 0 ; i < N ; i++ )
scanf("%d" , K + i ) ;
Sum = 0 ;
for ( i = 0 ; i < N ; i++ )
Sum = Sum + * ( K + i ) ;
printf("\n Sum Result Value = %d" , Sum ) ;
}
Result
Enter 5 Elements = 10 35 26 42 89
Algorithm:
Define a structure student with the members RNO , S1 , S2 , S3 , TOTAL as integers, NAME
as a string and AVG as float.
Step 1: START
Step 2: INPUT number of students as N
Step 3: REPEAT FOR i ← 1 TO N DO STEPS BY 1
INPUT ST[ i ] . RNO , ST[ i ] . NAME
INPUT ST[ i ] . S1 , ST[ i ] . S2 , ST[ i ] . S3
ENDREPEAT
Step 4: REPEAT FOR i ← 1 TO N DO STEPS BY 1
ST[i].TOTAL ← ST[i].S1 + ST[i].S2 + ST[i].S3
ST[i].AVG ← (float) ( ST[i].TOTAL / 3 )
ENDREPEAT
Step 5: REPEAT FOR i ← 1 TO N DO STEPS BY 1
WRITE ST[i].RNO , ST[i].NAME , ST[i].TOTAL , ST[i].AVG
ENDREPEAT
Step 6: STOP
#include<stdio.h>
#include<conio.h>
struct student
{
int RNO , S1 , S2 , S3 , TOTAL ;
char NAME[50];
float AVG;
}ST[50];
void main()
{
int N , i ;
clrscr();
printf("\n Enter How Many Students = " ) ;
scanf("%d" , &N);
for ( i = 1 ; i <= N ; i++ )
{
printf("\n Enter Student %d Roll Number = " , i ) ;
scanf("%d" , &ST[i].RNO ) ;
fflush(stdin);
printf("\n Enter Name of the Student =");
gets(ST[i].NAME);
printf("\n Enter Marks in Three Subjects = " ) ;
scanf("%d%d%d", &ST[i].S1 , &ST[i].S2 , &ST[i].S3 ) ;
}
for ( i = 1 ; i < = N ; i++ )
{
ST[i].TOTAL = ST[i].S1 + ST[i].S2 + ST[i].S3 ;
ST[i].AVG = (float) ( ST[i].TOTAL / 3 ) ;
}
printf("\n TOTAL AND AVERAGE MARKS OF STUDENTS ARE =\n\n");
for ( i = 1 ; i < = N ; i++ )
{
printf("\n");
printf("%d\t%s\t%d\t%.2f",ST[i].RNO,ST[i].NAME,ST[i].TOTAL,ST[i].AVG);
}
}
Algorithm:
Step 1: START
Step 2: INPUT number of students as N
Step 3: REPEAT FOR i ← 0 TO N-1 DO STEPS BY 1
INPUT ST[ i ] . RNO , ST[ i ] . NAME
INPUT ST[ i ] . S1 , ST[ i ] . S2 , ST[ i ] . S3
ENDREPEAT
Step 4: REPEAT FOR i ← 0 TO N-1 DO STEPS BY 1
IF ST[ i ].S1 ≤ 40 OR ST[ i ].S2 ≤ 40 OR ST[ i ].S3 ≤ 40 THEN
WRITE ST[ i ].RNO , ST[ i ].NAME
ENDIF
ENDREPEAT
Step 5: STOP
Program:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
typedef struct
{
int RNO,S1,S2,S3;
char NAME[50];
}Student;
void main()
{
Student *ST;
int N,i;
clrscr();
printf("\n Enter How Many Students =");
scanf("%d",&N);
ST=(Student *)calloc(N,sizeof(Student));
for(i=0;i<N;i++)
{
printf("\n Enter Student %d Roll Number =",i);
scanf("%d",&ST[i].RNO);
fflush(stdin);
Result
111 Mahesh
Algorithm:
Step 1: START
Step 2: CALL strcpy( STName , argv[ 1 ] )
S1 ← atoi( argv[ 2 ] )
S2 ← atoi( argv[ 3 ] )
Total ← S1 + S2
Step 3: WRITE argc , STNAME , S1 , S2 , Total values
Step 4: STOP
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
Result
Number of Arguments = 4
Studetn Name = Mahesh
Subject 1 Marks = 70
Subject 2 Marks = 80
Total Marks = 150
Algorithm:
Step 1: START
Step 2: INPUT Number of Elements as N
Step 3: Allocate memory for a pointer Variable P with malloc ( ) function
Step 4: REPEAT FOR i ← 0 TO N-1 DO STEPS BY 1
INPUT P + i values
ENDREPEAT
Step 5: Re Allocate memory for a pointer Variable P with realloc ( ) function
Step 6: REPEAT FOR i ← 0 TO N+1 DO STEPS BY 1
INPUT P + i values
ENDREPEAT
Step 7: REPEAT FOR i ← 0 TO N+1 DO STEPS BY 1
WRITE * ( P + i ) values
ENDREPEAT
Step 8: STOP
Program:
#include<alloc.h>
void main()
{
int *P,N,i;
clrscr();
printf("\n Enter How Many Elements =");
scanf("%d",&N);
P=(int*)malloc(N*sizeof(int));
printf("\n Enter %d Values =",N);
for(i=0;i<N;i++) scanf("%d",P+i);
P=(int*)realloc(P,(N+2)*sizeof(int));
printf("\n Re Enter %d Values =",N+2);
for(i=0;i<N+2;i++)
scanf("%d",P+i);
printf("\n\n Array Elements =\n\n");
for(i=0;i<N+2;i++)
printf("%d ",*(P+i));
}
Result
Algorithm creation (): This procedure creates a single liked list with the
specified number of elements.
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
NODE *START=NULL,*END=NULL;
void create();
void display();
void main()
{
int ch;
clrscr();
while(1)
{
printf("\n 1:CREATION\n 2:DISPLAY\n 3:EXIT\n");
printf("\n Enter Your Choice =");
scanf("%d",&ch);
switch(ch)
{
case 1:create();
break;
case 2:display();
break;
case 3:exit();
}
}
}
void create()
{
NODE *NEW;
int K;
while(1)
{
printf("\n Enter An Element (STOP:-999) =");
scanf("%d",&K);
if(K==-999)
return;
1:CREATION
2:DISPLAY
3:EXIT
Enter An Element(Stop:-999) = 11
Enter An Element(Stop:-999) = 22
Enter An Element(Stop:-999) = 33
Enter An Element(Stop:-999) = -999
1:CREATION
2:DISPLAY
3:EXIT
Enter Your Choice = 2
Algorithm:
Step 1: START
Step 2: Define a structure Demo1 with the members a as character, b as integer, c as float and
d as double data members.
Define a union Demo2 with the members a as character, b as integer, c as float and d
as
double data members.
Step 3: WRITE memory size of structure and union variables.
Step 4: STOP
Program:
#include<stdio.h>
#include<conio.h>
struct Demo1
{
char a;
int b;
float c;
double d;
}ST;
union Demo2
{
char a;
int b;
float c;
double d;
}UP;
void main()
{
clrscr();
printf("\n Memory Size of Structure ST = %d Bytes",sizeof(ST));
printf("\n Memory Size of Union UP = %d Bytes",sizeof(UP));
}
Result
Algorithm:
Step 1: START
Step 2: K ← 25
WRITE K << 2
Step 3: K ← 78
WRITE K >> 1
Step 4: STOP
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int K;
clrscr();
K=25;
printf("\n Left Shift Result = %d",K<<2);
K=78;
printf("\n Right Shift Result = %d",K>>1);
}
Result
Algorithm:
Step 1: START
Step 2: Define as structure BOOK with BID as integer, BNAME as string and BPRICE as
float.
Step 3: Create two variables B1 and B2 for the Book
Step 4: INPUT B1 values
Step 5: B2 ← B1
Step 6: WRITE B1 , B2 values
Step 7: STOP
Program:
#include<stdio.h>
#include<conio.h>
struct BOOK
{
int BID;
char BNAME[50];
float BPRICE;
};
void main()
{
struct BOOK B1={121,"Let Us C",275.00},B2;
clrscr();
printf("\n Original Book Data = %d\t%s\t%.2f RS",B1.BID,B1.BNAME,B1.BPRICE);
B2=B1;
printf("\n Copied Book Data = %d\t%s\t%.2f RS",B2.BID,B2.BNAME,B2.BPRICE);
}
Result
Algorithm:
Step 1: START
Step 2: INPUT two values as X and Y
Step 3: WRITE X , Y values
Step 4: CALL swap(&X , &Y)
Step 5: WRITE X , Y values
Step 6: STOP
swap (*P , *Q): Procedure used to swap the given two values linked with P and Q.
Step 1: Temp ← *P
*P ← *Q
*Q ← Temp
Step 2: RETURN
Program:
#include<stdio.h>
void swap(int*,int*);
void main()
{
int X,Y;
clrscr();
printf("\n Enter Two Values =");
scanf("%d%d",&X,&Y);
printf("\n\n Before Swapping Values Are = %d\t%d",X,Y);
swap(&X,&Y);
printf("\n\n After Swapping Values Are = %d\t%d",X,Y);
}
void swap(int *P,int *Q)
{
int Temp;
Temp=*P;
*P=*Q;
*Q=Temp;
}
Result
Algorithm:
Step 1: START
Step 2: INPUT Number of Elements as N
Step 3: Allocate memory for a pointer Variable P with malloc ( ) or calloc ( ) function
Step 4: REPEAT FOR i ← 0 TO N-1 DO STEPS BY 1
INPUT P + i values
ENDREPEAT
Step 5: WRITE *P
Step 6: Release memory of P
Program:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
void main()
{
int *P,N,i;
clrscr();
printf("\n Enter How Many Values =");
scanf("%d",&N);
P=(int*)calloc(N,sizeof(int));
printf("\n Enter %d Elements =",N);
for(i=0;i<N;i++)
scanf("%d",P+i);
printf("\n Starting Value =%d",*P);
free(P);
P=NULL;
printf("\n Dangling Pointer Result = %d",*P);
}
Result
Enter 5 Elements = 10 23 56 42 78
Starting Value = 10
Dangling Pointer Result = 0
Algorithm:
Step 1: START
Step 2: INPUT a string as S1
Step 3: WRITE S1
Step 4: Define a pointer P and initialize with S1
Step 5: REPEAT FOR i ← 0 TO * ( P + i ) ≠ '\0' DO STEPS BY 1
S2 [ i ] ← * ( P + i )
ENDREPEAT
Step 6: S2 [ i ] ← '\0'
Step 7: WRITE S2
Step 8: STOP
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
char S1[50],S2[50],*P;
int i;
clrscr();
printf("\n Enter A String = ");
gets(S1);
printf("\n Actual String = %s",S1);
P=S1;
for(i=0;*(P+i)!='\0';i++)
S2[i]=*(P+i);
S2[i]='\0';
printf("\n Copied String = %s",S2);
}
Result
Algorithm:
Step 1: START
Step 2: INPUT a string as S1
Step 3: NLC ← 0
NUC ← 0
ND ← 0
NSP ← 0
Step 4: Define a pointer P and initialize with S1 as P ← S1
Step 5: REPEAT FOR i ← 0 TO * ( P + i ) ≠ '\0' DO STEPS BY 1
IF isalpha( * ( P + i ) ) THEN
IF islower( * ( P + i ) ) THEN
NLC ← NLC + 1
ELSE
NUC ← NUC + 1
ENDIF
ELSEIF isdigit( * ( P + i ) ) THEN
ND ← ND + 1
ELSE
NSP ← NSP + 1
ENDIF
ENDREPEAT
Step 6: WRITE NLC , NUC , ND and NSP values
Step 7: STOP
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char S1[100],*P;
int i,NLC,NUC,ND,NSP;
clrscr();
printf("\n Enter A String = ");
gets(S1);
NLC=0; NUC=0; ND=0; NSP=0;
P=S1;
for(i=0;*(P+i)!='\0';i++)
{
if(isalpha(*(P+i)))
{
if(islower(*(P+i)))
NLC=NLC+1;
else
NUC=NUC+1;
}
else if(isdigit(*(P+i)))
ND=ND+1;
else
NSP=NSP+1;
}
printf("\n Number of Lower Case Characters = %d",NLC);
printf("\n Number of Upper Case Characters = %d",NUC);
printf("\n Number of Digits = %d",ND);
printf("\n Number of Special Characters = %d",NSP);
}
Result
Step 1: START
Step 2: INPUT P , R , T values
Step 3: A ← P * ( pow ( ( 1 + R / 100 ) , T ) )
CI ← A – P
Step 4: WRITE CI value
Step 5: STOP
Program:
#include<math.h>
void main()
{
float P,R,T,A,CI;
clrscr();
printf("\n Enter Principle Amount = ");
scanf("%f",&P);
printf("\n Enter Rate of Interest = ");
scanf("%f",&R);
printf("\n Enter Number of Years = ");
scanf("%f",&T);
A = P * ( pow ( ( 1 + R / 100 ) , T ) ) ;
CI = A – P ;
printf("\n Compound Interest Amount = %.2f Rs" , CI );
}
Result
Algorithm:
Step 1: START
Step 2: INPUT Three Sides of the Triangle as A , B , C values
Step 3: SP ← ( A + B + C ) / 2 ;
Area ← sqrt ( ( SP * ( SP – A ) * ( SP – B ) * ( SP – C ) ) )
Step 4: WRITE Area value
Step 5: STOP
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float A,B,C,SP,Area;
clrscr();
printf("\n Enter Three Sides of the Triangle = ");
scanf("%f%f%f",&A,&B,&C);
SP = ( A + B + C ) / 2 ;
Area = sqrt ( ( SP * ( SP – A ) * ( SP – B ) * ( SP – C ) ) ) ;
printf("\n Area of the Triangle = %.2f", Area );
}
Result
Algorithm:
Step 1: START
Step 2: INPUT three values as a , b and c
Step 3: Dis ← ( b * b ) - ( 4 * a * c )
Step 4: IF Dis = 0 THEN
WRITE ‘Roots Are Real and Equal’
R1 ← - b / ( 2 * a )
R2 ← - b / ( 2 * a )
WRITE R1 , R2 values
ELSEIF Dis > 0 THEN
WRITE ‘Roots Are Real and Distinct’
R1 ← ( - b + sqrt ( Dis ) ) / ( 2 * a )
R2 ← ( - b – sqrt ( Dis ) ) / ( 2 * a )
WRITE R1 , R2 values
ELSE
WRITE ‘Roots Are Imaginary Parts’
ENDIF
Step 5: STOP
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
double a,b,c,R1,R2,Dis;
clrscr();
printf("\n Enter A, B and C Values =");
scanf("%lf%lf%lf",&a,&b,&c);
Dis = ( b * b ) - ( 4 * a * c ) ;
if ( Dis = = 0 )
{
printf("\n Roots Are Real and Equal ");
R1 = - b / ( 2 * a ) ;
R2 = - b / ( 2 * a ) ;
printf("\n ROOT 1 = %.2lf" , R1 ) ;
printf("\n ROOT 2 = %.2lf" , R2 ) ;
}
else if ( Dis > 0 )
{
printf("\n Roots Are Real and Distinct ");
R1 = ( - b + sqrt ( Dis ) ) / ( 2 * a ) ;
R2 = ( - b – sqrt ( Dis ) ) / ( 2 * a ) ;
printf("\n ROOT 1 = %.2lf" , R1 ) ;
printf("\n ROOT 2 = %.2lf" , R2 ) ;
}
else
printf("\n Roots Are Imaginary Parts ");
}
Result
ROOT 1 = -1.00
ROOT 2 = -1.00
Sin (x) = x – x3 / 3! + x5 / 5! – x7 / 7! + - - - - - - - - - - - -
Algorithm:
Step 1: START
Step 2: INPUT number of terms as N
Step 3: INPUT x value
Step 4: Sum ← 0
Count ← 0
Step 5: REPEAT FOR i ← 1 TO 2 * N – 1 DO STEPS BY 2
Count ← Count + 1
Fact ← 1
REPEAT FOR j ← 1 TO i DO STEPS BY 1
Fact ← Fact * j
ENDREPEAT
Term ← pow ( x , i ) / Fact
IF Count MOD 2 = 0 THEN
Sum ← Sum – Term
ELSE
Sum ← Sum + Term
ENDIF
ENDREPEAT
Step 6: WRITE Sum value
Step 7: STOP
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int x,N,i,j,Count;
double Fact,Sum,Term;
clrscr();
printf("\n Enter How Many Terms = ");
scanf("%d",&N);
printf("\n Enter X Value = ");
scanf("%d",&x);
Sum = 0 ;
Count = 0 ;
for ( i = 1 ; i < = 2 * N – 1 ; i + = 2 )
{
Count = Count + 1 ;
Fact = 1 ;
for ( j = 1 ; j < = i ; j++ )
Fact = Fact * j ;
Term = pow ( x , i ) / Fact ;
if ( Count % 2 = = 0 )
Sum = Sum – Term ;
else
Sum = Sum + Term ;
}
printf("\n Sine Series Result = %.2lf" , Sum );
}
Result
Enter X Value = 2
Cos (x) = 1 – x2 / 2! + x4 / 4! – x6 / 6! + - - - - - - - - - - - -
Algorithm:
Step 1: START
Step 2: INPUT number of terms as N
Step 3: INPUT x value
Step 4: Sum ← 1
Count ← 1
Step 5: REPEAT FOR i ← 2 TO 2 * N – 2 DO STEPS BY 2
Count ← Count + 1
Fact ← 1
REPEAT FOR j ← 1 TO i DO STEPS BY 1
Fact ← Fact * j
ENDREPEAT
Term ← pow ( x , i ) / Fact
IF Count MOD 2 = 0 THEN
Sum ← Sum – Term
ELSE
Sum ← Sum + Term
ENDIF
ENDREPEAT
Step 6: WRITE Sum value
Step 7: STOP
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int x,N,i,j,Count;
double Fact,Sum,Term;
clrscr();
printf("\n Enter How Many Terms = ");
scanf("%d",&N);
printf("\n Enter X Value = ");
scanf("%d",&x);
Sum = 1 ;
Count = 1 ;
for ( i = 2 ; i < = 2 * N – 2 ; i + = 2 )
{
Count = Count + 1 ;
Fact = 1 ;
for ( j = 1 ; j < = i ; j++ )
Fact = Fact * j ;
Term = pow ( x , i ) / Fact ;
if ( Count % 2 = = 0 )
Sum = Sum – Term ;
else
Sum = Sum + Term ;
}
printf("\n Cosine Series Result = %.2lf" , Sum );
}
Result
Enter X Value = 2
Algorithm:
Step 1: START
Step 2: INPUT N and R values
Step 3: Res ← Fact ( N ) / ( Fact ( R ) * Fact ( N – R ) )
Step 4: WRITE Res value
Step 5: STOP
Step 1: F ← 1
Step 2: REPEAT FOR i ← 1 TO K DO STEPS BY 1
F ← F*i
ENDREPEAT
Step 3: RETURN F
#include<stdio.h>
#include<conio.h>
int Fact(int);
void main()
{
int N,R,Res;
clrscr();
printf("\n Enter N Value = ");
scanf("%d",&N);
printf("\n Enter R Value = ");
scanf("%d",&R);
Res = Fact ( N ) / ( Fact ( R ) * Fact ( N – R ) ) ;
printf("\n NCR Result Value = %d" , Res ) ;
}
int Fact(int K)
{
int i , F = 1 ;
for ( i = 1 ; i < = K ; i++ )
F=F*i;
return F;
}
Result
Enter N Value = 5
Enter R Value = 3
Algorithm:
Step 1: START
Step 2: INPUT a string as ch
Step 3: N ← Length ( ch )
Step 4: WRITE N value
Step 5: STOP
Step 1: count ← 0
Step 1: REPEAT FOR i ← 0 TO STR[ i ] ≠ ‘\0’ DO STEPS BY 1
count ← count + 1
ENDREPEAT
Step 3: RETURN count
Program:
#include<stdio.h>
void main()
{
char ch[50];
int N;
clrscr();
printf("\n Enter A String = ");
gets(ch);
N = Length ( ch ) ;
printf("\n String Length = %d" , N ) ;
}
int Length(char STR[50])
{
int i , count = 0 ;
for ( i = 0 ; STR[i] ! = '\0' ; i++ )
count = count + 1 ;
return count;
}
Result
Algorithm:
Step 1: START
Step 2: INPUT Number of Rows as R and Columns as C
Step 3: REPEAT FOR i ← 0 TO R - 1 DO STEPS BY 1
REPEAT FOR j ← 0 TO C - 1 DO STEPS BY 1
INPUT K[ i ][ j ] value
ENDREPEAT
ENDREPEAT
Step 4: REPEAT FOR i ← 0 TO R - 1 DO STEPS BY 1
REPEAT FOR j ← 0 TO C - 1 DO STEPS BY 1
WRITE K[ i ][ j ] value
ENDREPEAT
ENDREPEAT
Step 5: Call Transpose ( K , R , C )
Step 6: REPEAT FOR i ← 0 TO C - 1 DO STEPS BY 1
REPEAT FOR j ← 0 TO R - 1 DO STEPS BY 1
WRITE T[ i ][ j ] value
ENDREPEAT
ENDREPEAT
Step 7: STOP
#include<stdio.h>
#include<conio.h>
int K[10][10],T[10][10];
void main()
{
int R,C,i,j;
clrscr();
printf("\n Enter How Many Rows = ");
scanf("%d",&R);
printf("\n Enter How Many Columns = ");
scanf("%d",&C);
printf("\n Enter Matrix Elements =");
for ( i = 0 ; i < R ; i++ )
{
for ( j = 0 ; j < C ; j++ )
scanf("%d" , &K[i][j] ) ;
}
printf("\n\n Actual Matrix Elements = ");
for ( i = 0 ; i < R ; i++ )
{
printf("\n");
for ( j = 0 ; j < C ; j++ )
printf("%d " , K[i][j] ) ;
}
Transpose ( K , R , C ) ;
printf("\n\n Transpose Matrix Elements = ");
for ( i = 0 ; i < C ; i++ )
{
printf("\n");
for ( j = 0 ; j < R ; j++ )
printf("%d ", T[i][j] ) ;
}
Result
Euler method
The Euler method gives an approximation for the solution of the differential equation:
dy / dt = f ( t , y )
with the initial condition:
y (t0) = y0
where t is continuous in the interval [a, b].
Algorithm:
Step 1: START
Step 2: Define a Macro as f ( x, y ) x + y
Step 3: INPUT x0 , y0 , P and N values
Step 4: h ← ( P - x0 ) / N
Step 5: REPEAT FOR i ← 1 TO N DO STEPS BY 1
Slope ← f ( x0 , y0 )
Res ← y0 + h * Slope
WRITE x0 , y0 , Res values
y0 ← Res
x0 ← x0 + h
ENDREPEAT
Step 6: WRITE Res value
Step 7: STOP
#include<stdio.h>
#include<conio.h>
#define f ( x , y ) x+y
void main()
{
float x0,y0,h,P,Res,Slope;
int N,i;
clrscr();
printf("\n Enter Initial Value X0 = ");
scanf("%f",&x0);
printf("\n Enter Initial Value Y0 = ");
scanf("%f",&y0);
printf("\n Enter Calculation Point = ");
scanf("%f",&P);
printf("\n Enter Number of Steps = ");
scanf("%d",&N);
h = ( P - x0 ) / N ;
printf("\n X0\t Y0\t Res\n");
for ( i = 1 ; i < = N ; i++ )
{
Slope = f ( x0 , y0 ) ;
Res = y0 + h * Slope ;
printf("\n %.2f\t %.2f\t %.2f" , x0 , y0 , Res ) ;
y0 = Res ;
x0 = x0 + h ;
}
printf("\n Result Value at %.2f is = %.2f" , P , Res ) ;
}
X0 Y0 Res
0.00 1.00 1.10
0.10 1.10 1.22
0.20 1.22 1.36
0.30 1.36 1.53
0.40 1.53 1.72
0.50 1.72 1.94
0.60 1.94 2.20
0.70 2.20 2.49
0.80 2.49 2.82
0.90 2.82 3.19
Algorithm:
Step 1: START
Step 2: INPUT number of terms as K
Step 3: REPEAT FOR i ← 0 TO K - 1 DO STEPS BY 1
P ← Fib ( i )
WRITE P value
ENDREPEAT
Step 4: STOP
Step 1: IF N = 0 OR N = 1 THEN
RETURN N
ELSE
RETURN Fib ( N – 2 ) + Fib ( N – 1 )
ENDIF
#include<stdio.h>
#include<conio.h>
int Fib(int);
void main()
{
int i,K,P;
clrscr();
printf("\n Enter How Many Terms = " ) ;
scanf("%d",&K);
printf("\n Fibonacci Sequence = ");
for ( i = 0 ; i < K ; i++ )
{
P = Fib ( i ) ;
printf(" %d" , P ) ;
}
}
int Fib(int N)
{
if ( N = = 0 | | N = = 1 )
return N;
else
return Fib ( N – 1 ) + Fib ( N – 2 ) ;
}
Result
Fibonacci Sequence = 0 1 1 2 3
Algorithm:
Step 1: START
Step 2: INPUT two values as P and Q
Step 3: R ← LCM ( P , Q )
Step 4: WRITE R value
Step 5: STOP
#include<stdio.h>
#include<conio.h>
int LCM(int,int);
void main()
{
int P,Q,R;
clrscr();
printf("\n Enter Two Values = " ) ;
scanf("%d%d",&P,&Q);
R = LCM ( P , Q ) ;
printf("\n LCM Result = %d" , R ) ;
}
Result
Algorithm:
Step 1: START
Step 2: INPUT a values as K
Step 3: P ← Fact ( K )
Step 4: WRITE P value
Step 5: STOP
Step 1: IF N = 1 THEN)
RETURN 1
ELSE
RETURN N * Fact ( N – 1 )
ENDIF
Program:
#include<stdio.h>
int Fact(int);
void main()
{
int K,P;
clrscr();
printf("\n Enter A Value = " ) ;
scanf("%d",&K);
P = Fact ( K ) ;
printf("\n Factorial Value = %d" , P ) ;
}
int Fact(int N)
{
if ( N = = 1 )
return 1;
else
return N * Fact ( N – 1 ) ;
}
Result
Enter A Value = 5
Algorithm:
Step 1: START
Step 2: INPUT two values as P and Q
Step 3: R ← Ack ( P , Q )
Step 4: WRITE R value
Step 5: STOP
Step 1: IF M = 0 THEN
RETURN N + 1
ELSEIF N = 0 THEN
RETURN Ack ( M – 1 , 1 )
ELSE
RETURN Ack ( M – 1 , Ack ( M , N – 1 ) )
ENDIF
#include<stdio.h>
#include<conio.h>
int Ack(int,int);
void main()
{
int P,Q,R;
clrscr();
printf("\n Enter Two Non-Negative Values = " ) ;
scanf("%d%d",&P,&Q);
R = Ack ( P , Q ) ;
printf("\n Ackermann Function Result = %d" , R ) ;
}
Result
Algorithm:
Step 1: START
Step 2: INPUT Number of terms as N
Step 3: R ← RSum (N)
Step 4: WRITE R value
Step 5: STOP
Step 1: IF K = 0 THEN
RETURN 0
ELSE
RETURN K + RSum ( K – 1)
ENDIF
Program:
#include<stdio.h>
int RSum(int);
void main()
{
int N,R;
clrscr();
printf("\n Enter How Many Terms = " ) ;
scanf("%d",&N);
R = RSum ( N ) ;
printf("\n Sum Result Value = %d" , R ) ;
}
int RSum(int K)
{
if ( K = = 0 )
return 0;
else
return K + RSum ( K – 1 ) ;
}
Result
Algorithm:
Step 1: START
Step 2: Open a file pointer FP in “Write” mode
Step 3: REPEAT WHILE (ch = getchar( )) ≠ EOF
fputc( ch, FP)
ENDREPEAT
Step 4: Close the file pointer FP
Step 5: STOP
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *FP;
char ch;
clrscr();
FP=fopen("Demodata.txt","w");
printf("\n Enter Data Into the File =");
while((ch=getchar())!=EOF)
fputc(ch,FP);
fclose(FP);
FP=fopen("Demodata.txt","r");
if(FP==NULL)
{
printf("\n File Opening Error");
exit();
}
printf("\n File Contents Are =\n");
while((ch=fgetc(FP))!=EOF)
putchar(ch);
fclose(FP);
}
Result
Algorithm:
Step 1: START
Step 2: Define a student structure with rno as integer, name and branch as strings
Step 3: Open a file pointer f in “wb” mode
Step 4: INPUT number of students as n
Step 5: REPEAT FOR i ← 1 TO n DO STEPS BY 1
INPUT s[ i ].rno , s[ i ].name , s[ i ].branch
fwrite(&s[i],sizeof(s[i]),1,f)
ENDREPEAT
Step 6: Close file pointer f
Step 7: Open a file pointer f in “rb” mode
Step 8: REPEAT FOR i ← 1 TO n DO STEPS BY 1
fread(&s[i],sizeof(s[i]),1,f);
WRITE s[ i ].rno , s[ i ].name , s[ i ].branch
ENDREPEAT
Step 9: Close file pointer f
Step 10: STOP
#include<stdio.h>
#include<conio.h>
typedef struct
{
int rno;
char name[25],branch[5];
}student;
void main()
{
int i,n;
student s[20];
FILE *f = fopen("stdemo.txt","wb");
clrscr( );
printf("\n Enter how many students:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n Student %d data:\n",i);
printf("\n Enter Student Roll Number =");
scanf("%d",&s[i].rno);
fflush(stdin);
printf("\n Enter Name of the Student =");
gets(s[i].name);
fflush(stdin);
printf("\n Enter Branch of the Student =");
gets(s[i].branch);
fwrite(&s[i],sizeof(s[i]),1,f);
}
fclose(f);
f = fopen("stdemo.txt","rb");
printf("\n Database Details Are:\n");
for(i=1;i<=n;i++)
{
fread(&s[i],sizeof(s[i]),1,f);
printf("\n%d\t%s\t%s",s[i].rno,s[i].name,s[i].branch);
}
fclose(f);
}
Student 1 data:
Student 2 data:
Algorithm:
Step 1: START
Step 2: Open a file pointer FP1 in “Read” mode
IF FP1 = NULL THEN
WRITE ‘File Not Opened’
EXIT
ENDIF
Step 3: Open a file pointer FP2 in “Write” mode
Step 4: REPEAT WHILE (ch = fgetc(FP1 )) ≠ EOF
fputc( ch, FP2)
ENDREPEAT
Step 4: Close the file pointers FP2, FP1
Step 5: STOP
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *FP1,*FP2;
char ch;
clrscr();
FP1=fopen("Demodata.txt","r");
if(FP1==NULL)
{
printf("\n File Opening Error");
exit();
}
FP2=fopen("Copydata.txt","w");
while((ch=fgetc(FP1))!=EOF)
fputc(ch,FP2);
fclose(FP2);
fclose(FP1);
}
Demodata.txt
C and C++
Programming Languages.
Copydata.txt
C and C++
Programming Languages.
Algorithm:
Step 1: START
Step 6: STOP
Program:
#include<stdio.h>
#include<conio.h>
Demodata.txt
C and C++
Programming Languages.
Copydata.txt
Java Language.
Resultdata.txt
C and C++
Programming Languages.
Java Language.
Algorithm:
Step 1: START
Step 2: Open a file pointer FP1 in “Read” mode
IF FP1 = NULL THEN
WRITE ‘File Not Opened’
EXIT
ENDIF
Step 3: Lines ← 1
Words ← 1
Nchar ← 0
Step 4: REPEAT WHILE (ch = fgetc(FP1 )) ≠ EOF
Nchar ← Nchar + 1
IF isspace(ch) THEN
Words ← Words + 1
Nchar ← Nchar + 1
ENDIF
IF ch = ‘\n’ THEN
Lines ← Lines + 1
ENDIF
ENDREPEAT
Step 5: WRITE Lines, Words, Nchar values
Step 6: Close the file pointer FP1
Step 7: STOP
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
FILE *FP1;
char ch;
int Lines,Words,Nchar;
clrscr();
FP1=fopen("Count.txt","r");
if(FP1==NULL)
{
printf("\n File Opening Error");
exit();
}
Lines=1;
Words=1;
Nchar=0;
while((ch=fgetc(FP1))!=EOF)
{
Nchar=Nchar+1;
if(isspace(ch))
{
Words=Words+1;
Nchar=Nchar-1;
}
if(ch=='\n')
{
Lines=Lines+1;
}
}
printf("\n Number of Lines = %d",Lines);
printf("\n Number of Words = %d",Words);
printf("\n Number of Characters = %d",Nchar);
fclose(FP1);
}
Count.txt
C Language
In 1972.
Number of Lines = 2
Number of Words = 4
Number of Characters = 16
Algorithm:
Step 1: START
Step 2: Open a file pointer FP1 in “r” mode
Step 3: INPUT number of characters as N
Step 4: fseek(FP1,-N,2)
Step 5: REPEAT WHILE (ch = fgetc(FP1) ) ≠ EOF
WRITE ch
ENDREPEAT
Step 6: Close the file pointer FP1
Step 7: STOP
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
FILE *FP1;
char ch;
int N;
clrscr();
FP1=fopen("Check.txt","r");
if(FP1==NULL)
{
printf("\n File Opening Error");
exit();
}
printf("\n Enter How Many Characters You Need =");
scanf("%d",&N);
printf("\n Resultant Characters = \n");
fseek(FP1,-N,2);
while((ch=fgetc(FP1))!=EOF)
putchar(ch);
fclose(FP1);
}
Result
Check.txt
C and C++
Programming Languages.
THE END