Вы находитесь на странице: 1из 77

ROOTS OF A QUADRATIC EQUATION

3.Develop a program to compute the roots of a


quadratic equation by accepting the coefficients. Print
appropriate messages.

Algorithm:

steps:
1.Start
2.Read the values of the coefficient a,b and c
3.Check whether if(a==b==0)
4.Print cannot find the roots. Goto stop
5. if(a==0) then print linear equation go to stop
6.Find the value of d
d=b*b-4*a*c
7.find the roots of quadratic equation
if(d==0).Print ‘the roots are equal and the value of the roots are
r1=r2= -b/(2.0*a) else stop
8.else if(d>0) then write ‘the roots are real and distinct’.the
roots are;
r1=(-b+sqrt(d))/(2.0*a);
r2=(-b-sqrt(d))/(2.0*a);
stop
9.else if(d<0) then write ‘the roots are complex and the
roots are;
r1=a+ib
r2=a-ib
stop
10.stop
FLOWCHART :
Start

Input a,b,c

d=b*b - 4*a*c

if d==0 r1=-b/(2.0*a)
r2=-b/(2.0*a)

if d>0 r1=(-b+sqrt(d))/(2.0*a)
r2=(-b-sqrt(d))/(2.0*a)

Outer complex roots Output r1 and r2

stop
PROGRAM :
#include<stdio.h>
#include<math.h>
main()
{
float a,b,c,d,r1,r2,r;
printf("enter the coefficients a,b and c\n");
scanf("%f %f %f",&a,&b,&c);
if(a*b*c==0)
{
printf("invalid inputs\n");
}
else if(a==0)
{
printf(“linear equation\n”);
}
else
{
d=b*b-4*a*c;
r=sqrt(fabs(d));
if(d>0)
{
r1=(-b+r)/(2.0*a);
r2=(-b-r)/(2.0*a);
printf("roots are real and distinct\n");
printf("r1=%f\n,r2=%f\n",r1,r2);
}
else if(d==0)
{
r1=-b/(2.0*a);
r2=-b/(2.0*a);
printf("roots are real and equal\n");
printf("r1=%f\n,r2=%f\n",r1,r2);
}
else if(d<0)
{
r1=-b/(2.0*a);
r2=r/(2.0*a);
printf("roots are complex\n");
printf("r1=%f+i%f\n",r1,r2);
printf("r2=%f-%f\n",r1,r2);
}
}
}

OUTPUT :
1.enter the coefficient a,b,c
296
the roots are real and distinct
r1= -0.813859
R2= -3.686141

2.enter the coefficient a,b,c


8,10,66
the roots are complex
r1= -0.625000+i2.803458
r2=-0.625000-i2.803458

3.enter the coefficient a,b,c


4,4,1
the roots are equal
r1= -0.500000
r2= -0.500000
TRACING :
Enter the coefficient of a,b and c

a=4
b=4
c=1

if(4*4*1==0) /*this statement is false hence comes out of if loop*/

d = 4*4 – 4*4*1
d=0

r = sqrt(fabs(d))

if(0>0)
/*this statement is false hence comes out of if loop*/

else if(0==0)
/*this statement is true hence else if loop is executed */

{
r1= -4/(2.0*4)
r1=-0.500000

r2= -4/(2.0*4)
r2 = -0.500000

the following program prints

the roots are equal

r1 = 0.500000
r2 = 0.500000
PALINDROME
4.Develop a program to find the reverse of a positive
integer and check for palindrome or not. Display
appropriate messages.

ALGORITHM :
Steps:

1.start
2.input n
3.num=n
4.while(num!=0)
rem←num%10
num←num/10
rev←rev*10+rem
[end while]
write ‘the reverse of the number is rev’
if(rev==num)
print ‘the number is palindrome’
else ‘the number is not a palindrome’
5.Stop
FLOWCHART :
start

input n

num=n

while(num!=0)

rem←num%10
num←num/10
rev←rev*10+rem

print the reverse of the


number is rev

If print the number is


rev==n not a palindrome

print the number


is palindrome

stop
PROGRAM :
#include<stdio.h>
int main()
{
int n,num,rem,rev;
printf("enter a number\n");
scanf("%d",&num);
rev=0;
n=num;
if(num<0)
{
printf(“invalid inputs\n”);
}
while(num!=0)
{
rem=num%10;
num=num/10;
rev=(rev*10)+rem;
}
printf("the reverse of %d is %d",n,rev);
if(rev==n)
{
printf("\n%d is a palindrome",n);
}
else
{
printf("\n%d is not a palindrome",n);
}
} OUTPUT :
enter the number
1567
the reverse of the number is 7651
1567 is not a palindrome

enter the number


1991
the reverse of the number is 1991
1991 is a palindrome
TRACING :
Enter a number
1991
rev=0
n=1991
while(1991!=0)
rem=1991%10 rem = 1
num=1991/10 num = 199
rev = (0*10)+1 rev = 1
/*this while loop is executed until num becomes equal to 0*/

while(199!=0)
rem=199%10 rem = 9
num=199/10 num = 19
rev = (1*10)+9 rev = 19

while(19!=0)
rem=19%10 rem = 9
num=19/10 num = 1
rev = (19*10)+9 rev = 199

while(1!=0)
rem=1%10 rem = 1
num=1/10 num = 0
rev = (199*10)+1 rev = 1991

while(0!=0)
/*this statement is false hence comes out of loop*/

it prints the following

the reverse of 1991 is 1991


if(1991==1991)
/*this statement is true*/

hence it prints 1991 is a palindrome


ELECTRICITY BILL
5.An electricity board charges the following rates for the
use of electricity:for the first 200 units 80 paise per
unit:for the next 100 units 90 paise/unit:beyond 300 units
rs 1 per unit. All users are charged in minimum of rs 100
as meter charge. If the total amount is more than rs 400
then an additional surcharge of 15% of total amount is
charged. Write a program to read a name of the
user,number of units consumed and print out the
charges.

ALGORITHM :
steps:

1.start
2.read name,unit and amount
3.check whether if(unit<=200)
then write amount=unit*0.8
4.else if (unit<=300)
write amount=(unit- 200)*0.9+160
5.else if(unit>300)
amount=(unit-300)*1+250
6.else amount=100
7.if (amount>400)
equate charge=amount*0.5
8.print name,unit,amount,charge,amount due=amount+charge
9.stop
FLOWCHART : start

Print name and unit

unit<=200 amount=unit*0.8

unit<=300 amount=(unit-200)*0.9+160

unit>300 amount=(unit-300)*1+250

amount=100

amount>400 charge=amount*0.5

Print amount,charge,amount due=amount+charge

stop
PROGRAM :

#include<stdio.h>
int main()
{
char name[20];
float amount,unit,charge=0.0;
printf("name and units consumed\n");
scanf("%s%f",name,&unit);

if(unit<=200)
amount=unit*0.8;

else if(unit<=300)
amount=(unit-200)*0.9+160;

else if(unit>300)
amount=(unit-300)*1+250;

else
amount=100;

if(amount>400)
charge=amount*0.15;

printf("name=%s\n",name);
printf("units consumed=%f\n",unit);
printf("amount=%f\n",amount);
printf("charge=%f\n",charge);
printf("amount due:%f\n",amount+charge);
return 0;
}
OUTPUT :

1.name and units consumed


xyz
350
name=xyz
units consumed=350.000000
amount=300.000000
charge=0.000000
amount due:300.000000

2.name and units consumed


abc
530
name=abc
units consumed=530.000000
amount=480.000000
charge=72.000000
amount due=552.000000
TRACING :

name and unit consumed

xyz
350

if(350<=200)
/*this statement is false hence comes out */

else if(350<=300)
/*this statement is false hence comes out */

else if(350>300)
/*this statement is true hence it is executed*/

amount = (350 – 300) * 1 + 250


amount = 300.000000

if(300>400)
/*this statement is false hence comes out */

it prints the following data


name = xyz
units consumed = 350.0000
amount = 300.000000
charge = 0.000000
amount due = amount + charge = 300.000000+0.000000
amount due = 300.000000
BINARY SEARCH
6.Introduce 1D array manipulation and implement binary search.

ALGORITHM :

steps:
1.start
2.enter the number of names to read i.e., enter n and
initialize found=0
3.enter the names in ascending order i.e., name[i]
4.enter the name to be searched i.e.,enter key
5.initialize low=0 and high=n-1
6.check: low<=high&&!found
7.if true,compute mid=(low+high)/2
check if(strcmp(name[mid],key)==0)
then found=1
else if(strcmp(name[mid],key)<0)
low=mid+1
else
high=mid-1
8.if false check if(found==1)
then print position of the name where it is found
else
print name not found
9.stop.
START
FLOWCHART :

ENTER THE NUMBER OF NAMES


TO READ I.E.,N

FOUND=0

i++ i=0
i<n

ENTER THE NAMES IN ASCENDING ORDER I.E.,NAME[i]

ENTER THE NAME TO BE SEARCHED

LOW=0
HIGH=N-1

WHILE
LOW<=HIGH&&!FOUND

mid=(low+high)/2

IF
STRCMP(NAME[MID],KEY)==0 FOUND=1

IF
STRCMP(NAME[MID],KEY)<0 LOW=MID+1
HIGH=MID-1

FOUND==1 PRINT THE POSITION OF


THE NAME FOUND

PRINT NAME NOT FOUND

STOP
PROGRAM :
#include<stdio.h>
#include<string.h>
char name[10][20],key[20];
int i,n,low,high,mid,found=0;
int main()
{
printf("enter the number of names to read \n");
scanf("%d",&n);
printf("enter the names in ascending order\n");
for(i=0;i<n;i++)
scanf("%s",name[i]);
printf("enter the name to be searched\n");
scanf("%s",key);
low=0;
high=n-1;
while(low<=high&&!found)
{
mid=low+high/2;
if(strcmp(name[mid],key)==0)
found=1;
else if(strcmp(name[mid],key)<0)
low=mid+1;
else
high=mid-1;
}
if(found==1)
printf("name found in position %d",mid+1);
else
printf("name not found");
}
OUTPUT :

enter the number of names to read


3
enter the names in ascending order
1
2
3
enter the name to be searched
1
name found in position
1

enter the number of names to read


3
enter the names in ascending order
cat
hat
rat
enter the name to searched
cat
name found in position
1
TRACING :
enter the number of names to read:3
enter the names in ascending order
for(i=0;0<3;i++) /*the statement is true*/
1
for(i=1;1<3;i++)
2
for(i=2;2<3;i++)
3
for(i=3;3<3;i++) /*the statement is false so comes out of
loop*/
enter the name to be searched:1
low=0 and high=3-1=2
while(0<=2&&!0)
while(1&&1) /*this is true hence the instructions are executed*/
mid=0+2/2=1
if(strcmp(name[1],1)==0)
if(strcmp(2,1)==0) /*false so comes out of loop*/
else if(strcmp(2,1)<0) /*false so comes out of loop*/
so high=1-1=0
while(0<=0&&1)
{
mid=0+0/2=0
if(strcmp(1,1)==0) /*true*/
found=1
}
if(found==1)
if(1==1)
print name found in position mid+1=1
CONDITIONAL CHECK:PRIME NUMBER
7.Implement using the functions to check whether the
given number is prime and display appropriate
messages (No built-in functions).

ALGORITHM

STEP 1 : Start.
STEP 2 : Input a number.
STEP 3 : Check if isprime(n)
if it is true then print given number is prime
number
if it is false then print given number is not a
prime number
STEP 4 : Stop

FUNCTION : isprime(n)
for i=2 check i <= n/2
if true check if n%i = 0
if true return 0
then i=i+1
if false return 1
FLOWCHART : Start

Input a number

if
isprime(n)

true
Print the number is prime number

Stop

isprime(n)

read i

i=2 false
i++
i<=m/2
true

false if
m%i=0

true
return 0 return 1

Stop
PROGRAM :
#include<stdio.h>
int isprime(int m);
int main()
{
int n;
printf("enter a number\n");
scanf("%d",&n);

if(isprime(n))
printf("%d is a prime number",n);
else
printf("%d is not a prime number",n);
}

int isprime(int m)
{
int i;
for(i = 2;i <= m/2;i++)
{
if(m%i == 0)
{
return 0;
}
}
return 1;
}
OUTPUT :

1. enter a number
3
3 is a prime number

2. enter a number
6
6 is not a prime number
TRACING :

1. enter a number
n=3
if(isprime(3))

int isprime(3)
for(i=2 ;2<= 3/2 is true
if(3%2 == 0) is false
return 1

if(1)
3 is a prime number

2. enter a number
n=6
if(isprime(6))

int isprime(6)
for(i=2 ; 2 <= 6/2 is true
if(6%2 ==0) is true
return 0

i=3 ; 3<= 6/2 is true


if(6%3 ==0) is true
return 0

i=4 ; 4<=6/2 is false

if(0)
6 is not a prime number
MATRIX MULTIPLICATION
8.Develop a program to introduce 2D array manipulation
and implement Matrix multiplication and ensure the
rules of multiplication are checked.

ALGORITHM
STEP 1 : Start
STEP 2 : Enter the size of first and second matrix that
is m,n and p,q respectively.
STEP 3 : check if n is not equal to p
STEP 4 : If true , print matrix multiplication is not possible.
if false, go to STEP 5
STEP 5 : for i <--- 0 to m
for j <--- 0 to n
enter the elements of first matrix
STEP 6 : for j <--- 0 to q
for i <--- 0 to p
enter the elements of second matrix
STEP 7 : for i <--- 0 to m
for j <--- 0 to q
initialise c[i][j] = 0
STEP 8 : Compute c[i][j] = a[i][k] * b[k][j]
STEP 9 : Print the resultant matrix
STEP 10 : Stop
FLOWCHART : Start

read m,n
and p,q

if T Print matrix
multiplication
n!=p
is not possible
F
i=0 F
i++
i<m
T
F j=0
j++
j<n
T

read a[i][j]

j=0 F
j++
j<q
T
F i=0
i++
i<p
T

Read b[i][j]

a
a

i=0 F
i++
i<m
T
F j=0
j++
j<q
T
c[i][j] =0

F k=0
k++
k<n
T

c[i][j]=c[i][j]+a[i][k]*b[k][j]

i=0 F
i++
i<m
T
F j=0
j++
j<q
T

print c[i][j]

Stop
PROGRAM :
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[5][5],b[5][5],c[5][5],m,n,p,q,i,j,k;
printf("enter the size of first matrix\n");
scanf("%d %d",&m,&n);
printf("enter the size of second matrix\n");
scanf("%d %d",&p,&q);

if(n != p)
{
printf("matrix multiplication is not possible\n");
exit(0);
}

printf("enter the elements of first matrix\n");


for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}

printf("enter the elements of second matrix\n");


for(j=0;j<q;j++)
{
for(i=0;i<p;i++)
{
scanf("%d",&b[i][j]);
}
}

for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
} }
}

