Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.

# Learning C

8 replies to this topic

### #1 nickautomatic

nickautomatic

• Members
• 143 posts
• OFFLINE
•
• Gender:Male
• Local time:01:29 AM

Posted 13 November 2015 - 03:42 PM

Good day guys!

I am a student, studying programming. I am trying to make a very simple program that I could not figure how to make it. Here is it.

program should ask 2 intergers, first will be base and second should be the exponent.

sample Output:

Enter base and exponent: 5  3

5 raised to the power of 3 is 125.
---

Thank you.

### #2 Nikhil_CV

Nikhil_CV

Vestibulum Bleep

• Members
• 1,145 posts
• OFFLINE
•
• Gender:Male
• Location:err: Destination unreachable! bash!
• Local time:09:59 PM

Posted 13 November 2015 - 04:24 PM

Logic:

• Initialize Assume b is base and e is exponent in integer/double value and read input.
• Check if b is 0, 0 power anything is 0 (except 0^0 = indeterminate form, taken as 1 as a rule) (create a user function if needed)
• Check if e is 0, anything power 0 is 1
• Check if e is 1, anything power 1 is the base itself.
• Use a loop while b>0 and e>1 to evaluate multiplication of b to b for e times.
• Display result and halt.

Libraries required: math.h, stdio.h

Default function: pow(x,y);

http://www.tutorialspoint.com/c_standard_library/c_function_pow.htm

Edited by Nikhil_CV, 13 November 2015 - 04:32 PM.

Regards : CV                                                                                                    There is no ONE TOUCH key to security!
Always have a Backup Plan!!! Because human idiotism doesn't have a cure! Stop highlighting!
Questions are to be asked, it helps you, me and others.  Knowledge is power, only when its shared to others.            signature contents © cv and Someone.......

### #3 nickautomatic

nickautomatic
• Topic Starter

• Members
• 143 posts
• OFFLINE
•
• Gender:Male
• Local time:01:29 AM

Posted 13 November 2015 - 04:33 PM

Thanks for the information, Nikhil_CV.

### #4 nickautomatic

nickautomatic
• Topic Starter

• Members
• 143 posts
• OFFLINE
•
• Gender:Male
• Local time:01:29 AM

Posted 19 November 2015 - 03:04 PM

thanks for the information Guys! Programming is fun!

### #5 Nikhil_CV

Nikhil_CV

Vestibulum Bleep

• Members
• 1,145 posts
• OFFLINE
•
• Gender:Male
• Location:err: Destination unreachable! bash!
• Local time:09:59 PM

Posted 21 November 2015 - 04:29 AM

Regards : CV                                                                                                    There is no ONE TOUCH key to security!
Always have a Backup Plan!!! Because human idiotism doesn't have a cure! Stop highlighting!
Questions are to be asked, it helps you, me and others.  Knowledge is power, only when its shared to others.            signature contents © cv and Someone.......

### #6 blueelvis

blueelvis

Bleep Blop Bleep

• Malware Response Team
• 1,666 posts
• OFFLINE
•
• Gender:Male
• Location:India
• Local time:09:59 PM

Posted 21 November 2015 - 06:08 AM

This appears to be a homework question...

Member of the Bleeping Computer A.I.I. early response team!

In case I have been helping you and you haven't received a reply from me in 48 hours, please feel free to PM me. Anything else? Still feel free to PM me

### #7 nickautomatic

nickautomatic
• Topic Starter

• Members
• 143 posts
• OFFLINE
•
• Gender:Male
• Local time:01:29 AM

Posted 21 November 2015 - 01:30 PM

Yes, it is blueelvis. It is an homework question.

### #8 blueelvis

blueelvis

Bleep Blop Bleep

• Malware Response Team
• 1,666 posts
• OFFLINE
•
• Gender:Male
• Location:India
• Local time:09:59 PM

Posted 21 November 2015 - 02:24 PM

Yes, it is blueelvis. It is an homework question.

I generally frown upon helping others with their homework questions until and unless they show me the effort they have put into it. That way you get to learn something yourself which is more important

-Pranav

Member of the Bleeping Computer A.I.I. early response team!

In case I have been helping you and you haven't received a reply from me in 48 hours, please feel free to PM me. Anything else? Still feel free to PM me

### #9 AceInfinity

AceInfinity

• Members
• 30 posts
• OFFLINE
•
• Gender:Male
• Local time:09:29 AM

Posted 09 January 2016 - 10:25 PM

I know this is an older topic, but pow() from <math.h> deals with floating point datatypes. For this reason I generally use my own implementation when i'm dealing with integers to avoid floating point arithmetic.

unsigned long powl(int b, int e)
{
int i = 1;
unsigned long n = b;
while (i++ < e) n *= b;
return e ? n : 1;
}

You'll want to use scanf() for reading in the 2 integers before passing them to the function calculating the exponential value.

Edited by AceInfinity, 09 January 2016 - 10:26 PM.

Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users