# Calculate Power of a number

Page 1 of 1

## 9 Replies - 11506 Views - Last Post: 22 January 2010 - 02:42 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=151397&amp;s=da663f7c40c593d90a1b8475aaceb7f3&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 fwang321

Reputation: 0
• Posts: 4
• Joined: 22-January 10

# Calculate Power of a number

Posted 22 January 2010 - 12:30 PM

So here is my code, it is a cheap way to calculate the power of a number to place it in scientific notation. It just counts the number of numbers and subtracts 1 from it. It does what i ask but it prints out ever process it goes through to get the final answer. How do i make it just print the final answer?
I am also trying to prevent someone from typing a letter and making the program end.

```import java.util.Scanner;
public class getPower {
public static void main (String[] args)	 {

Scanner scan = new Scanner( System.in );
int i = 0;
System.out.println ("Give me a Number");

String phrase = scan.nextLine();

for (i = 0; i < 3; i++){

phrase = phrase.toLowerCase();

int count = 0;
for (i = 0; i < phrase.length(); i++) {
char c = phrase.charAt(i);
if (c=='1' || c=='2' || c=='3' || c=='4' || c=='5' || c=='6' || c=='7' || c=='8' || c=='9' || c=='0') {
count++;
}
if (count == 2)
{System.out.println( phrase + " is to the " + (count - 1) + "st " + "power");}
if (count == 3)
{System.out.println( phrase + " is to the " + (count - 1) + "nd " + "power");}
if (count == 4)
{System.out.println( phrase + " is to the " + (count - 1) + "rd " + "power");}
if (count > 4)
{System.out.println( phrase + " is to the " + (count - 1) + "th " + "power");}
else
System.out.println( phrase + " includes a letter or symbol,\n symbols or letters do not have powers");
}

System.exit(0);
}
}
}
```

Edited by Locke. Please, in the future -->

This post has been edited by Locke: 22 January 2010 - 01:13 PM

Is This A Good Question/Topic? 0

## Replies To: Calculate Power of a number

### #2 SwiftStriker00

• No idea why my code works

Reputation: 439
• Posts: 1,617
• Joined: 25-December 08

## Re: Calculate Power of a number

Posted 22 January 2010 - 12:47 PM

### #3 g00se

• D.I.C Lover

Reputation: 3533
• Posts: 16,009
• Joined: 20-September 08

## Re: Calculate Power of a number

Posted 22 January 2010 - 12:48 PM

Quote

It just counts the number of numbers and subtracts 1 from it.

There's a great deal of code there to do just that, which would be

```int pow = input.length() - 1;

```

### #4 SwiftStriker00

• No idea why my code works

Reputation: 439
• Posts: 1,617
• Joined: 25-December 08

## Re: Calculate Power of a number

Posted 22 January 2010 - 12:49 PM

I would just get rid of your println in your loop, and put one at the end before your system.exit call

### #5 fwang321

Reputation: 0
• Posts: 4
• Joined: 22-January 10

## Re: Calculate Power of a number

Posted 22 January 2010 - 12:53 PM

SwiftStriker00, on 22 Jan, 2010 - 11:49 AM, said:

I would just get rid of your println in your loop, and put one at the end before your system.exit call

DAYUM that did it! thank you

### #6 g00se

• D.I.C Lover

Reputation: 3533
• Posts: 16,009
• Joined: 20-September 08

## Re: Calculate Power of a number

Posted 22 January 2010 - 01:06 PM

fwang321, please post your current, working code for the benefit of future visitors

### #7 fwang321

Reputation: 0
• Posts: 4
• Joined: 22-January 10

## Re: Calculate Power of a number

Posted 22 January 2010 - 01:28 PM

g00se, on 22 Jan, 2010 - 12:06 PM, said:

fwang321, please post your current, working code for the benefit of future visitors

sure
```import java.util.Scanner;
public class getPower {
public static void main (String[] args)	 {

Scanner scan = new Scanner( System.in );
int i = 0;
System.out.println ("Give me a Number");

String phrase = scan.nextLine();

for (i = 0; i < 3; i++){

phrase = phrase.toLowerCase();

int count = 0;
for (i = 0; i < phrase.length(); i++) {
char c = phrase.charAt(i);
if (c=='1' || c=='2' || c=='3' || c=='4' || c=='5' || c=='6' || c=='7' || c=='8' || c=='9' || c=='0') {
count++;
}
}
if (count == 2)
{System.out.println( "The number is to the " + (count - 1) + "st " + "power");}
if (count == 3)
{System.out.println( "The number is to the " + (count - 1) + "nd " + "power");}
if (count == 4)
{System.out.println( "The number is to the " + (count - 1) + "rd " + "power");}
if (count > 4)
{System.out.println( "The number is to the " + (count - 1) + "th " + "power");}
else
System.out.println( phrase + " includes a letter or symbol,\n symbols or letters do not have powers");

System.exit(0);
}
}
}

```

### #8 g00se

• D.I.C Lover

Reputation: 3533
• Posts: 16,009
• Joined: 20-September 08

## Re: Calculate Power of a number

Posted 22 January 2010 - 01:50 PM

OK. Firstly, the significant change you've made to your original is nothing to do with the final println - it's to do with where the inner for loop ends.

In fact your println about the input not being a number still prints, irrespective of input

### #9 fwang321

Reputation: 0
• Posts: 4
• Joined: 22-January 10

## Re: Calculate Power of a number

Posted 22 January 2010 - 02:27 PM

g00se, on 22 Jan, 2010 - 12:50 PM, said:

OK. Firstly, the significant change you've made to your original is nothing to do with the final println - it's to do with where the inner for loop ends.

In fact your println about the input not being a number still prints, irrespective of input

yeah i just realized it failed once again. I dun think i saved the right one

### #10 g00se

• D.I.C Lover

Reputation: 3533
• Posts: 16,009
• Joined: 20-September 08

## Re: Calculate Power of a number

Posted 22 January 2010 - 02:42 PM

Get rid of the outer loop. Test for improper input using Integer.parseInt - if it's not a number you'll get an exception