Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## Iterative Help

Im writing a program that takes a number inputed and prints the first n positive numbers beginning at 0 by 10's, separated by spaces.

For example: n=4 prints 0, n=65 prints 0 10 20 30 40 50 60

Im not understanding how to write this using an iterative method. Any help please?

2. ## Re: Iterative Help

First you probably want to divide the number by 10, which will give you a number that tells you how many you gotta print. for instance, 76 / 10 = 7. Then toss that into a loop which starts at 0 and goes until your integer, multiplying each by 10 and displaying it.

3. ## The Following User Says Thank You to Jared For This Useful Post:

Jnoobs (October 6th, 2010)

4. ## Re: Iterative Help

That makes a lot of sense, thank you.

5. ## Re: Iterative Help

Recursively in pseudo-code:
```public void printVals(int max, int current){
//evaluate whether to return. Is current > max?
//do work - increment and print
//loop by calling myself with new values determined from work
}```

6. ## Re: Iterative Help

I ran into a problem with my code. My Iterative works but my Recursion isnt.

```public static int Recursion(int n) {
int result = 1, num;

num = n/10;

if(n == 0) {
result = 1; // Base case
}
else if(n == 1) {
result = 1; // Base case
}
else {
result = n * 10;
}
System.out.print("Recursive: " + result + " " + "\n");
return result;
}

public static void Iterative(int n) {
int var, num;

var = n/10;

for(int i = 0; i <= var;i++) {
num = i*10;
System.out.print("Iterative: " + num + " ");
}

}```

The recursive method should print the same output as the iterative method

7. ## Re: Iterative Help

Recursive functions call themselves...I don't see where your Recursion() function does this.

8. ## Re: Iterative Help

but now that i tried incorporating the call it still doesnt work.

```public static int Recursion(int n) {
int result = 1, num;

num = n/10;

System.out.print("\nRecursive: ");
if(n == 0) {
result = 1; // Base case
}
else if(n == 1) {
result = 1; // Base case
}
else {
result = num * Recursion(num - 1);
}
System.out.print(result + " ");
return result;
}```

9. ## Re: Iterative Help

please define doesn't work...eg output, exceptions, compile time errors, etc..

10. ## Re: Iterative Help

My Recursive output needs to be the same as my iterative Output, but it isnt outputting the same way.

11. ## Re: Iterative Help

Is the parameter to Recusion() on each iteration what you would expect? Add a println in the function to test the value of the parameter to make sure it is what it should be.

12. ## Re: Iterative Help

I dont understand what you mean

13. ## Re: Iterative Help

I also need to do these but dont understand the question

3. Write an iterative Java method that is passed two non–negative integer numbers m and n as parameters,
and multiplies the two numbers by consecutive additions. The method should return the product m∗n.
4. Write the same method as above, but use recursion instead of iteration.

and finally:
Write a recursive method to solve Ackermann’s Function assuming non–negative integers as input. Ackermann’s function is a that solves the following mathematical recurrence function:
 n + 1 if m = 0 A(m, n) = A(m − 1, 1) if m > 0 and n = 0  A(m−1,A(m,n−1) ifm>0andn>0
NOTE: This recurrence relation grows extremelly rapidly, so test it using small m and n values (≤ 4).
Doesnt print results; here is my code:
```System.out.print("\nProblem 6 (Enter a positive number): ");
m = nm.nextInt();
n = nm.nextInt();
result3 = Recurfour(m, n);
System.out.println("Problem 6: " + result3);```

AND the method is
```public static int Recurfour(int m, int n) {
int result=0;

if(m == 0) {
result = n+1; // Base case
}
else if(m > 0 && n == 0) {
result = Recurfour(m-1,1); // Base case
}
else if(m > 0 && n > 0){
result = Recurfour(m-1,Recurfour(m,n-1));
}
return result;
}```

14. ## Re: Iterative Help

One thing at a time...

I dont understand what you mean
I mean check your math. Adding some System.out.println statements to print out the values of variables - calculate what you'd expect, then see what it gives - is an invaluable method in debugging code such as this. Once again check your math...say you first call Recursion with 50, your next call will be with 4. Hmmmm.