Cake Number in Java24 Sept 2025 | 7 min read In this section, we will learn about finding the cake number in Java. The cake number CKn represents the maximum number of pieces a cake can be divided with the help of n planar cuts. The tridimensional version of the pancake numbers is the cake numbers. In general, CKn = (n3 + 5 *n + 6) / 6 where n >= 0 Thus, For n = 0, CK0 = (03 + 5 * 0 + 6) / 6 = (6) / 6 = 6 / 6 = 1 For n = 1, CK1 = (13 + 5 * 1 + 6) / 6 = (1 + 5 + 6) / 6 = 12 / 6 = 2 For n = 2, CK2 = (23 + 5 * 2 + 6) / 6 = (8 + 10 + 6) / 6 = 24 / 6 = 4 For n = 3, CK3 = (33 + 5 * 3 + 6) / 6 = (27 + 15 + 6) / 6 = 48 / 6 = 8 For n = 4, CK4 = (43 + 5 * 4 + 6) / 6 = (64 + 20 + 6) / 6 = 90 / 6 = 15 For a = 5, CK5 = (53 + 5 * 5 + 6) / 6 = (125 + 25 + 6) / 6 = 156 / 6 = 26 ImplementationLet's see how one can implement the above mathematical formula. Observe the following example. FileName: CakeNumberExample1.java Output: The first 30 cake numbers are: 1 2 4 8 15 26 42 64 93 130 176 232 299 378 470 576 697 834 988 1160 1351 1562 1794 2048 2325 2626 2952 3304 3683 4090 Using RecursionThe cake numbers can also be found using recursion. However, to find cake numbers using recursion, we must know the recursive formula of it. CK0 = 1, for n = 0 CK1 = 2, for n = 1 CKn = n + 1C3 + n + 1, for n >= 2 Thus, For n = 2 ![]() ImplementationLet's see how one can implement the above mathematical formula. Observe the following example. FileName: CakeNumberExample2.java Output: The first 30 cake numbers are: 1 2 4 8 15 26 42 64 93 130 176 232 299 378 470 576 697 834 988 1160 1351 1562 1794 2048 2325 2626 2952 3304 3683 4090 Using IterationThe cake numbers can also be found using iteration. We will use loops to find the cake numbers with the help of a two-dimensional array. Observe the following program. FileName: CakeNumberExample3.java Output: The first 30 cake numbers are: 1 2 4 8 15 26 42 64 93 130 176 232 299 378 470 576 697 834 988 1160 1351 1562 1794 2048 2325 2626 2952 3304 3683 4090 Another ApproachApart from the above-discussed approach, there is another approach to compute the cake numbers using combinations. Observe the following mathematical statement. CK0 = 1, for CK1 = 2, for CK2 = 4, for CKn = nC3 + nC2 + nC1 + nC0, for n >= 3 Thus, For n = 3, ![]() ImplementationLet's see the recursive as well as the iterative implementation of the above formula to find the cake numbers in the code mentioned below. FileName: CakeNumberExample4.java Output: The first 30 cake numbers are: 1 2 4 8 15 26 42 64 93 130 176 232 299 378 470 576 697 834 988 1160 1351 1562 1794 2048 2325 2626 2952 3304 3683 4090 The first 30 cake numbers are: 1 2 4 8 15 26 42 64 93 130 176 232 299 378 470 576 697 834 988 1160 1351 1562 1794 2048 2325 2626 2952 3304 3683 4090 Next TopicJava-throwable-addsuppressed-method |
In the context of Reactor and the Spring ecosystem, a Mono is a fundamental building block for reactive programming. It represents a stream of zero or one element and is part of Project Reactor that provides a foundation for building reactive applications on the Java Virtual...
3 min read
Queues are fundamental data structures utilized in computer science and programming. They follow the First-In-First-Out (FIFO) principle, wherein the first object delivered may be removed first. Many programming languages, together with Java, implement queues through the Queue interface. The queue interface provides a variety of methods...
4 min read
In Java, null is a reserved word for literal values. It seems like a keyword, but actually, it is a literal similar to true and false. Points to remember It is case-sensitive. It is a value of the reference variable. The access to a null reference generates a NullPointerException. It...
1 min read
The java.io.ObjectInputStream class is used to deserialize objects and primitive data iously serialized using the ObjectOutputStream. It allows the reconstruction of object graphs and ensures compatibility between the serialized object's class and the current JVM (Java Virtual Machine) class definitions. ObjectOutputStream and ObjectInputStream collaborate to save and...
22 min read
In Java, the ">>>" operator is the right shift zero fill operator. When using the right shift operator in Java, the bits of a number are shifted to the right, and any bits that are shifted beyond the right end are discarded. The bits shifted from...
3 min read
Padovan sequence as a tremendous and innovative mathematical sequence that differential from the well-known Fibonacci sequence although has its strong relation. It is said to be defined by a recurrence relation based on the fact that any term is the sum of two terms of the...
5 min read
ASCII (American Standard Code for Information Interchange) is a character encoding system used to represent text in computers and other devices. It assigns a unique numeric value to character, letters, numbers, punctuation, and other symbols. A seven-bit binary code is used to represent each character...
5 min read
A full-stack developer is a person who can develop application's backend and frontend. Java full-stack is basically a term used for a web developer that uses Java to develop the entire technology stack is referred to as Java full stack developer. A developer should have the following...
8 min read
? File: SwapWithoutTemp .java public class SwapWithoutTemp { public static void main(String args[]) { String a = "Love"; String b = "You"; System.out.println("Before swap: " + a + " " + b); a = a + b; b = a.substring(0, a.length() - b.length()); a = a.substring(b.length()); System.out.println("After : " + a + " " +...
1 min read
In the realm of secure communication and data protection, cryptographic libraries play a pivotal role. One such library that has gained significant recognition is the Bouncy Castle for Java. This library provides a comprehensive set of cryptographic algorithms and protocols, offering developers a robust foundation for...
4 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India

