a program that raise a number to a given power

0 Maneh Clovis · June 11, 2015
this program has raise a number to a given number but is not working.





#include <stdio.h>
#include <stdlib.h>


int main()

{
    int a,c,b;
    printf(" enter the number:");
    scanf(" %d",&a);

    printf("\n enter the power:");
    scanf(" %d",&b);

    c=(a^);

    printf("the result is %d",c);

return 0;

}




Post a Reply

Replies

Oldest  Newest  Rating
0 Rich Sharma · June 21, 2015
exackly
0 K D · June 11, 2015
you either have to import math.h and use the pow function which returns a float or use a loop to simulate it. C, C++, and Java do not have a built in operator like python and even in python thats not the operator you would use.  so it would look like:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()

{
int a,b;
float c;
...
c = pow(a, b);

printf("the result is %f",c);

return 0;
}


if you want to control how many decimal places are shown you can use "%.<number of decimal places>f" for example:
printf("the result is %.2f",c);

would only display 2 decimal places.

i really hope this helps.
0 Jon Z. · June 17, 2015
Here I fixed it for you.

Instead of the loop you could use "c = pow(a,b)" if you include the "math.h" library. 

#include 
...
c = pow(a,b);


Enjoy 8-)



#include

int main()

{
    int a, b, c;
    int i;

    printf(" enter the number:");
    scanf(" %d",&a);

    printf("\n enter the power:");
    scanf(" %d",&b);

    c = 1;
    for(i=0; i
0 Rich Sharma · June 20, 2015
if you actually want to get into making the pow function in c, then use something called recursion. you should make a method, pass in the base and power, and then in the method check if the power == 0 if it is return 1  otherwise multiply the base by itself and at the end of the function you can all the method, but power-- or decrementing the power by one everytime you go through the function. 
0 Kenneth Malicay · June 20, 2015
If you're a beginner I suggest use a while or for loop to do it. You'd now have to think the logic on how to do it yourself instead of using a predefined function.
Sure thousands of libraries are made to make programming life easier but logic is a part of what makes you a programmer so you have to practice and improve it.
Just a food for thought.
  • 1

C

107,124 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator