954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to write a program for power

how to write a program for raising a number to a power(integer only)
(e.g 2^2=4 ; 3^3=27 etc)
without using a precompiled C function(ie pow()). but only used addition, substraction, while loops,if statement and/or for loop.

erictham83
Newbie Poster
1 post since Sep 2004
Reputation Points: 10
Solved Threads: 0
 

#include
int main(void)
{[INDENT]int base, power, index;
long answer;[/INDENT] [INDENT]base = 0;
power = 0;
answer = 1.00;[/INDENT] [INDENT]printf("Enter a base number: ");
scanf("%d", &base);
printf("Enter a power to raise the base to: ");
scanf("%d", &power);[/INDENT] [INDENT]for(index = 1; index <= power; index++)[INDENT]answer = answer * base;[/INDENT][/INDENT] [INDENT]printf("%d raised to the power of %d is %ld.", base, power, answer);
getchar();
getchar();
return 0;[/INDENT]}


This is just something I threw together in about a minute.

Brent_Ritterbec
Newbie Poster
24 posts since Aug 2004
Reputation Points: 12
Solved Threads: 0
 

I still haven't quite figured out the indention method on this forum, and the getchar() at the end are for the Dev-C++ compiler so that the DOS-Prompt doesn't close automatically.

Brent_Ritterbec
Newbie Poster
24 posts since Aug 2004
Reputation Points: 12
Solved Threads: 0
 

How to give command for power.

gayatridas
Newbie Poster
2 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

Why, in any circumstance, would resurrecting a four year old thread to ask a vague question using bad grammar (forget english grammar, I mean C grammar. C doesn't have "commands") be a good idea? Hmmm...

death_oclock
Posting Whiz
393 posts since Apr 2006
Reputation Points: 129
Solved Threads: 45
 

#include
#include
#include
void main()
{
int base,power,answer;
clrscr();
printf("Enter the base no\n");
scanf("%d",&base);
prinf("Enter the power\n");
scanf("%d",&power);
answer=pow(base,power);
printf("The result is %d",answer);
getch();
}

BimanD
Newbie Poster
8 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

>BimanD

Did you even read what the question posed was...? And what others have replied to it...?

csurfer
Posting Pro
568 posts since Jan 2009
Reputation Points: 485
Solved Threads: 88
 
how to write a program for raising a number to a power(integer only) (e.g 2^2=4 ; 3^3=27 etc) without using a precompiled C function(ie pow()). but only used addition, substraction, while loops,if statement and/or for loop.

You intend to do it without inbuilt library functions like pow() right ? Do it with recursive call of a function as:

int power( int b , int p)
{
if( p > 1 )
               return b * power( b , p-1 );
else
               return b;
}


where b and p would take the format in library function pow as pow(b,p).Just call the above shown function in main passing the two numbers as arguments.Your work is done.
There are many other ways of doing this.But its just left to your interest.If you really want to learn programming in C then try in ow many ways can you write the same program(using different logic) and to what extent you can simplify.

csurfer
Posting Pro
568 posts since Jan 2009
Reputation Points: 485
Solved Threads: 88
 

is there any other method to find the power of any i.e. divide and subtract method

mtx_rashid
Newbie Poster
1 post since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

Do not resurrect an old thread to ask a question. You should only post help to an existing thread.

Now go start your own thread and reword your question giving us all the detail necessary for understanding exactly what you want.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

hw to get power flow studies 32 bus system c++ program?

shiva_202
Newbie Poster
1 post since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You