I am trying to do an assignment that requires us to do something with pentagonal numbers. This is my code:

import java.util.Scanner;
public class Pentagonal {

    public static void main(String[] args) {

        System.out.print("The Pentagonal Numbers: ");
        getPentagonalNumber(10);
    }

    public static int getPentagonalNumber(int n) {

        Scanner input = new Scanner(System.in);
System.out.print(
"Enter an integer: ");
long n = input.nextInt();
        int n1 = 1;

        while (n<=100) {
            n = (3*n1*n1 - n1)/2;
            System.out.println(n);
            n1++;
        }
        return n;

    }

}

I am trying to make the output look like this:

Sample Run 1:
Enter an integer: 2
The pentagonal numbers are: 1 5

Sample Run 2:
Enter an integer: 4
The pentagonal numbers are: 1 5 12 22

logic seems to be correct if you need the numbers in same line with tabs.use "\t" in print statement.if you need to print the number specific times then implement logic like this:

int count=1;
while(count<=n)
{
//your code
//your code
count++;
}

try this (for printing tabs):

System.out.print(n+"\t");

hope now you can adjust the code to your suitability.

So you mean like this:

import java.util.Scanner;
public class Pentagonal {

    public static void main(String[] args) {

        System.out.print("The Pentagonal Numbers: ");
        getPentagonalNumber(10);
    }

    public static int getPentagonalNumber(int n) {

        Scanner input = new Scanner(System.in);
System.out.print(
"Enter an integer: ");
long n = input.nextInt();
        int n1 = 1;
        int count = 1;
        while (n<=100) {
            n = (3*n1*n1 - n1)/2;
            System.out.println(n);
            n1++;
            count++;
        }
        return n;

    }

}

but this gives me errors? Is that right?

Okay, so here is what i ended up with:

import java.util.Scanner;
public class Pentagonal {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter an integer: ");
        long n = input.nextInt();
        System.out.print("The pentagonal numbers are: ");
        getPentagonalNumber(n);
    }
    public static long getPentagonalNumber(long n) {


    int n1 = 1;
    while (n<=100) {
        n = (3*n1*n1 - n1)/2;
        System.out.print(n + " ");
        n1++;
    }
    return n;
    }
}

but how do you limit the output to 5 numbers?

try this:

int n,x,n1;
//here write code for getting input to n variable
n1=1;
while (n1<=n) 
{
        x = (3*n1*n1 - n1)/2;
        System.out.print(x+" ");
        n1++;
}

hope this helps

@Learner, but where in the snippet you have provided limits the amount of numbers that get outputted? Instead of displaying all of the pentagonal numbers, how about just a few (like 5)?

Read the second line of my just above post. you have to get input into n variable and that's your limit of the pentagonal number sequences.

for example if give 5 to n then it will display first 5 pentagonal number

Read the second line of my just above post. you have to get input into n variable and that's your limit of the pentagonal number sequences.

That is the part i don't understand on making? May you show me how to do it, i have an idea on how to do it but i don't think i understand it very well.

import java.util.Scanner;
public class Pentagonal {
public static void main(String[] args) {
int n,x,n1;
Scanner input = new Scanner(System.in);
System.out.print("How Many Elements you want in the sequence ");
n = input.nextInt();
n1=1;
while (n1<=n) 
{
        x = (3*n1*n1 - n1)/2;
        System.out.print(x+" ");
        n1++;
}
}
}

Hope now you understand .

commented: OH! +10

Oh, that makes a whole lot of sense!
Last question and I will be marking this solved.

How do you make it so that there isn't an extra space at the end?

Apparently it displays like this: 1[]2[]3[]4[]5[]6[]
Rather than: 1[]2[]3[]4[]5[]6

(Those brackets are spaces)

Thanks!

you can write an additional condition like this(inside while):

if(n1==n)
System.out.print(x);
else
System.out.print(x+" ");

i don't think i did it right...:

import java.util.Scanner;
public class Pentagonal {
public static void main(String[] args) {
int n,x,n1;
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
n = input.nextInt();
n1=1;
System.out.print("The pentagonal numbers are: ");
while (n1<=n)
{
            x = (3*n1*n1 - n1)/2;
    if(n1==n)
System.out.print(x);
else
System.out.print(x+" ");
        n1++;
}   
}
}

what output you are getting ?

Here is the error log:

Your output has right initial contents, but is missing 1 characters at end
You have only a First Line Error

sdiff side by side difference your output versus solution....
Enter an integer: The pentagonal numbers are: 1 5 12 22 35    \ Enter an integer: The pentagonal numbers are: 1 5 12 22 35
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.