CS10-8 Assignment ControlStructures
CS10-8 Assignment ControlStructures
Write a program, TabRoots.m, that uses a for loop to tabulate for each positive integer n, its
square, square root, and en. The output should look like this:
n= 1
sqrt(n)= 1
nˆ2= 1
exp(n)= 2.7183
n= 2
sqrt(n)=1.4142
nˆ2= 4
exp(n)= 7.3891
n= 3
sqrt(n)= 1.7321
nˆ2= 9
exp(n)= 20.0855
n= 4
sqrt(n)= 2
nˆ2= 16
exp(n)= 54.5982
n= 5
sqrt(n)= 2.2361
nˆ2=25
exp(n)= 148.4132
%%TabRoots.m
%This program calculates the square, square root
%and exponent nth times
num=input('Input an integer number: ');
for x=1: num
sqr=x^2;
sqrt=(sqrt(x));
ex=exp(x);
disp(['n= ', num2str(x)]);
disp(['square of ', num2str(x),' is ' num2str(sqr)]);
disp(['square root of ',num2str(x),' is ' num2str(sqrt)]);
disp(['exponent of ', num2str(x), ' is ' num2str(ex)]);
end
>> TabRoots
Input an integer number: 1
n= 1
square of 1 is 1
square root of 1 is 1
exponent of 1 is 2.7183
>>