Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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)
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
a=4
b=4
c=1
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
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
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
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*/
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
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
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 :
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*/
if(300>400)
/*this statement is false hence comes out */
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 :
FOUND=0
i++ i=0
i<n
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
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 :
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
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);
}
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];
} }
}
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
if(3!=3) is false
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
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
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
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
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
Stop
mysine(rad,n)
Read i,sum = 0
i=1 F
i+=2
i<=n
T if F
(i-1)%4 == 0
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));
}
int fact(int n)
{
if(n==0)
return 1;
else
return (n*fact(n-1));
}
OUTPUT :
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
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'
a[i]='\0';
FLOWCHART :
Start
compare_strings(a,b)
concatenate_strings(a,b)
Stop
PROGRAM :
#include<stdio.h>
#include<stdlib.h>
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));
}
unequal strings
equal strings
compare_strings(stone,ring)
c=0
while(a[0] == b[0]
s == r is false
unequal strings
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
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]
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
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 :
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
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
FUNCTIONS:
DEFINE AND
DECLARE A
STRUCTURE
INPUT
ROLL NUMBER,
NAME,
MARK1, MARK 2
TOTAL=MARK1+MARK2
AVERAGE= TOTAL /2.0
PRINT
AVERAGE MARKS
PRINT BELOW
AVERAGE STUDENTS
i++ i=0
i<n
if(s[i]<=avg)
a
a
I++ i=0
i<n
if(s[i]>avg)
RETURN(0)
STOP
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];
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
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
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)
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. sum = 0
Sumstd = 0
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
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