Question

Using #include <stdio.h> 6) Write a program that computes X^Y (X to the power of Y)....

Using #include <stdio.h>
6) Write a program that computes X^Y (X to the power of Y). You are required to use a while loop to solve the problem. Do not use the pow function from the “math.h” library. You may hard code the variables X and Y in the beginning of your program.
Hint: 5^4 =5*5*5*5

0 0
Add a comment Improve this question Transcribed image text
Answer #1

SOURCE CODE IN C:

#include <stdio.h>

//function to calculate x to the power y and return it

double power(int x, int y)

{

//res stores x to power of absolute value of y

int res=1,copy=y;

if(y<0)

copy=-copy; //storing absolute value of y in copy

//calculating x to power of absolute value of y

while(copy!=0)

{

res*=x;

copy-=1;

}

if(y<0) //if y is negative, we return 1/(x to power of absolute value of y)

return 1.0/res;

else //else we return x to power of absolute value of y

return res;

}

int main(void)

{

//testing function

printf("2 to the power 4: %.2f\n",power(2,4));

printf("3 to the power -2: %.2f\n",power(3,-2));

printf("4 to the power 0: %.2f\n",power(4,0));

return 0;

}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Using #include <stdio.h> 6) Write a program that computes X^Y (X to the power of Y)....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #include <stdio.h> .. int main(int argc, char *argv[]) { int base, power; printf("enter base and power:...

    #include <stdio.h> .. int main(int argc, char *argv[]) { int base, power; printf("enter base and power: "); scanf("%d %d", &base, &power); while (base != -100){ double res = pow(base, power); double res2 = my_pow(base, power); printf("pow: %.4f\n", res); printf("my_pow: %.4f\n", res2); .... } return 0; } // this function should be RECURSIVE // should not use any loop here double my_pow(double base, double p) { } lab4pow.c file contains: 2.1 Specification Write an ANSI-C program that reads input from the...

  • Two positive integers are entered through the keyboard. Write a program to find the value of...

    Two positive integers are entered through the keyboard. Write a program to find the value of one number raised to the power of another. You are NOT allowed to use the pow function in math.h library file

  • Create a CPP file for a C++ Program. Write exactly these functions, power(x,y) function and a...

    Create a CPP file for a C++ Program. Write exactly these functions, power(x,y) function and a print(text, number) function and the main() function. The power(x,y) function returns an integer result that is calculated by raising a number x (integer) to a power y (integer). The second argument y (exponent) of the function can not exceed 100. If the second argument exceeds 100, the function should return -1. Your power(x,y) function must be able to take either 1 or 2 integer...

  • Write a C program that computes Pi with the approximation algorithm that I introduced in class....

    Write a C program that computes Pi with the approximation algorithm that I introduced in class. I want you to write the program in two different ways: The first version will add up the first 28284277 elements of the approximation series in a simple for loop. I ask you to put the code into a function with this signature: float get_pi_for(void); The second version will break out of a while loop depending on whether a pair of adjacent elements (remember...

  • #include <stdio.h> int main ( ) { int k; int sum=0; for (k=0; k<6; k++) {...

    #include <stdio.h> int main ( ) { int k; int sum=0; for (k=0; k<6; k++) { sum += k; } printf("%d\n", sum); return 0; } (a) [10 pts] Convert the C program to be an assembly code named as lab3_ex7_assembly.s. Please notice that your code must be executable in the Venus simulator. In the assembly code you write, you need to answer the following questions as well: (b) [5 pts] What are the registers in your code to represent the...

  • What to do Write a C program that computes Pi with the approximation algorithm that I...

    What to do Write a C program that computes Pi with the approximation algorithm that I introduced in class. I want you to write the program in two different ways: The first version will add up the first 28284277 elements of the approximation series in a simple for loop. I ask you to put the code into a function with this signature: float get_pi_for(void); The second version will break out of a while loop depending on whether a pair of...

  • Write a program that contains a main function, a “double custom_operator_function(int op_type, double x, double y)”,...

    Write a program that contains a main function, a “double custom_operator_function(int op_type, double x, double y)”, a “double min(double,double)”, a double max(double x,double y), and a double pow(double x, int y). The main function is used to call the custom operator function. Next, the custom operator function calls the min/max/pow functions based on the op_type. From the main, call the custom operator function with the three inputs 1/-3/5, 2/4/5, and 3/4/5. Print the result of each input to screen. i)...

  • /*––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/ /*    */ /* */ /* This program computes average power, average magnitude */ /*...

    /*––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/ /*    */ /* */ /* This program computes average power, average magnitude */ /* and zero crossings from a speech signal. */ #include <stdio.h> #include <math.h> #define MAXIMUM 50 int main(void) { /* Declare variables */ int k=0, npts=30; double speech[MAXIMUM]={0.000000,-0.023438,-0.031250,-0.031250,-0.039063,-0.039063,-0.023438,0.000000,0.023438,0.070313,-0.039063,-0.039063,0.046875,0.101563,0.117188,0.101563,0.070313,0.054688,0.023438,0.000000,-0.031250,-0.039063,-0.070313,-0.070313,-0.070313,-0.070313,-0.062500,-0.046875,-0.039063,-0.031250}; /* Declare the function prototypes */    /* Compute and print statistics. */ printf("\n SPEECH DATA "); print(,,);//complete the statement printf("\n"); printf(" SPEECH STATISTICS : \n"); printf(" average power: %f \n",);//complete the statement printf(" average magnitude: %f...

  • Write a MIPSzy subroutine, power, that accepts 2 arguments x and y and computes x^y ....

    Write a MIPSzy subroutine, power, that accepts 2 arguments x and y and computes x^y . You can assume that x and y are both larger than 0. The main program passes the two parameters and receives the return value through the program stack. addi $t0, $zero, 5100 top: lw $t1, 0($t0) beq $t1, $zero, done addi $sp, $sp, -4 sw $t1, 0($sp) addi $t2, $zero,4 addi $sp, $sp, -4 sw $t2, 0($sp) addi $sp, $sp, -4 jal power lw...

  • Consider the problem of computing the power function pow(n,x) = n^x using only multiplications. The first...

    Consider the problem of computing the power function pow(n,x) = n^x using only multiplications. The first approach is to perform x multiplications ($n \cdot n \cdot n \cdot \ldots \cdot n$, x times). Find a better, recursive algorithm to solve this problem (by better, we mean one that uses fewer than $x$ multiplications). Write down the pseudocode for this new function, and then analyze the runtime of that recursive program by first writing out the recurrence relation $T(n, x)$ that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT