In the previous article, we have seen Java Program to Find the Vertex, Focus, Directrix of a Parabola
In this article we are going to see how to find the triangular matchstick number using Java programming language.
Java Program for Triangular Matchstick Number
Before Jumping into the program directly let’s see how we can find the triangular matchstick number.
Explanation:
Let X represents the floor of a matchstick pyramid, Formula to find triangular matchstick number = (3*x*(x+1))/2
Example:
Let x = 1
So, the number of matchstick required to form a triangular pyramid = (3*x*(x+1))/2
=> (3*1*(1+1))/2
=> (3*1*2)/2
=> 6/2 =3
Let’s see different ways to find the triangular matchstick number.
Method-1: Java Program for Triangular Matchstick Number By Using Static Value
Approach:
- Declare an integer variable say “
x
”, assign the value to it, which holds the value for the floor of a matchstick pyramid. - Find the number of matchstick required to form a triangular pyramid using the formula
(3*x*(x+1))/2
- Print the result.
Program:
class Main { public static void main(String [] args) { //value of 'x' declared int x = 1; //finding number of matchstick required to form a triangular pyramid //Using the formula (3*x*(x+1))/2 int no = (3*x*(x+1))/2; System.out.println("The number of matchstick required to form a triangular pyramid is: " + no); } }
Output: The number of matchstick required to form a triangular pyramid is: 3
Method-2: Java Program for Triangular Matchstick Number By Using User Input Value
Approach:
- Declare an integer variable say “
x
” and take the value of it as user input, which holds the value for the floor of a matchstick pyramid. - Find the number of matchstick required to form a triangular pyramid using the formula
(3*x*(x+1))/2
- Print the result.
Program:
import java.util.*; class Main { public static void main(String [] args) { //Scanner class object created Scanner sc=new Scanner(System.in); //taking input of value of 'x' System.out.print("Enter value of x(floor of match stick): "); int x = sc.nextInt(); //finding number of matchstick required to form a triangular pyramid //Using the formula (3*x*(x+1))/2 int no = (3*x*(x+1))/2; System.out.println("The number of matchstick required to form a triangular pyramid is: " + no); } }
Output: Enter value of x(floor of match stick): 2 The number of matchstick required to form a triangular pyramid is: 9
Method-3: Java Program for Triangular Matchstick Number By Using User Defined Method
Approach:
- Declare an integer variable say “
x
” and take the value of it as user input, which holds the value for the floor of a matchstick pyramid. - Then call a method
findNumber()
and passx
as the parameter. - Then inside the method, find the number of matchstick required to form a triangular pyramid using the formula
(3*x*(x+1))/2
- Print the result.
Program:
import java.util.*; class Main { public static void main(String [] args) { //Scanner class object created Scanner sc=new Scanner(System.in); //taking input of value of 'x' System.out.print("Enter value of x(floor of match stick): "); int x = sc.nextInt(); //calling findNumber() Method findNumber(x); } //findNumber() user defined method //tofind the number of matchstick required to form a triangular pyramid public static void findNumber(int x) { //finding number of matchstick required to form a triangular pyramid //Using the formula (3*x*(x+1))/2 int no = (3*x*(x+1))/2; System.out.println("The number of matchstick required to form a triangular pyramid is:" + no); } }
Output: Enter value of x(floor of match stick): 1 The number of matchstick required to form a triangular pyramid is: 3
Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.
Related Java Articles: