power code

import java.io.*;

import java.util.*;

publicclass Power

{

static Scanner console =new Scanner(System.in);

publicstaticvoid main (String[] args)

{

long base;

long power;

int product;

System.out.print("Enter base: ");

System.out.flush();

base = console.nextLong();

System.out.println();

System.out.print("Enter power: ");

System.out.flush();

power = console.nextLong();

System.out.println();

do

{

System.out.print(product);

product = base^power;

while (base > 0, base^power);

}

}

This program is supposed to ask a user for a base and power, it is then supposed to compute. However, with my lack of concrete math knowledge I don't know how to create such a program when using a loop.

[1491 byte] By [nsfoura] at [2007-11-27 4:22:29]
# 1
Hint: I bet you are expected to achieve this through repeated multiplication. Am I right?Tip: operator ^ is not exponentiation. It is in fact bitwise exclusive-or, and of no use in your assignment.
Hippolytea at 2007-7-12 9:29:49 > top of Java-index,Java Essentials,Java Programming...
# 2
Here is the API you wanted: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Math.html#pow(double,%20double)
rym82a at 2007-7-12 9:29:49 > top of Java-index,Java Essentials,Java Programming...
# 3

> However, with my lack of concrete math knowledge I don't know how

> to create such a program when using a loop.

[url=#" style="background:url(http://upload.wikimedia.org/math/3/4/c/34c04a6ca2ac051a6e46494f613dfad2.png) norepeat; width:142; height:39] [/url]

You need a loop in order to repeat multiplication operation n times (a for loop is probably the best suited loop for that task).

The operation to perform being multiplying current result by a.

Basically:result = 1;

loop( n times) {

result = result * a;

}

A concrete execution for a=4 (base) and n=3 (exponent/power) would then be:result = 1;

// here comes the loop (3 times) :

result = result * a; // 1*4=4

result = result * a; // 4*4=16

result = result * a; // 16*4=64

// final value of result is 64

TimTheEnchantora at 2007-7-12 9:29:49 > top of Java-index,Java Essentials,Java Programming...
# 4

thanks for the tip!

however, when i run this program it prints 2 and 6 on different lines, when a = 2 and n = 2

import java.io.*;

import java.util.*;

public class Power

{

static Scanner console = new Scanner(System.in);

public static void main (String[] args)

{

int a;

int n;

int result;

System.out.print("Enter base: ");

System.out.flush();

a = console.nextInt();

System.out.println();

System.out.print("Enter power: ");

System.out.flush();

n = console.nextInt();

System.out.println();

for (a = 1; n > a; n++)

{

a = a * n;

System.out.print(a);

System.out.println();

}

}

}

nsfoura at 2007-7-12 9:29:49 > top of Java-index,Java Essentials,Java Programming...
# 5
thats bcoz for each repetition of the loop you are printing the resultyou should print the result only after you have calculated the final value.when you run the current code with large numbers you will understand whats wrong with the code
sridanua at 2007-7-12 9:29:49 > top of Java-index,Java Essentials,Java Programming...
# 6

Two things:

1) You need a loop that loops n times. Read the [url=http://java.sun.com/docs/books/tutorial/java/nutsandbolts/for.html]tutorial[/url] about for loop.

2) You are not building the result, but you are modifying the value of a. You should not change value of neither a nor n.Check my pseudocode again.

TimTheEnchantora at 2007-7-12 9:29:49 > top of Java-index,Java Essentials,Java Programming...
# 7
result = 1;for (n = 1; n < 0; n++){result = result * a;System.out.print(result);System.out.println();}I still don't get the increment part.
nsfoura at 2007-7-12 9:29:49 > top of Java-index,Java Essentials,Java Programming...
# 8

for (n = 1; n < 0; n++)

Arrgh, come on, think! This loop says: you start with n being 1 and you want to repeat as long as n is less than 0. The last time I checked, 1 was greater than 0. So how many times do you think will your loop body get executed?

thomas.behra at 2007-7-12 9:29:49 > top of Java-index,Java Essentials,Java Programming...
# 9
Isn't there an example of a simple for loop that loops ten times in the tutorial ?Can't you imagine a way to generalize it in order to loop n times ?
TimTheEnchantora at 2007-7-12 9:29:49 > top of Java-index,Java Essentials,Java Programming...
# 10
i still can't get it
nsfoura at 2007-7-12 9:29:49 > top of Java-index,Java Essentials,Java Programming...
# 11

I like to loop n times with an arrow that is composed of the '--' and '>' operators.

The arrow inidicates n moving towards 0:unsigned int n = 10;

while (n --> 0)

{

// n=9 ... n=0

}

tom_jansena at 2007-7-12 9:29:49 > top of Java-index,Java Essentials,Java Programming...
# 12
> i still can't get itWhich part don't you understand ?The sentence written in English ?or The basic Mathematics ?
rym82a at 2007-7-12 9:29:49 > top of Java-index,Java Essentials,Java Programming...
# 13

> I like to loop n times with an arrow that is composed

> of the '--' and '>' operators.

> The arrow inidicates n moving towards

> 0:[code]unsigned int n = 10;

> while (n --> 0)

> {

>// n=9 ... n=0

> code]

I don't think it is Java code, or we have "unsigned" in new version of Java.

When you know how many times of looping, for-loop is better than do-while or while.

It is more readable and traceable.

rym82a at 2007-7-12 9:29:49 > top of Java-index,Java Essentials,Java Programming...