C& DS Lab Manual
C& DS Lab Manual
C& DS Lab Manual
(a)
1) AIM: To evaluate algebraic exp(ax+b)/(ax-b)
2) ALGORITHM:
Step1: start
Step2: input a,b,x,s
Step3: s= (a*x+b)/(a*x-b)
Step4: Result s
Step 5: stop
3) FLOW CHART:
start
To evaluate algebraic exp (ax+b)/ (ax-b)
take a,b,x
a=5,b=4,x=3
R=(ax+b)/(ax-b)
display
stop
5) Result:
Enter the values of a,b,x… 1 3 2
The value of s=5
2) ALGORITHM:
Step1: start
Step2: input x,y,v
Step3: v=2.5*log(x)+cos(32*3.14/180)+mod(x*x-y*y)+sqrt(2*x*y)
Step4: Result v
Step 5: stop
3) FLOWCHART:
take x,y
x=1,y=1
R=2.5*logx+cos32+mod(x*x-y*y)+sqrt(2*x*y)
Display R
stop
CH Ramesh Asst Prof Dept of IT Page 4
4) PROGRAM:
#include<math.h>
main()
{
float x,y,v;
clrscr();
printf("enter x and y values");
scanf("%f,%f",&x,&y);
v=2.5*log(x)+(cos(32*3.14/180))+mod(x*x-y*y)+sqrt(2*x*y);
printf("the value of v=%f",v);
}
5) Result:
2) ALGORITHM:
Step1: start
Step2: input x,s
Step3:s=pow(x,s)+10*pow(x,4)+8*pow(x,3)+4*x+2
Step4: Result s
Step 5: stop*/
3) FLOWCHART:
start
take x
x=6
R==pow(x,s)+10*pow(x,4)+8*pow(x,3)+4*x+2
Display R
stop
#include<stdio.h>
#include<math.h>
main ()
{
float x,s;
printf("enter the values of x");
scanf("%f",&x);
s=pow(x,5)+10*pow(x,4)+8*pow(x,3)+4*x+2;
printf("the value of s=%f",s);
}
5) Result:
2) ALGORITHM:
3) FLOWCHART:
take a,k,t
a=1,k=1,t=1
R=a*pow(e,-kt)
displayR
stop
#include<stdio.h>
#include<math.h>
main()
{
int a,k,t;
float r;
printf("enterthree values");
scanf("%d%d%d",&a,&k,&t);
r=a*pow(e,-k*t);
printf("result=%f");
getch();
}
5) Result:
Enter values
1
2
3
Result=1.000000
6) Questions:
i) What is an Expression?
ii) What is the use of main( ) function?
iii) What are preprocessors of C?
iv) What is a variable?
7) Debugging:
1) undefined symbol ‘a’ in function main( ) First you should declare ‘a’ and use
2) ‘r’ is assigned a value which is never used When you assigned a value to a variable, that
must be used in the program
3) redeclaration of ‘c’ in function main( ) You should declare any variable only one time
3) FLOWCHART:
Start
take a,b,c,s,a
a=5,b=4,c=2
s=(a+b+c)/2
A=sqrt(s-a)(s-b)(s-c)
Display R
#include<math.h>
void main()
{
int a,b,c;
float s,area;
clrscr();
printf("enter the values of a,b,c");
scanf("%d%d%d",&a,&b,&c);
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("the area of a trangle is =%f",area);
getch();
}
5) Result:
6) Questions:
7) Debugging:
1) Function ‘sqrt( )’ should have a prototype You should include ‘math.h’ first, then you
can use ‘sqrt ( )’ and other mathematical
functions.
2) Unterminated string or character constant You should end double quotation or single
quotation properly
3) Function call missing ‘)’ in function main You might be missed any special characters in
that line.
3) FLOWCHART:
start
take a,b
a=1,b=10
a=a+b;b=a-b;a=a-b
Display a and b
void main()
{
int a,b;
clrscr();
printf("enter the values of a,b");
scanf("%d%d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("the values of a,b are: %d %d",a,b);
getch();
}
5) Result:
6) Questions:
i) What is the use of getch( ) function?
ii) What is the use of specifications of the data types?
3) FLOWCHART:
Start
Take a,b
C= (a>b)? a:b
Display c
Stop
void main()
{
int a,b,c;
clrscr();
printf("enter the values of a,b");
scanf("%d%d",&a,&b);
c=(a>b)?a:b;
printf("the biggest no is %d",c);
getch();
}
5) Result:
6) Questions:
1) What is an operators?
2) How many operators are there in C and List out them?
3) What is the difference between logical and conditional operators?
2) ALGORITHM:
Step1:start
Step2:input a,b,c
Step3:if(a>b) &&(a>c)
Step4:display a is grater
Step 5:else
Step6:if(b>c)
Step7: display b is grater
Step 8:else
Step: display c is grater
Step10:stop
Start
Take a,b,c
If
(a>b)&&(a>
c)
Display a
If (b>c)
Display b
Display c
Stop
void main()
{
int a,b,c;
clrscr();
printf("enter the values of a,b and c");
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c)
printf("a is greatest of %d %d %d", a,b,c);
else
if(b>c)
printf("b is greatest of %d %d %d",a,b,c);
else
printf("c is gratest of %d %d %d",a,b,c);
getch();
}
5) Result:
6) Questions:
i) What are the conditional statements?
ii) How many conditional statements are there in C?
iii) What is the difference between conditional and multi-conditional statements?
2) ALGORITHM:
Step1:start
Step2:input a,b,c
Step3:if(a>b) &&(a>c)
Step4:if(b>c)
Step5:display a,b,c
Step6:else
Step7:display a,c,b
Step8:else if(b<c && b<a)
Step9:if(c<a)
Step10:print b,c,a
Step11:else
Step12:print b,a,c
Step13:else if(c<a && c<b)
Step14:if(a<b)
Step15:print c,a,b
Step16:else
Step17:print c,b,a
Step18:stop*/
Start
Take
a,b,c
If
(a>b && a>c)
If (b>c)
Display a,c,b
Display
a,b,c
If
(b<c
&&(b<a)
If
(c<
a)
If
C<a &&
c<b)
If
Stop
( a<b)
void main()
{
int a,b,c;
clrscr();
printf("enter the values of a,b and c");
scanf("%d%d%d",&a,&b,&c);
if(a<b && a<c)
{
if(b<c)
{
printf(" %d%d%d", a,b,c);
}
else
if(b>c)
printf(" %d%d%d",a,c,b);
}
else
if(b<c && b<a)
{
if(c<a)
printf(" %d%d%d",b,c,a);
else
printf("%d%d%d",b,a,c);
}
else
if(b<a)
printf("%d%d%d",c,b,a);
else
printf(%d%d%d",c,a,b);
}
}
5) Result:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
int op;
clrscr();
printf(" 1.addition\n 2.subtraction\n 3.multiplication\n 4.division\n");
printf("enter the values of a & b");
scanf("%d%d",&a,&b);
printf("enter your choice : ");
scanf("%d",&op);
switch(op)
{
case 1 :printf("sum of %d and %d=%d",a,b,a+b);
break;
case 2 :printf("difference of %d and %d=%d",a,b,a-b);
break;
case 3 :printf("multiplication of %d and %d=%d",a,b,a*b);
break;
case 4 :printf("Divisionn of two numbers is %d=",a/b);
break;
default : printf(" Enter Your Correct Choice.");
break;
}
getch();
}
5) Result:
1. Addition
2. Substraction
3. Multiplication
4. Division
Enter your choice : 1
Enter a and b values 10 20
Sum of 10 and 20 = 30
Start
Take n
F=i=1
If
(i<=n
)
f=f*i; i=i+1
Display f
Stop
void main()
{
int n,i,f;
f=i=1;
clrscr();
printf("enter a number");
scanf("%d",&n);
while(i<=n)
{
f*=i;
i++;
}
printf("the factorial of %d is %d",n,f);
getch();
}
5) Result:
Enter a number 5
6) Questions;
Start
Take n,i,j
i=1,
if i<n
j=1, fact=0
If
j<=n
If i%j=0
If
fact=2
Print i, j++
void main()
{
int n,i,fact,j;
printf("enter the range");
scanf("%d",&n);
printf(“Prime numbers are\n”);
for(i=1;i<=n;i++)
{
fact=0;
for(j=1;j<=n;j++)
{
if(i%j==0)
fact++;
if(f==2)
printf("%d “,i);
}
getch();
}
5) Result:
2) Algorithm:
Step1: start
Step2: read n
Step3: i=0,sum=0
Step4: perform from step 5 to step 6 until i<=n
Step5: i++
Step6:sum+=i;
Step7: write sum
Step8: stop
3) Flow chart:
start
Read n
i=0;sum=0
F
While(i<=n) T
i++
Sum+=i
Write sum
CH Ramesh Asst Prof Dept of IT Page 30
stop
4) Program:
#include<stdio.h>
#include<conio.h>
main()
{
int n,i=0,sum=0;
clrscr( );
printf(“Enter Limit : “);
scanf(“%d”,&n);
while(i<=n)
{
i++;
sum+=i;
}
printf(“Sum of %d natural numbers = %d”,n,sum);
getch();
}
5) Result:
Enter Limit : 10
Sum of 10 natural numbers = 55
2) ALGORITHM:
step1: start
step3: stop
Start
Take i, a[20],
sum
i=0
If
i<=20
If
(A[i]
%2==0)
Sum=sum+a[i],i++
I++
Display Sum
Stop
#include<stdio.h>
main()
{
int a[20],i,sum=0;
printf("enter5 integrs");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
for(i=0;i<5;i++)
{
if(a[i]==0)
sum=sum+a[i];
}
prinf("sum =%d",sum);
getch();
}
5) Result:
Entger 5 integers
24682
Sum = 22
2) ALGORITHM:
step1: start
step2: for(i=0;i<20;i++)
{
if(a[i]%2==1)
sum=sum+a[i];
}
step3:stop
Start
Take i, a[20],
sum
i=0
If
i<=20
If
(A[i]
%2==1)
Sum=sum+a[i],i++
I++
Display Sum
Stop
#include<stdio.h>
main()
{
int a[20],i,sum=0;
printf("enter 5 integrs");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
for(i=0;i<5;i++)
{
if(a[i]==1)
sum=sum+a[i];
}
prinf("sum =%d",sum);
getch();
}
Enter 5 integers
12345
Sum=9
void main()
{
int i,n,sum;
sum=0;
clrscr();
printf("enter any number");
scanf("%d",&n);
for(i=2;i<=n;i++)
{
if(i%2==0)
sum=sum+i;
}
printf("total no of even integer is %d",sum);
}
5) Result:
Start
Take i, a[20],
sum
i=0
If
i<=20
If
(A[i]
%2==1)
Sum=sum+a[i],i++ Stop
i++
Display Sum
void main()
{
int i,n,sum;
sum=0;
clrscr();
printf("enter any number");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%2!=0)
sum=sum+i;
}
printf("total no of even integer is %d",sum);
5) Result:
Start
Take a[3][3],b[3][3],c[3]
[3],i,j,k
I=0,j=0,k=0
If
i<=20
if
(i<n
if
j<n
Stop
For
(k=0;j<k.k+
+)
k++
j++
C[i][j]=c[i][j]+a[k][j]*b[i][k]
i++
Display c[i][j]
#include<stdio.h>
void main()
{
int i,j,k,a[3][3],b[3][2],c[3][2];
printf("enter elements of matrix a");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
}
printf("enter elements of matrix b");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
scanf("%d",&b[i][j]);
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
printf("\t%d",c[i][j]);
}
printf("\n");
}
}
}
}
5) Result:
Start
Take I, x, f, f1, f2
While
(i<=n)
I++,F1=f2;f2=f
F=f+f2 Stop
F=f+f2
Display F
void main()
{
int i,n,f,f1,f2;
printf("enter the range");
scanf("%d",&n);
f=0;
f1=1;
f2=1;
do
{
i++;
printf("%d\n",f);
f1=f2;
f2=f;
f=f1+f2;
}
while(i<=n);
}
5) Result:
Enter the range 9
0 1 1 2 3 5 8 13 21
2) ALGORITHM:
step1:start
step2:take I,j and n
step3:for(i=1;i<n;i++)
for(j=0;j<i;j++)
{
printf(”%d”,i);
printf(”\n”);
}
step4: stop
Take I,j,n,i=1,j=1
If
i<n
If
j<n
Stop
Display i
Display “\n”
J++
CH Ramesh AsstI++
Prof Dept of IT Page 50
4) PROGRAM:
Program to print the following format
1
2 2
3 3 3
4 4 4 4
#include<stdio.h>
main()
{
int i,j,n;
printf(“enter n value”);
scanf(“%d”,&n);
for(i=0;i<=n;i++)
{
for(j=0;j<i;j++)
printf(”%d”,i);
printf(”\n”);
}
printf(”\n”);
}
5) Result:
1
2 2
3 3 3
4 4 4 4
step1: start
step2: take three integers i,j,n
step3: repeat step4 to step6 for i=1,i<=n,i++
step4: repeat step5 for j=1,j<=n,j++
step5: if j>=1 then
Display I and a space
Else
Display space
step6: transfer cursor to meet line by printing ‘\n’
step7: stop
Start
Take i,j,n
For
(i=1;i<=n;i+
+)
If
(j>i)
I++
Stop
Display “\t”
Display “\n”
5) Result:
1
2 2
3 3 3
Experiment 16.3 :
1) AIM: Program to print the following format
2) ALGORITHM:
1
2 2
3 3 3
step1: start
step2: take three integers i,j,k
step3: repeat step2 to step8 for i=1,i<=n,i++
step4: repeat step3 to step4 for k=1,k<=n-i,k++
step5: display blank space
step6: repeat step 5 to step7 for j=1,j<=I,j++
step7: display blank space
step8: take cursor to new line
step9: stop
Take i,j,n,k=0,j=1
If
I<n
K<n
Display “\n”
If
J<=i
Display”\n”
Display i
Stop
J++
I++
5) Result:
1
2 2
Experiment 16.4 :
1) AIM: Program to print the following format
2) ALGORITHM:
1
23
4 5 6
step1: start
step2: take three integers i,j,k,n and initialize k as 1
step3: repeat step4 to step7 for i=1,i<=n,i++
step4: repeat step5 to step6 for j=1,j<=i,j++
step5: display value of k
step6: increment k by 1
step7: transfer cursor to next line by printing ‘\n’
step8: stop
Start
Take I=0,j=0,k
K=0
If
I<=n
if
j<=n
K=k+1
Display K
J++
PROGRAM:
Program to print the following format
1
2 3
4 5 6
#include<stdio.h>
main()
{
int i,j,k=1,n;
printf(“enter n value”);
scanf(“%d”,&n);
for(i=0;i<=n;i++)
{
for(j=0;j<=i;j++)
printf(”%d\t”,k++);
printf(”\n ”);
}
}
5) Result:
1
23
4 5 6
2) ALGORITHM:
Start
Take n[20]
[20],r[20],s[20],I=0,j,n
If
I<n
I++
Stop
I=0
If
I<n
J=0
If
Display Asst
CH Ramesh s[i][j] Prof Dept of IT Page 62
I<n
J++
4) PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
char n[20][10];
int i,j,r[20],s[20][6];
printf("enter n value");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter name,rollno,....");
scanf("%s%d",&n[i],&r[i]);
printf("enter 5 subject marks");
s[i][5]=0;
for(j=0;j<5;j++)
{
scanf("%d",s[i][j]);
s[i][5]=s[i][5]+s[i][j];
}
}
printf("the data entered is \n");
for(i=0;i<n;i++)
{
printf("%s\t%d\t",n[i],r[i]);
for(j=0;j<5;j++)
printf("%d\t",s[i][j]);
}
getch();
}
5) Result:
Enter name,rollno,….Eswar 20
Enter 5 subject marks
10 50 34 06 42
The data entered is
Eswar 20 10 50 34 06 42
2) ALGORITHM:
step1:start
Step2:take a number n
Step3:read a number n
For(i=0;i<n;i++)
Factorial=fact*I;
Display num
Step4:stop
3) FLOWCHART: Start
F=i=1
If
(i<=n
)
f=f*i; i=i+1
CH Ramesh Asst Prof Dept of IT Stop Page 64
Display f
4) PROGRAM:
#include<stdio.h>
#include<math.h>
void main()
{
clrscr();
printf("enter a number");
fact();
getch();
}
fact()
{
int i,fact=1,n;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("\nfactorial of a given no is: %d ",fact);
return fact;
}
5) Result:
Enter a number 5
Factorial of a given no is: 120
2) ALGORITHM:
step1: start
Step2: take a number I and fact=1
Step3: read a number n
For(i=0;i<n;i++)
Factorial=fact*i;
Display fact
Step4: stop
Start
Take n
Fact(n)
F=i=1
If
(i<=n
)
f=f*i; i=i+1
Stop
Display f
#include<stdio.h>
#include<math.h>
void main()
{
clrscr();
printf("enter a number");
fact();
getch();
}
fact()
{
int i,fact=1,n;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("\nfactorial of a given no is: %d ",fact);
return fact;
}
5) Result:
Enter a number 5
Factorial of a given no is: 120
1) AIM: Program on function to scan a character string and convert lower case character
to upper case
2) ALGORITHM:
step1: start
Step2: take a string a function of return value data type is void str upper
Step3: read a string
While (s[i]! =’\0’)
{
if((s[i]>=’a’) &&(s[i]<=’z’))
s[i]=s[i]-32;
i++;
}
display changed string.
Step4: stop
Program on function to scan a character string and convert lower case character to upper
case
Start
Take str,I,j,
While
S[i]!=’\0’
If
((S[i]>=’a’)
&& (s[i]>=’z’))
S[i]=s[i]-32
Stop
I++
Program on function to scan a character string and convert lower case character to upper
case
#include<stdio.h>
#include<conio.h>
void main()
{
char str;
printf("enter a string");
scanf("%s",str);
to_str_upper(char[]);
printf("changed to %s",str);
}
void to_str_upper(char[])
{
int i=0;
while(s[i]!='\0')
{
if((s[i]>='a') && (s[i]>='z'))
s[i]=s[i]-32;
i++;
}
}
}
5) Result:
Enter a string
gnec
changed to GNEC
1) AIM: A program to extract a portion of character string and print extracted string
2) ALGORITHM:
step1: start
Step2: take a a and r characters arrays and I,j,m,n be untegers
Step3: enter the values of m,n
J=0;
For(i=n-1;i<m+n-1;i++)
{
r[j]=s[i];
j++;
}
step4: display the extract part of string
Step5:stop
Start
Take
s[30],r[30],j=0,n
I=n-1
If
I<m+nm-
1
r[j]=s[i]
J++
Stop
I++
#include<stdio.h>
void main()
{
char s[30],r[30];
int i,j,m,n;
clrscr();
printf("enter a string");
gets(s);
printf("enter the values of m n");
scanf("%d%d",&m,&n);
j=0;
for(i=n-1;i<m+n-1;i++)
{
r[j]=s[i];
j++;
}
printf("the extract part of string %s: ",r);
getch();
}
5) Result:
Enter a string
Gurunanak
Enter the values of m,n
35
The extract part of string: run
1) AIM: Program to read five cities and sort them and print sorted list of citied in
alphabetical order
2) ALGORITHM:
step1:start
Step2:enter 5 city names
Step3:take I and j loop variables
For(i=65;i<122;i++)
{
for(j=0;j<5;j++)
{
if(city[j][0]==i)
printf(”\n%s”,city[j]);
}
}
Step4:stop
3) FLOWCHART:
A program to read five cities and sort them and Take city[5][20], I,j
Print sorted list of citied in alphabetical order
I=0
If
(i<5)
I++
if
I<122
J=0
if
j<5
Stop
If
(City[j]
[0]==i)
Display city[j]
CH Ramesh
J++] Asst Prof Dept of IT Page 76
I++
4) PROGRAM:
A program to read five cities and sort them and print sorted list of citied in alphabetical
order
#include<stdio.h>
#include<conio.h>
void main()
{
ch city[5][20];
int I,j;
clrscr();
printf("enter the names of cities...\n\n");
for(i=0;i<5;i++)
scanf("%s",&city[i]);
printf("sorted list of cities...\n\n");
for(i=65;i<122;i++)
{
for(j=0;j<5;j++)
{
if(city[j][0]==i)
printf("\n%s",city[j]);
}
}
}
5) Result:
Enter the names of cities
Hyd Chennai Bombay goa vizag
Sorted list of cities
Bombay
Chennai
Goa
Hyd
vizag
2) ALGORITHM:
step1: start
Step2: enter f and n
Step3: read a number n
F=factorial (n);
Step4: inside the functional(x) define a local variable ‘x’
If(x==l)
Return (l);
Else
Fact=x*factorial(x-l);
Return(fact);
Step5: stop
Start
Take n
Fact(n) Display f
F=i=1
If
(i<=n
)
Stop
5) Result:
Enter n 4
24
3) Flowchart:
start
Declare a
Print &a
stop
5) Result:
Address of a =64453
1) AIM: program to illustrate accessing the value of variable using pointers using
arithmetic operations
2) ALGORITHM:
step1: start
step2: take a,b,x,y,z and two pointers variables *p1,*p2
step3: assign values to these variables
p1=&a;
p2=&b;
x=*p1*p2-6;
y=(4*-*p2)/(*p1+10);
display x and y
step4:*p2=*p2+3
*p1=*p2-5;
z=*p1*p2-6;
display a,b and z
step5: stop
A program to illustrate accessing the value of variable using pointers using arithmetic
operations
Start
a=12,b=4
P1=&a,p2=&b
X=*p1*p2-6
Y=(4-*p2)/*p1+10
Display p1,p2,a,b,x,y
*p2=*p2+3,*p1=*p2-5
Z=*p1*p2-6
Display a,b,z
Stop
A program to illustrate accessing the value of variable using pointers using arithmetic
operations
#include<stdio.h>
main()
{
int a,b,*p1,*p2,x,y,z;
clrscr();
a=12,b=4;
p1=&a; p2=&b;
x=*p1**p2-6;
y=(4-*p2)**p1+10;
printf("addressof a=%d\n",p1);
printf("addressof b=%d\n",p2);
printf("a=%d,b=%d\n",a,b);
printf("x=%d,y=%d\n",x,y);
*p2=*p2+3; *p1=*p2-5;
z=*p1**p2-6;
printf("a=%d,b=%d\n",a,b);
printf("z=%d\n",z);
getch();
}
5) Result:
Address of a = 65543
Address of b = 64455
a = 12 b = 4
z=42
3) FLOWCHART:
A program to illustrate the address of a variable using various methods
Start
a=’a’,x=125,p=10.25
, q=18.76
Display a, &a
Display x, &x
Display p, &p
Display q, &q
Z=*p1*p2-6
Stop
5) Result:
a is stored at address 65525
125 is stored at address 65522
10.250000 is stored at address 65518
18.760000 is stored at address 65514
3) FLOWCHART:
A program to print the elements of array using pointers
Start
If
I<5
Display *(p+i)
Display (p+i)
I++ Stop
5) Result:
12345
12345
Start
Take a=10,b=20
C=add (&a,&b)
Z=*x+*y
Stop
Return (z)
5) Result:
30
Start
Take a[10],i=0,n,l
If
I<n
Read a+I
I++
L=max(a,n)
I=o
Stop
If
I<n
If
(i==0||
(arr+i)
Max=*(arr+i)
I++
Display (p+i)
564
The largest number is 6
Start
Take product[3],*ptr
If
Ptr<product
+3
Ptr=product
while
Ptr<product
+3
Display ptr->name,Ptr=>number,
ptr->price
Ptr ++
Start
FLOWCHART:
A program to display student information by initializing structures
Start
Struct Student
Stop
5) Result:
Ente name, rollno,age
Ravi 11 25
Ravi 11 25
Struct student s
If
I<n
S[i].total=0
J=0
If
j<5
S[i]=s[i].total+s[i].subject[j]
J++
Display Total
I++
Start
CH Ramesh Asst Prof Dept of IT Page 102
PROGRAM:
A program to find the total no. of marks
#include<stdio.h>
struct student
{
char name[10];
int rollno;
int subject[5],total;
};
main ( )
{
static struct student s[100];
int n,i,j;
clrscr();
printf("enter the no.of students");
scanf("%d",&n);
printf("enter the marks of fivesubjects");
for(i=0;i<n;i++)
{
printf("enter s[%d] student marks",i);
s[i].total=0;
for(j=0;j<5;j++)
{
scanf("%d",&s[i].subject[j]);
s[i].total=s[i].total+s[i].subject[j];
}
printf("%d",s[i].total);
5) Result:
enter the no.of students2
enter the marks of fivesubjectsenter s[0] student marks1
2
3
4
5
15enter s[1] student marks12
32
14
15
65
138
Start
Struct employee e1
Struct salary s1
E1.s1.hra=15%*basic
E1.s1.da=45%*basic
E1.s1.gross=e1.s1.basic+e1.s1.hra
+e1.s1.da+e1.s1.pf
Stop
4) Result:
Enter the marks
10 20 30 40 50
150
4) Result:
Gnec ibrahimpatnam rr dist
1) AIM: Program to write data file and read data from file
2) ALGORITHM:
step1: start
step2: take a character ch and define a file pointer f2
step3: open a file data.dat for writing
step4: while ((ch=getch()!=eof)
read a character ch
step5: close the file data.dat
step6: open the same file for reading
while((ch=get(f2)!=EOF)
display charecter on monitor
step7: close the data.dat
step8:stop
FILE *f2
While
((Ch==g
etchar()
)!=EOF)
Putc(ch,f2)
Close (f2)
While
((Ch==
getc())!
==EOF)
Close (f2)
5) Result:
Gurunanak Engineering College, Ibrahimpatnam, RR Dist.
Gurunanak Engineering College, Ibrahimpatnam, RR Dist.
1) AIM: Program to write integer data into file and read it from file
2) ALGORITHM:
step1: start
step2: take a number and define a file pointer
step3: open a file data.dat for writing
step4: read on integer and also read aninter into file
step5: close the file data.dat
step6: open the same file for reading
display an integer
step7: stop
Start
FILE *f2
Read num
Putw( num,f2)
Close (f2)
Num=getw(f2)
Display Num
Close (f2)
5) Result:
12
12
5) Result:
Start
FILE *f2
Enter name,price,quality
b=p*q
Display c,p,q.b
Close (f2)
Stop
FILE *f2
3) FLOWCHART:
Program to use command line I=0
arguments in files
If
I<argc
Close (f2)
I=0
If
I<argc
Close (f2)
CH Ramesh Asst Prof Dept of IT Page 123
Stop
4) PROGRAM:
Program to use command line arguments in files
#include<stdio.h>
main(argc,argv)
{
char word[10],*argv[];
int i,argc;
FILE *f2;
f2=fopen("command.dat","w");
for(i=1;i<argc;i++)
fprintf(fp,"%s",argv[i]);
fclose(fp);
f2=fopen("command.dat","r");
for(i=1;i<argc;i++)
{
fscanf(fp,"%s",word);
}
fclose(fp);
}
3) PROGRAM:
Stack operations using arrays
#include<stdio.h>
#define max 10
void push();
void pop();
void display();
int s[max];
int top=0;
void main()
{
char ch;
int choice;
do
{
printf("enter choice of operation");
printf("1.push(),2.pop(),3.display()");
scanf("%d",&choice);
switch(choice)
{
case1:
break;
case3:
display();
break;
default:
printf("invalid option");
}
printf("do u wantto continue y/n");
fflush(stdin);
scanf("%c",&ch);
}
while(ch=='y'||ch=='y')
}
void push()
{
int item;
if(top>=max)
printf("stackisfull");
else
{
printf("enter any item");
scanf("%d",&item);
top++;
{
int item;
if(top==0)
printf("stack is empty");
else
{
item=s[top];
printf("the related elemnt is %d",item);
top--;
}
}
void display()
{
int item;
int i;
if(top==0)
printf("\n stack is empty no element isdisplayed");
else
{
printf("\n%d\n",s[i]);
printf("\n----\n");
}
15150
----
do u wantto continue y/nn
step2:[delete element]
if(front==0)
front=1;
}
void delete()
{
int item;
if(front==0)
printf("queue underflow");
else
{
item=cq[front];
printf("the deleted element id %d",item);
}
if(front==rear)
{
front=0;
rear=0;
return;
}
if(front==max)
front=1;
else
front=front+1;
}
void dispaly()
{
5) Result:
1) Insert 2) Delete 3) Display
Enter choice for circular queue 1
Enter any item 14
1) Insert 2) Delete 3) Display
Enter choice for circular queue 1
Enter any item 15
1) Insert 2) Delete 3) Display
Enter choice for circular queue 1
Enter any item 20
1) Insert 2) Delete 3) Display
Enter choice for circular queue 3
14 15 20
2) PROGRAM 43:
#include<stdio.h>
#include<conio.h>
#define MAX 100
void push(char);
char pop();
int top=-1;
char stack[MAX];
void main()
{
char A,infix[100],post[100],x,ch;
int i,j=0;
clrscr();
printf("Enter the Infix expression.....\n");
gets(infix);
push('(');
for(i=0;(x=infix[i])!='\0';i++)
{
ch=')';
if((x>='A')&&(x<='Z'))
post[j++]=x;
else
if(x=='(')
push(x);
else
if(x==')')
{
while(ch!='(')
{
ch=pop();
post[j++]=ch;
}
j--;
}
else
{
while(prec(x)<=prec(stack[top]))
{
ch=pop();
post[j++]=ch;
post[j]='\0';
printf("The Postfix Expression is.....\n");
puts(post);
getch();
}
int prec(char y)
{
int k;
switch(y)
{
case '+':k=1;
break;
case '-':k=1;
break;
case '*':k=2;
break;
case '/':k=2;
break;
case '^':k=3;
break;
case '(':k=0;
break;
}
return(k);
}
void push(char item)
{
if(top==MAX)
{
printf("OverFlow");
return;
}
else
{
top=top+1;
stack[top]=item;
}
return;
}
else
{
item=stack[top];
top=top-1;
return item;
}
}
4) Result:
2) PROGRAM:
#include<conio.h>
#define MAX 100
void push(int);
int pop();
int top=-1,f=0,i;
int stack[MAX];
void main()
{
char post[100],x;
int value, a,b;
clrscr();
printf("Enter the Postfix Expression....");
gets(post);
for( i=0;(x=post[i])!='\0';i++)
{
if(isdigit(x))
{
push(x-'0');
}
else
{
a=pop();
b=pop();
value=perform(x,a,b);
push(value);
}
}
gets(post);
for(i=0;(x=post[i])!='\0';i++)
{
if(isdigit(x))
{
push(x=0);
}
else
{
a=pop();
b=pop();
value=perform(x,a,b);
top=top-1;
return item;
}
}
3) Result: 1
1.Enter the Postfix expression 654*+
The value of the Postfix expressin is: 26
Result: 2
int st[100];
int st_top=-1;
/*main function*/
void main()
{
char in[100],post[100];
clrscr();
printf("\n\tEnter the Infix Expression: ");
gets(in);
in_post(in);
getch();
}
/*end main*/
int pop_item()
{
int it;
if(st_top==-1)
{
for(z=0;z<y;z++)
printf("%c",post[z]);
printf("\n\nDo you want to evaluate the Result of Postfix Expression?(Y/N):");
scanf("%c",&a);
if(a=='y' || a=='Y')
{
result=cal(post);
printf("\n\n\tResult is: %d\n",result);
getch();
}
else if(a=='n' || a=='N')
{
exit(0);
}
}
switch(post[j])
{
case '+':x=n+m;
break;
case '-':x=n-m;
break;
case '*':x=n*m;
break;
case '/':x=n/m;
break;
}
push_item(x);
}
j++;
}
if(st_top>0)
{
printf("Number of Operands are more than Operators.");
exit(0);
4) Result:
Enter the Infix Expression: a+b*c
print(list->next);
}
return;
}
int count(node *list)
{
if(list->next==null)
return(0);
else
return(1+count(list->next));
}
node insert(node *head)
{
node *find(node *p,int a);
node *new,*n1;
int key,x;
printf("enter value of new item\n");
scanf("%d",&x);
printf("value of key item before which item is inserted?-999 if it is lost");
scanf("%d",&key);
if(head->number==key)
{
new=(node*)malloc(sizeof(node));
new->number=x;
new->next=head;
{
n1=find(head,key);
if(n1==null)
printf("key is not found");
else
{
new=(node*)malloc(sizeof(node));
new->number=x;
new->next=n1->next;
n1->next=new;
}
}
return(head);
}
node *find(node *list,int key)
{
if(list->next->number==key)
return(list);
else
if(list->next->next==null)
return(null);
else
find(list->next,key);
}
node *n1,*p;
printf("enter the num to be deleted");
scanf("%d",&key);
if(head->number==key)
{
p=head->next;
free(head);
head=p;
}
else
{
n1=find(head,key);
if(n1==null)
printf("\nkey not found");
else
{
p=n1->next->next;
free(n1->next);
n1->next=p;
}
}
return(head);
}
printf("1.traverse\n");
printf("2.insert\n");
printf("3.delete\n");
while(ch=='y')
{
printf("enter u rchoice\n");
scanf("%d",&choice);
switch(choice)
{
case1:printf("the element in the list are\n");
traverse();
break;
case2:insert();
break;
case3:delete();
break;
}scanf("%c",&ch);
}
}
voidcreate()
{
int no;
struct node *temp;
printf("enter the num \n");
temp->info=no;
temp->rptr=null;
if(head==null)
{
head=temp;
current=temp;
}
Else
{
current->rptr=temp;
temp->lptr=current;
current=temp;
}
}
voidtraverse()
{
struct node *t1=head;
if(t1==null)
printf("\n");
else
for(;t1!=null;t1->rptr)
{
printf("5d\n",t1->info);
else
{
printf("enter the no to insert \n");
scanf("%d",&no);
printf("1.insert at begining \n");
printf("2.insert at end \n");
printf("3.insert at middle \n");
printf("enter u r option \n");
scanf("%d",&option);
new=(struct node*)malloc(sizeof(struct(node));
new->lptr=null;
new->info=no;
new->rptr=null;
head=new;
break;
case2:
for(;t->rptr!=null;t2=t2->rptr)
new->lptr=t2;
t2->rptr=new;
break;
case3:
printf("enter the elements after which u want to insert \n");
scanf("%d",&p);
for(;t2!=null && t2->info!=p;t2=t2->rptr)
if(t2=null)
{
printf("elements not found \n");
}
else
{
new->rptr=t2->rptr;
t2->rptr->lptr=new;
t2->prtr=new;
new->lptr=t2;
}
4) Result:
enter the max no.of elements u wanna sort
5
enter the elements u want to sort
10 20 15 6 40
6 10 15 20 40
4) Result:
enter the max no.of elements u wanna sort
5
enter the elements u want to sort
10 20 15 6 40
6 10 15 20 40
4) Result:
Enter the max no.of elements u want to sort
5
Enter the elements u want to sort
10 20 15 6 40
6 10 15 20 40
2) ALGORITHM:
step1: take first a list of unsorted values
step2: take firstelement as 'pivot'
step3: keep the firstelement as 'pivot' and correct its position in the list
step4: divide the list into two based on first element
step5: combine the list
3) PROGRAM:
Program to implement Quick sort
#include<stdio.h>
main()
{
int a[10],i,left,right,n;
int min,loc;
clear();
printf("\n enter the max no.of elements u wanna sort \n");
scanf("%d",&n);
printf("\n enter the elements u want to sort \n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
left=0;
right=n-1;
quicksort(a,left,right);
4) Result:
enter the max no.of elements u wanna sort
5
enter the elements u want to sort
10 20 15 6 40
6 10 15 20 40
2) PROGRAM:
Program to implement Heap sort
#include<stdio.h>
main()
{
int a[10],i,j,n;
int min,loc;
clear();
printf("\n enter the max no.of elements u wanna sort \n");
scanf("%d",&n);
printf("\n enter the elements u want to sort \n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
heapsort(a,n);
display(a,n);
}
heapsort(inta[],int n)
{
int temp,i,key,q;
create heap(a,n);
for(q=n;q>2;q--)
{
3) Result:
enter the max no.of elements u wanna sort
5
enter the elements u want to sort
10 20 15 6 40
3) Result:
enter the max no.of elements u wanna sort
5
enter the elements u want to sort
12345
enter the value to be searched
3
search is successful
the elemnts is 3
3) Result:
Enter number of elements : 5
Enter 5 elements
15 35 62 45 11
enter the value to be seached
62
62 element is found at location 3
REFERENCES:
4. C & Data Structures – Prof. P.S.Desh Pande, Prof. O.G.Kakde, Wiley Dreamtech Pvt.Ltd