printf("the resultant matrix\n");


for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}

OUTPUT :
1. enter the size of first matrix
3 2
Enter the size of second matrix
3 2
Matrix multiplication is not possible
2. enter the size of first matrix
2 3
Enter the size of second matrix
3 2
Enter the elements of first matrix
5 4 6 2 1 3
Enter the elements of second matrix
5 4 6 2 1 3
The resultant matrix is
77 32
32 14
TRACING :
1. enter the size of first matrix
m=3,n=2
enter the size of second matrix
p=3,q=2
if(2!=3) is true
matrix multiplication is not possible

2. enter the size of first matrix


m=2,n=3
Enter the size of second matrix
p=3,q=2

if(3!=3) is false

enter the elements of first matrix


for( i = 0 ; 0 < 2 is true
for( j = 0 ; 0 < 3 is true
a[0][0] = 5
j = 1 ; 1 < 3 is true
a[0][1] = 4
j=2 ; 2 < 3 is true
a[0][2] = 6

j=3 ; 3 < 3 is false

i=1 ; 1 < 2 is true


for(j=0 ; 0 < 3 is true
a[1][0] = 2

j =1 ; 1 < 3 is true
a[1][1] = 1

j = 2 ; 2 < 3 is true
a[1][2] = 3

j = 3 ; 3 < 3 is false
i = 2 ; 2 < 2 is false
enter the elements of second matrix
for(j = 0 ; 0 < 2 is true
for(i = 0 ; 0 < 3 is true
b[0][0] = 5
i = 1 ; 1 < 3 is true
b[1][0] = 4
i = 2 ; 2 < 3 is true
b[2][0] = 6
i = 3 ; 3 < 3 is false

j = 1 ; 1 < 2 is true
for(i = 0 ; 0 < 3 is true
b[0][1] = 2
i = 1 ; 1 < 3 is true
b[1][1] = 1
i = 2 ; 2 < 3 is true
b[2][1] = 3
i = 3 ; 3 < 3 is false
j = 2 ; 2 < 2 is false

for(i = 0 ; 0 < 2 is true


for(j = 0 ; 0 < 2 is true
c[0][0] = 0
for(k = 0 ; 0 < 3 is true
c[0][0] = c[0][0] + a[0][0] *b[0][0]
=0 +5*5
= 25

k = 1 ; 1 < 3 is true
c[0][0] = c[0][0] + a[0][1] * b[1][0]
= 25 +4*4
= 41

k = 2 ; 2 < 3 is true
c[0][0] = c[0][0] + a[0][2] * b[2][0]
= 41 +6*6
c[0][0] = 77
k = 3 ; 3 < 3 is false

j = 1 ; 1 < 2 is true

c[0][1] = 0
for(k=0 ; 0 < 3 is true
c[0][1] = c[0][1] + a[0][0] * b[0][1]
=0 +5*2
= 10

k = 1 ; 1 < 3 is true
c[0][1] = c[0][1] + a[0][1] * b[1][1]
= 10 +4*1
= 14

k=2 ; 2<3 is true


c[0][1] = c[0][1] + a[0][2] * b[2][1]
= 14 +6*3
c[0][1] = 32
k=3 ; 3 < 3 is false
j = 2 ; 2 < 2 is false
i = 1 ; 1 < 2 is true
for(j = 0 ; 0 < 2 is true

c[1][0] = 0
for(k=0 ; 0< 3 is true
c[1][0] = c[1][0] + a[1][0] * b[0][0]
= 0 +2*5
= 10

k = 1 ; 1<3 is true
c[1][0] = c[1][0] + a[1][1] * b[1][0]
= 10 +1* 4
= 14

k=2 ; 2< 3 is true


c[1][0] = c[1][0] + a[1][2] * b[2][0]
= 14 + 3*6
c[1][0] = 32
k=3 ; 3< 3 is false

j = 1 ; 1< 2 is true

c[1][1] =0
for(k=0 ; 0< 3 is true
c[1][1] = c[1][1] + a[1][0] * b[0][1]
= 0 + 2*2
=4

k=1 ; 1< 3 is true


c[1][1] = c[1][1] + a[1][1] * b[1][1]
=4 +1*1
=5

k=2 ; 2< 3 is true


c[1][1] =c[1][1] + a[1][2] * b[2][1]
=5 +3*3
c[1][1] = 14

k=3 ; 3<3 is false


j=2 ; 2<2 is false
i=2 ; 2<2 is false

the resultant matrix is


for(i=0 ; 0 < 2 is true
for(j=0 ; 0 < 2 is true
c[0][0] = 77

j=1 ; 1< 2 is true


c[0][1] = 32
j=2 ; 2<2 is false
i=1 ; 1< 2 is true
for(j=0 ; 0<2 is true
c[1][0] = 32
j=1 ; 1< 2 is true
c[1][1] = 14
j=2 ; 2< 2 is false
i=2 ; 2< 2 is false
TAYLOR SERIES
9. Develop a Program to compute Sin(x) using Taylor series
approximation. Compare your result with the built-in
library function. Print both the results with appropriate
messages
ALGORITHM :

STEP 1 : Start.
STEP 2 : Read the value of x and n
STEP 3 : rad <-- x * 3.14 / 180
STEP 4 : res <-- mysine(rad,n)
STEP 5 : Print the result obtained using Taylor series.
Also print the result of sine function using
built-in library function.
STEP 6 : Stop

FUNCTION : mysine(rad,n)
read i
sum = 0
for i <-- 1 to n do
if((i-1) % 4 == 0)
sum <-- sum + ( pow(x,i) / fact(i) )
else
sum <-- sum – ( pow(x,i) / fact(i) )
return sum

FUNCTION : fact(n)
if(n == 0)
return 1
else
return (n * fact(n-1) )
FLOWCHART : Start

Read x,n

rad <-- x * 3.14 / 180


res <-- mysine(rad,n)

print the value using library function


and without using library function

Stop

mysine(rad,n)

Read i,sum = 0

i=1 F
i+=2
i<=n

T if F
(i-1)%4 == 0

sum = sum + xi / i! sum = sum + xi / i!

return sum
fact(n)

if F
n == 0

T
return 1 return n * fact(n-1)
PROGRAM :
#include<stdio.h>
#include<math.h>
float mysine(float x,int n);
int fact(int n);

void main()
{
int x,n;
float rad,res;
printf("enter the values of degree and number of turns\n");
scanf("%d %d",&x,&n);
rad=(x*3.14)/180;
res=mysine(rad,n);
printf("my defined sine function(%d)=%f\n",x,res);
printf("\n using library function sine(%d)=%f\n",x,sin(rad));
}

float mysine(float x,int n)


{
int i;
float sum=0;
for(i=1;i<=n;i+=2)
{
if((i-1)%4==0)
sum=sum+(pow(x,i)/fact(i));
else
sum=sum-(pow(x,i)/fact(i));
}
return sum;
}

int fact(int n)
{
if(n==0)
return 1;
else
return (n*fact(n-1));
}
OUTPUT :

1. Enter the values of degree and number of terms


90 2
my defined sine function (90) = 1.570000
using library function sine(90) = 1.000000

2. Enter the values of degree and number of terms


30 15
my defined sine function (30) = 0.499770
using library function sine(30) = 0.499770
TRACING :
1. Enter the values of degree and number of terms
x = 90 n = 2
rad = (90 *3.14) / 180
rad = 1.570000
res = mysine(1.570000,2)

mysine(1.570000,2)
sum = 0
for(i = 1 ; 1<=2 is true
if((1-1)%4 == 0) is true
sum = 0 + (1.570000)1 / fact(1) )

fact(1)
if(1 == 0 is false
return 1 * fact(0)

fact (0)
if(0 == 0 is true
return 1

fact(1)
return 1*1

sum = 0 + (1.570000 / 1)
sum = 1.570000

i = 1+2 = 3 ; 3 <= 2 is false


return sum

my defined sine function (90) = 1.570000


using library function sine(90) = 1.000000
STRING OPERATIONS
10. Write functions to implement string operations such as
compare,concatenate,string length. Convince the
parameter passing techniques.
ALGORITHM:
STEP 1 : Start
STEP 2 : Enter the first and second string a and b
STEP 3 : using compare_strings, compare both the strings
STEP 4 : print the length of strings a and b by using
string_length.
STEP 5 : Concatenate the strings using concatenate_strings
and print the concatenated string.
STEP 6 : Print the length of concatenated string
STEP 7 : Stop

FUNCTION : string_length
count = 0
check while(a[count] != ‘\0’)
if true, count++
return count

FUNCTION : compare_strings
c=0;
check while(a[c]==b[c])
if true, if(a[c]=='\0' && b[c]=='\0')
if true,print equal strings
c++
if false, print unequal strings

FUNCTION : concatenate_strings
i,j=0;
for(i <- 0 to a[i] != '\0'

for(j <- 0 to b[j] != '\0'


a[i]=b[j];

a[i]='\0';
FLOWCHART :
Start

Enter first string a


and second string b

compare_strings(a,b)

Print the length of


strings a and b, string_length(s)

concatenate_strings(a,b)

Print the length of


concatenated string

Stop
PROGRAM :
#include<stdio.h>
#include<stdlib.h>

int compare_strings(char a[],char b[]);


int concatenate_strings(char a[],char b[]);
int string_length(char s[]);

int main()
{
char a[20],b[20];
printf("enter the first string\n");
scanf("%s",a);
printf("enter the second string\n");
scanf("%s",b);
compare_strings(a,b);
printf("\nlength of first string: %d",string_length(a));
printf("\nlength of second string: %d",string_length(b));
concatenate_strings(a,b);
printf("\nlength of resultant string after concatenation:
%d\n",string_length(a));
}

int compare_strings(char a[],char b[])


{
int c=0;
while(a[c]==b[c])
{
if(a[c]=='\0' && b[c]=='\0')
{
printf("\nequal strings\n");
return 0;
}
c++;
}
printf("\nunequal strings\n");
return 0;
}

int concatenate_strings(char a[],char b[])


{
int i,j;
for(i=0;a[i]!='\0';++i);
for(j=0;b[j]!='\0';++j,++i)
{
a[i]=b[j];
}
a[i]='\0';
printf("\nafter concatenation:%s\n",a);
return 0;
}

int string_length(char s[])


{
int i=0;
while(s[i]!='\0')
{
i++;
}
return i;
}
OUTPUT :
1. enter the first string
stone
Enter the second string
ring

unequal strings

length of first string: 5


length of second string: 4
after concatenation:stonering

length of resultant string after concatenation: 9

2. enter the first string


whole
Enter the second string
whole

equal strings

length of first string: 5


length of second string: 5
after concatenation:wholewhole

length of resultant string after concatenation: 10


TRACING :
1. Enter the first string
a[20] = stone
Enter the second string
b[40] = ring
compare_strings(stone,ring)

compare_strings(stone,ring)
c=0
while(a[0] == b[0]
s == r is false
unequal strings

length of first string : , string_length(stone)

string_length(stone)
i=0
while( s[0]!=’\0’ is true
i=1
s[1] != ‘\0’ is true
i=2
s[2] != ‘\0’ is true
i=3
s[3] != ‘\0’ is true
i =4
s[4] != ‘\0’ is true
i=5
s[5] != ‘\0’ is false

return 5

length of first string : 5


// same for second string
concatenate_strings(stone,ring)

i = 0 ;a[0]!= ‘\0’ is true


j = 1 ; b[1] != ‘\0’ is true
j = 0 ; b[0] != ‘\0’ is true ; i = 5
a[5] = b[0] = r
j = 1 ; b[1] != ‘\0’ is true ; i = 6
a[6] = b[1] = i
j = 2 ; b[2] != ‘\0’ id true ; i = 7
a[7] = b[2] = n
j = 3 ; b[3] != ‘\0’ is true ; i = 8
a[8] = b[3] = g
j = 4 ; b[4] != ‘\0’ is false ; i = 9
a[9] = ‘\0’
after concatenation: stonering
// length of resultant string is calculated in same way as the
Original strings
SORTING : BUBBLE SORT
11. Develop a program to sort the given set of N numbers
using Bubble sort

ALGORITHM :

STEP 1 : Start
STEP 2 : Read n ,array a[i]
STEP 3 : print a[i]
STEP 4 : for i <-- 0 to n-2
j <-- 0 to n-2-i
check if a[j] > a[j+1]
if true , assign temp <-- a[j]
a[j] <-- a[j+1]
a[j+1] <-- temp
otherwise go to STEP 5
STEP 5 : Print the sorted array
STEP 6 : Stop
FLOWCHART : Start

Read n

i=0 F
i++
i <= n-1
T
Read a[i]

print the given


array

i=0 F
i++ i <= n-2
T
F j=0
j++ j <= n-2-i
T
if
F
a[j] > a[j+1]

T
temp <-- a[j]
a[j] <-- a[j+1]
a[j+1] <-- temp

print the sorted


array

Stop
PROGRAM :

#include<stdio.h>
#include<math.h>
void main()
{
int i,j,n,temp,a[20];
printf("enter the number of integer\n");
scanf("%d",&n);
printf("enter the integers\n");
for(i=0;i<=n-1;i++)
{
scanf("%d",&a[i]);
}
printf("the given array is\n");
for(i=0;i<=n-1;i++)
printf("%d\t",a[i]);
for(i=0;i<=n-2;i++)
{
for(j=0;j<=n-2-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\n the sorted array is\n");
for(i=0;i<=n-1;i++)
{
printf("%d\n",a[i]);
}
}
OUTPUT :

1. enter the number of integers


6
Enter the integers
5 9 2 1 1116 14
The given array is
5 9 2 1 1116 14
The sorted array is
1 2 5 9 14 1116

2. enter the number of integers


3
Enter the integers
36 10 42
The given array is
36 10 42
TRACING :
1. Enter the number of integers
n=3
Enter the integers
for( i = 0 ; 0 < 3 is true
a[0] = 36
i=1 ; 1 < 3 is true
a[1] = 10
i = 2 ; 2<3 is true
a[2] = 42
i= 3 ; 3<3 is false
the given array is
for( i = 0 ; 0<=2 is true
a[0] = 36
i = 1 ; 1<= 2 is true
a[1] = 10
i = 2 ; 2<=2 is true
a[2] = 42
i=3 ; 3<= 3 is false

for(i = 0 ; 0 <=3-2 is true


for(j = 0 ; 0 <=3-2-0 is true
if(a[0] > a[ 0+1 ]
36 > 10 is true

temp = a[0] = 36
a[0] = a[1] = 10
a[1] = 36

j = 1 ; 1 <= 1 is true
if(a[1] > a[2]
36 > 42 is false

j = 2 ; 2<= 1 is false

i = 1 ; 1 <= 1 is true
for(j = 0 ; 0<= 0 is true
if( a[0] > a[1]
10 > 36 is false

j = 1 ; 1<= 0 is false
i = 2 ; 2<= 1 is false

the sorted array is


for(i = 0 ; 0<= 3-1 is true
a[0] = 10

i = 1 ; 1<= 2 is true
a[1] = 36

i = 2 ; 2 <= 2 is true
a[2] =42

i = 3 ; 3 <= 2 is false
SQUARE ROOT
12. Develop aprogram to find the square root of a given
number N and execute for all possible inputs with
appropriate messages.
Note : Don’t use library function sqrt(n)

ALGORITHM :

STEP 1 : Start
STEP 2 : Enter a number num.
STEP 3 : Compute sqroot = num / 2
temp <-- 0
STEP 4 : Check while(sqroot != temp)
if true, then temp <-- sqroot
sqroot = (number / sqroot + sqroot ) / 2
STEP 5 : Stop

PROGRAM :

#include<stdio.h>
void main()
{
float number,sqroot,temp;
printf("enter a number\n");
scanf("%f",&number);
sqroot=number/2;
temp=0;
while(sqroot!=temp)
{
temp=sqroot;
sqroot=(number/sqroot+sqroot)/2;
}
printf("sqroot of %f is %f",number,sqroot);
}
FLOWCHART : Start

Read number

sqroot = number / 2
temp = 0

while F
sqroot != temp

temp = sqroot
sqroot = (number / sqroot + sqroot) / 2

print sqroot

Stop
OUTPUT :
1. Enter a number
4.000000
square root of 4.000000 is 2.000000

2. Enter a number
15.000000
square root of 15.000000 is 3.872983
TRACING :
2 . enter a number
number = 15

sqroot = 15 / 2
= 7.500000
temp = 0

while( 7.500000 != 0 is true


temp = 7.500000
sqroot = ( 15 / 7.5 + 7.5) /2
sqroot = 4.750000
while( 4.750000 != 7.500000 is true
temp = 4.750000
sqroot = ( 15 / 4.75 + 4.75 ) / 2
sqroot = 3.953947
while( 3.953947 != 4.750000 is true
temp = 3.953947
sqroot = ( 15 / 3.953947+ 3.953947 ) /2
sqroot = 3.873812
while( 3.873812 != 3.953947 is true
temp = 3.873812
sqroot = ( 15 / 3.873812 + 3.873812 ) / 2
sqroot = 3.872983
while( 3.872983 != 3.873812 is false

sqroot of 15.000000 is 3.872983


STRUCTURES
13.Implement Structures to Read,Write and Compute
Average-Marks and the students scoring above and
below the average marks for a class of N Students.
ALGORITHM :
STEP 1: Start
STEP 2: Define and Declare a Structure.
STEP 3: Input the number of Students
STEP 4: Read “N”
STEP 5: Input Roll Number, Name, Marks in 2 Subjects.
STEP 6: Total Marks <- Mark 1+Marks 2
STEP 7: Average Marks <- Total Marks / 2.0
STEP 8: Print Average Marks.
STEP 9: Call Function compute
STEP 10: Print Below Average Students.
STEP 11: for i <-0 , to less than n
if(s[i].avg<=mean)
Call Function display
STEP 12: Print Above Average Students
for i <- 0, to less than n
if (s[i]>mean)
Call Function post
STEP 13: Stop.

FUNCTIONS:

1. float compute(struct student s[],int n)


int i;
float sum=0;
for i <-0,less than n
sum <-sum+s[i].avg;
return sum/n;

2. void display(struct student s)


print Student Information

3. void post(struct student s)


print Student Information
FLOWCHART :
START

DEFINE AND
DECLARE A
STRUCTURE

INPUT “N” STUDENTS

INPUT
ROLL NUMBER,
NAME,
MARK1, MARK 2

TOTAL=MARK1+MARK2
AVERAGE= TOTAL /2.0

PRINT
AVERAGE MARKS

CALL COMPUTE FUNCTION

PRINT BELOW
AVERAGE STUDENTS

i++ i=0
i<n

if(s[i]<=avg)

a
a

CALL DISPLAY FUNCTION

PRINT ABOVE AVERAGE


STUDENTS

I++ i=0
i<n

if(s[i]>avg)

CALL POST FUNCTION

RETURN(0)

STOP

void display(struct student s)

PRINT STUDENT INFORMATION

void post (struct student s)

PRINT STUDENT INFORMATION


float compute(struct student s[],int n)

int i;
float sum=0;

I++ i=0
i<n

sum=sum+s[i].avg

return (sum/n)
PROGRAM :
#include<stdio.h>
struct student
{
int roll,marks1,marks2,total;
char name[20];
float avg;
}s[20];

float compute(struct student s[],int n)


{
int i;
float sum=0;
for(i=0;i<n;i++)
{
sum=sum+s[i].avg;
}
return (sum/n);
}

void display(struct student s)


{
printf("\n %d\t %s\t %d\t %d\t %d\t %f
\n",s.roll,s.name,s.marks1,s.marks2,s.total,s.avg);
}

void post(struct student s)


{
printf("\n %d\t %s\t %d\t %d\t %d\t %f
\n",s.roll,s.name,s.marks1,s.marks2,s.total,s.avg);
}

int main()
{
int i,n;
float mean,avg;
printf("Enter the Number of Students:");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("Enter the Rollnumber:\n");
scanf("%d",&s[i].roll);
printf("Enter the Name:\n");
scanf("%s", s[i].name);
printf("Enter the Marks in 2 Subjects:\n");
scanf("%d %d",&s[i].marks1,&s[i].marks2);
s[i].total=s[i].marks1+s[i].marks2;
s[i].avg=s[i].total/2.0;
printf("Average Marks:%f\n",s[i].avg);
}
mean=compute(s,n);
printf("Below Average Students:\n");
printf("Rollnumber: Name: Sub1: Sub2: Total: Average:");
for(i=0;i<n;i++)
{
if(s[i].avg<=mean)
display(s[i]);
}
printf("Above Average Students :\n");
printf("Rollnumber: Name: Sub1: Sub2: Total: Average:");
for(i=0;i<n;i++)
{
if(s[i].avg>mean)
post(s[i]);
}
return(0);

OUTPUT :
Enter the Number of Students:5
Enter the Rollnumber:
1
Enter the Name:
A
Enter the Marks in 2 Subjects:
100
100
Average Marks:100.000000
Enter the Rollnumber:
2
Enter the Name:
B
Enter the Marks in 2 Subjects:
100
100
Average Marks:100.000000
Enter the Rollnumber:
3
Enter the Name:
C
Enter the Marks in 2 Subjects:
75
75
Average Marks:75.000000
Enter the Rollnumber:
4
Enter the Name:
D
Enter the Marks in 2 Subjects:
75
75
Average Marks:75.000000
Enter the Rollnumber:
5
Enter the Name:
E
Enter the Marks in 2 Subjects:
25
25
Average Marks:25.000000
Below Average Students:
Rollnumber: Name: Sub1: Sub2: Total: Average:
3 C 75 75 150 75.000000

4 D 75 75 150 75.000000

5 E 25 25 50 25.000000
Above Average Students :
Rollnumber: Name: Sub1: Sub2: Total: Average:
1 A 100 100 200 100.000000

2 B 100 100 200 100.000000


TRACING :
STANDARD DEVIATION
14. Develop a program using pointers to compute the sum,
mean and standard deviation of all elements stored in
an array of n real numbers

ALGORITHM :

STEP 1 : Start
STEP 2 : Enter the number of terms : n, elements: a[i],
STEP 3 : Assign ptr <-- a
STEP 4 : Compute sum = sum + *ptr
ptr++
STEP 5 : Compute mean = sum / n
ptr <-- a
STEP 6 : sumstd <-- sumstd + (*ptr – mean)2
ptr ++
STEP 7 : Compute std = sqrt(sumstd / n)
STEP 8 : Print the value of sum, mean, std
STEP 9 : Stop
FLOWCHART : Start

Enter the number


of elements n

i=0 F
i++
i<n
T
read the array
elements a[i]

ptr <-- a

i=0 F
i++
i<n
T
sum = sum + *ptr
ptr++

mean = sum / n
ptr <-- a

i=0 F
i++
i<n
T
sumstd = sumstd + (*ptr – mean)2
ptr ++

a
a

std = sqrt(sumstd / n)

print the value of sum,


mean,std

Stop
PROGRAM :
#include<stdio.h>
#include<math.h>
int main()
{
float a[20],*ptr,mean,std,sum=0,sumstd=0;
int n,i;
printf("enter the number of elements\n");
scanf("%d",&n);
printf("enter the array elements\n");
for(i=0;i<n;i++)
{
scanf("%f",&a[i]);
}
ptr=a;
for(i=0;i<n;i++)
{
sum=sum+*ptr;
ptr++;
}
mean=sum/n;
ptr=a;
for(i=0;i<n;i++)
{
sumstd=sumstd+pow((*ptr-mean),2);
ptr++;
}
std=sqrt(sumstd/n);
printf("sum=%3f\t",sum);
printf("mean=%3f\t",mean);
printf("standard deviation=%f\n",std);
}
OUTPUT :

1. enter the number of array elements


3
enter the array elements
1 2 3
sum = 6.000000 mean = 2.000000
standard deviation = 0.816497

2. enter the number of array elements


5
enter the array elements
1 3 2 4 6
sum = 16.000000 mean = 3.200000
standard deviation = 1.720465
TRACING :

1. sum = 0
Sumstd = 0

Enter the number of elements


n=3

enter the array elements


for(i = 0 ; 0 < 3 is true
a[0] = 1
i = 1 ; 1 < 3 is true
a[1] = 2
i = 2 ; 2 < 3 is true
a[2] = 3
i = 3 ; 3 < 3 is false

ptr = a[0]
for(i = 0 ; 0 < 3 is true
sum = 0.000000 + 1 = 1.000000
ptr = ptr + 1 = a[1]

i = 1 ; 1 < 3 is true
sum = 1.000000 + *(a[1])
= 1.000000 + 2
sum = 3.000000
ptr = ptr + 1
ptr = a[2]

i = 2 ; 2 < 3 is true
sum = 3.000000 + *(a[2])
= 3.000000 + 3
sum = 6.000000
ptr = ptr + 1
ptr = a[3]

i = 3 ; 3 < 3 is false
mean = 6.000000 / 3
mean = 2.000000

ptr = a = a[0]
for(i = 0 ; 0 < 3 is true
sumstd = 0.000000 + pow((*(a[0]) – 2.000000),2)
= (1 – 2.000000)2
= (1.000000 – 2.000000)2
sumstd = 1.000000
ptr = ptr + 1 = a + 1 = a[0 + 1] = a[1]

i = 1 ; 1 < 3 is true
sumstd = 1.000000 + pow((*(a[1]) – 2.000000),2)
= 1.000000+(2 – 2.000000)2
sumstd = 1.000000
ptr = ptr + 1 = a[1 + 1] = a[2]

i = 2 ; 2 < 3 is true
sumstd = 1.000000 + pow((*(a[2]) – 2.000000),2)
= 1.000000 + (3 – 2.000000)2
= 1.000000 + 1.000000
sumstd = 2.000000
ptr = ptr +1 = a[2 + 1] = a[3]

i = 3 ; 3 < 3 is false

std = sqrt(2.000000 / 3)
std = 0.816497

sum = 6.000000
mean = 2.000000
std = 0.816497
BINARY TO DECIMAL CONVERSION
15.Implement recursive functions for Binary to Decimal
Conversion.

ALGORITHM :

STEP 1: Start
STEP 2: Enter a Binary Number to be converted
STEP 3: Read the value of “num”
STEP 4: Number entered is assigned to binary_val variable
STEP 5: Condition: while number>0
rem<- num%10
decimal_val<- decimal_val+rem*base
num<-num/10
base<- base*2
STEP 6: Print the Binary Equivalent of the number
STEP 7: Print the Decimal Equivalent of the number
STEP 8: Stop
FLOWCHART : START

INPUT THE VALUE

binary_val=num

while(num>0)

rem=num%10
decimal_val=decimal+val+rem*base
num=num/10
base=base*2

PRINT BINARY
EQUIVALENT

PRINT DECIMAL
EQUIVALENT

STOP
PROGRAM :

#include<stdio.h>
int main()
{
int num,binary_val,decimal_val=0,base=1,rem;
printf("Enter a Binary number(1's and 0's)\n");
scanf("%d",&num);
binary_val=num;
while(num>0)
{
rem=num%10;
decimal_val=decimal_val+rem*base;
num=num/10;
base=base*2;
}
printf("The Binary Number is=%d\n",binary_val);
printf("Its Decimal Equivalent is %d\n", decimal_val);
}

OUTPUT :
Enter a Binary number(1's and 0's)
0000
The Binary Number is=0
Its Decimal Equivalent is 0

Enter a Binary number(1's and 0's)


0001
The Binary Number is=1
Its Decimal Equivalent is 1

Enter a Binary number(1's and 0's)


1010
The Binary Number is=1010
Its Decimal Equivalent is 10

Enter a Binary number(1's and 0's)


1111
The Binary Number is=1111
Its Decimal Equivalent is 15
TRACING :

Вам также может понравиться