summaryrefslogtreecommitdiff
path: root/659/CH9/EX9.4/exm9_4.sci
blob: 6b8d2abdec07fa7aeb1f20c85541338508c5ebd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//             Example 9.4 
//Write a program with function power that computes x raised to the power y.
funcprot(0);
function p = power(x,y)
     p=1.0;               // x to power 0
    if((y>=0)) then
        while(y)         //computes positive powers
            p=p*x;
            y=y-1;
        end
    else
        while(y)       //computes negative powers
            p=p/x;
            y=y+1;
        end
    end
endfunction
disp("Enter x,y:");
disp("[Enter in single line seperated by space] ");
[x,y]=scanf("%d %d");    //input using scanf function

//calling power() function and printing its output
printf("%d to the power %d is %f",x,y,power(x,y));