User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 423,369 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,998 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 3442 | Replies: 2
Reply
Join Date: Sep 2004
Posts: 1
Reputation: erictham83 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
erictham83 erictham83 is offline Offline
Newbie Poster

how to write a program for power

  #1  
Sep 3rd, 2004
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2004
Posts: 24
Reputation: Brent_Ritterbec is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
Brent_Ritterbec Brent_Ritterbec is offline Offline
Newbie Poster

Re: how to write a program for power

  #2  
Sep 3rd, 2004
#include <stdio.h>
int main(void)
{
int base, power, index;
long answer;
base = 0;
power = 0;
answer = 1.00;
printf("Enter a base number: ");
scanf("%d", &base);
printf("Enter a power to raise the base to: ");
scanf("%d", &power);
for(index = 1; index <= power; index++)
answer = answer * base;
printf("%d raised to the power of %d is %ld.", base, power, answer);
getchar();
getchar();
return 0;
}


This is just something I threw together in about a minute.
Reply With Quote  
Join Date: Aug 2004
Posts: 24
Reputation: Brent_Ritterbec is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
Brent_Ritterbec Brent_Ritterbec is offline Offline
Newbie Poster

Re: how to write a program for power

  #3  
Sep 3rd, 2004
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 12:22 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC