3

I need help making an array that prints out multiples of a given number. The output looks like this:

Enter a positive integer: 8
Test array: 1 1 2 3 5 8 13
Counting up: 1 2 3 4 5 6 7 8
Counting down: 8 7 6 5 4 3 2 1
The first 8 multiples of 5: 5 10 15 20 25 30 35 40
The first 8 multiples of 10: 10 20 30 40 50 60 70 80

This is what I have so far in code:

//Creates an array that counts up to the user input
public static int[] countUp(int n){
    int [] temp = new int[n];
    for(int i=0; i<n; i++){
        temp[i] = i+1;
    }
    return temp;
}

//Creates an array that counts down to the user input
public static int[] countDown(int n){
    int [] temp = new int[n];
    for(int i=0; i<temp.length; i++){
        temp[i] = n--;
    }
    return temp;
}

//Creates an array that gets n amount of multiples of 5
public static int[] getMultiplesOfFive(int n){
    int [] temp = new int[n];
    for(int i=0; i<temp.length; i++){
        temp[i] = n+5;
    }
    return temp;
}

//Creates an array that gets n amount of multiples of 10
public static int[] getMultiplesOfTen(int n){
    int [] temp = new int[n];
    for(int i=0; i<temp.length; i++){
        temp[i] = n+10;
    }
    return temp;
}
}

However my output looks like this:

Enter a positive integer: 8
Test array: 1 1 2 3 5 8 13 
Counting up: 1 2 3 4 5 6 7 8 
Counting down: 8 7 6 5 4 3 2 1 
The first 8 multiples of 5: 13 13 13 13 13 13 13 13 
The first 8 multiples of 10: 18 18 18 18 18 18 18 18 

Obviously the problem is in the last two arrays called getMultiplesofFive and getMultiplesofTen. I'm just not sure how to create a loop that gives me the correct output. Thank you so much!

0

2 Answers 2

2

In your multiply methods, you were not multiplying. Try this:

    //Creates an array that gets n amount of multiples of 5
    public static int[] getMultiplesOfFive(int n){
        int [] temp = new int[n];
        for(int i=0; i<temp.length; i++){
            temp[i] = (i+1)*10;
        }
        return temp;
    }

    //Creates an array that gets n amount of multiples of 10
    public static int[] getMultiplesOfTen(int n){
        int [] temp = new int[n];
        for(int i=0; i<temp.length; i++){
            temp[i] = (i+1)*10;
        }
        return temp;
    }

Adding (i+1) since the index will start at 0 and you want to stat with n*1

Sign up to request clarification or add additional context in comments.

3 Comments

You might also just do int i = 1; i <= n;
True @AndreasBrunnet. But then you still have to reference the array index and in that case subtract 1: temp[i-1] ='. Or, you could skip the i++ increment and do that in the array index: for(int i = 1; i < temp.length;){ temp[i++] = ...'. Either way somewhere you have to make the adjustment.
Damn it forgot about the index. Happens when it's late one is tired... Either way your answer should be marked ;)
0

Your problem is that you keep on calculating the same results. You pass "n" into your method and you store the result of n + 10 in every index (i) of your array.

What you want is something like this (just using the times 5 example):

int[] temp = new int[n];
for(int i = 1; i <= n; i++)
{
    temp[i-1] = i * 5;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.