In the previous article, we have seen Java Program to Find Area of Hexagon
In this article we are going to see how to find the nth pentagonal number using Java programming language.
Java Program to Find n’th Pentagonal Number
Before Jumping into the program directly let’s see how we can find the nth pentagonal number.
Explanation:
Formula to find nth pentagonal number = 3*n*(n-1)/2 + n
Example:
Example-1 If n = 1 So, nth pentagonal number = 3*n*(n-1)/2 + n = 3*1*(1-1)/2 + 1 = 0+1 = 1 Example-2 If n = 10 So, nth pentagonal number = 3*n*(n-1)/2 + n = 3*10*(10-1)/2 + 10 = 30*9/2+10 = 30*4.5+10 = 135+10 = 145
Let’s see different ways to find the nth pentagonal number.
Method-1: Java Program to Find n’th Pentagonal Number By Using Static Value
Approach:
- Declare an integer variable say ‘
n
‘, assign the value to it, which holds the value for nth pentagonal number. - Find the nth pentagonal number using the formula
3*n*(n-1)/2 + n
- Print the result.
Program:
class Main { public static void main(String [] args) { //n value declared int n = 10; //finding nth pentagonal number double f = (3*n*(n-1)/2) + n; System.out.println("The nth pentagonal number is: " + f); } }
Output: The nth pentagonal number is: 145.0
Method-2: Java Program to Fins n’th Pentagonal Number By Using User Input Value
Approach:
- Declare an integer variable say ‘
n
‘ and take the value of it as user input, which holds the value for nth pentagonal number. - Find the nth pentagonal number using the formula
3*n*(n-1)/2 + n
- Print the result.
Program:
import java.util.*; class Main { public static void main(String [] args) { //Scanner class object created Scanner s = new Scanner(System.in); //taking the value of 'n' as user input System.out.println("Enter the value of n to find the nth pentagonal number: "); int n = s.nextInt(); //finding nth pentagonal number double f = (3*n*(n-1)/2) + n; System.out.println("The nth pentagonal number is: " + f); } }
Output: Enter the value of n to find the nth pentagonal number: 5 The nth pentagonal number is: 35.0
Method-3: Java Program to Fins n’th Pentagonal Number By Using User Defined Method
Approach:
- Declare an integer variable say ‘
n
‘ and take the value of it as user input, which holds the value for nth pentagonal number. - Then call the user defined method
pentagon()
and pass ‘n
‘ as parameter. - Then inside the method find the nth pentagonal number using the formula
3*n*(n-1)/2+n
- Print the result.
Program:
import java.util.*; class Main { public static void main(String [] args) { //Scanner class object created Scanner s = new Scanner(System.in); //taking the value of 'n' as user input System.out.println("Enter the value of n to find the nth pentagonal number: "); int n = s.nextInt(); //calling the user defined method pentagon() pentagon(n); } //pentagon() method to find nth pentagonal number public static void pentagon(int n) { //finding nth pentagonal number double f = (3*n*(n-1)/2) + n; System.out.println("The nth pentagonal number is: " + f); } }
Output: Enter the value of n to find the nth pentagonal number: 3 The nth pentagonal number is: 12.0
The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.
Related Java Articles: