In the previous article, we have seen Java Program to Check Whether a Point Exists in Circle Sector or Not
In this article, we will learn how to solve the pizza cut problem using java program language.
Java Program to Solve Pizza Cut Problem(Circle Division by Lines)
In this problem, there is given the number of cuts on pizza in input, our task is to find the number of pieces of pizza that will be present after cutting.
The number of pieces can be find out simply by using a Formula.
Maximum number of pieces = 1 + n*(n+1)/2
Where,
- n = number of cuts
Let’ see different ways to solve pizza cut problem.
Method-1: Java Program to Solve Pizza Cut Problem(Circle Division by Lines) By Using Static Input Values
Approach
- A simple approach to counting pieces is by using the above-mentioned formula.
- Declare an integer variable say ‘
n
‘ which holds the value of number of cuts. - Display the result by calling the
countMaximumPieces()
method. - Print the result.
Program:
class Main { // main method public static void main(String arg[]) { int n=5; //calling the user defined method //and pasing number of cuts as paramater countMaximumPieces(n); } // Function for finding maximum pieces // with n cuts. public static void countMaximumPieces(int n) { int pieces= 1+n*(n+1)/2; System.out.print("Maximum no. of pieces:"+ pieces); } }
Output: Maximum no. of pieces:16
Method-2: Java Program to Solve Pizza Cut Problem(Circle Division by Lines) By Using User Input Values
Approach
- A simple approach to counting pieces is by using the above-mentioned formula.
- Declare an integer variable say ‘
n
‘ which holds the value of number of cuts. - Take the user input of value of
n
. - Display the result by calling the
countMaximumPieces()
method. - Print the result.
Program:
import java.util.*; class Main { // main method public static void main(String arg[]) { //scanner class object created Scanner sc=new Scanner(System.in); System.out.print("Enter number of cuts:"); int n=sc.nextInt(); //calling the user defined method //and pasing number of cuts as paramater countMaximumPieces(n); } // Function for finding maximum pieces // with n cuts. public static void countMaximumPieces(int n) { int pieces= 1+n*(n+1)/2; System.out.print("Maximum no.of pieces:"+ pieces); } }
Output: Enter number of cuts:5 Maximum no.of pieces:16
Access the Simple Java program for Interview examples with output from our page and impress
your interviewer panel with your coding skills.
Related Java Articles:
- Java Program to Find Minimum Revolutions to Move Center of a Circle to a Target
- Java Program to Check if a Line Touches or Intersects a Circle
- Java Program to Check if a Given Circle Lies Completely Inside the Ring Formed by Two Concentric Circle
- Java Program to Find Area of a Circumscribed Circle of a Square