Here is the code for the C power example:
#include <stdio.h>
/*
power recursive
return x^y
*/
int power(int x,int y){
if (y==0) return 1;
if (y==1) return x;
return( x*power(x,y-1) );
}
/*
power not recursive
return x^y
*/
int power2(int x,int y) {
if (y==0) return 1;
if (y==1) return x;
int result,i;
for (i=0,result=1;i<y;i++) {
result = result * x;
}
return(result);
}
int main() {
int x,y;
do{
printf("Give integer X from 1 to 20 : ");
scanf("%d",&x);
}
while( x>20 || x<1 );
do{
printf("Give integer Y from 0 to 5 : ");
scanf("%d",&y);
}
while( x>20 || x<1 );
printf("%d raised to %d gives %d \n",x,y,power(x,y));
printf("%d raised to %d gives %d ",x,y,power2(x,y));
exit (0);
}
The output is like that:
Please give integer X from 1 to 20: 2
Please give integer Y from 0 to 5: 5
X raised to Y gives: 32
You may want to see Simple MIPS counter or MIPS bubble sort.
plz tell us modification in an existing code if we want to calculate power of unsigned number and print result using shift operator
ReplyDelete