pi Estimation The value of pi can be estimated from the expr
Solution
%1)
%clearing window, variables and figures
clear all;
close all;
clf;
clc;
ch=1;
while(ch==1)
n=input(\'Enter the number of terms:\');
pro=1;
term=(2)^0.5;
for i=1:1:n
pro=pro*term;
term=(term+(2))^0.5;
end
format long
pro
ch=input(\'\ Do you want to continue, press 1 to continue:\');
end
Result;
Enter the number of terms:5
pro =
20.380016247096115
Do you want to continue, press 1 to continue:1
Enter the number of terms:10
pro =
6.518989025679382e+02
Do you want to continue, press 1 to continue:1
Enter the number of terms:40
pro =
6.999708421902658e+11
Do you want to continue, press 1 to continue:0
%2)
%clearing window, variables and figures
clear all;
close all;
clf;
clc;
x=0;
n=input(\'Enter the number of terms:\');
for i=1:1:n
fprintf(\'\ Enter the %dth term:\',i);
x(i)=input(\'\ Enter:\');
end
for i=1:1:n
for j=1:1:n-i
if x(j)>x(j+1)
a=x(j);
x(j)=x(j+1);
x(j+1)=a;
end
end
end
fprintf(\'\ The sorted matrix:\');
x
Result;
Enter the number of terms:4
Enter the 1th term:
Enter:5
Enter the 2th term:
Enter:2
Enter the 3th term:
Enter:3
Enter the 4th term:
Enter:7
The sorted matrix:
x =
2 3 5 7
%3)
%clearing window, variables and figures
clear all;
close all;
clf;
clc;
sum=0;
n=10;
num=10;
while sum<=100
sum=(n^2/2)+(n/2);
n=n+1;
end
n;
while sum<1000
i=1;
sum=(n^2/2)+(n/2);
if sum>=1000
break
else
num=sum;
while(num>0)
x(i)=rem(num,10);
num=num/10;
num=fix(num);
i=i+1;
end
n1=length(x);
for j=1:1:n1
if j==1
dig=x(j);
elseif x(j)==dig
c=0;
else
c=1;
break;
end
end
if c==0
fprintf(\'\ The sum corresponding to %d is %d:\',n,sum);
end
n=n+1;
end
end
Result;
The sum corresponding to 36 is 666:


