Skip to content

Commit 4a09e17

Browse files
committed
feat: Add FunctionCC5 class with printSquare method
WHAT the code does: - Defines a `FunctionCC5` class with a static method `printSquare(int num)`. - `printSquare()` computes the square of the input number and prints the result. - `main()` calls `printSquare(5)` as a demonstration. WHY this matters: - Demonstrates **user-defined methods** in Java. - Reinforces arithmetic operations and method parameter passing. - Shows how to separate computation logic into reusable methods. HOW it works: 1. `printSquare(5)` → computes `5 * 5 = 25`. 2. Prints → `"Square of 5 is: 25"`. Tips & gotchas: - Currently prints result only; could be extended to return the value for reuse. - Works only for integers; using `long` or `BigInteger` avoids overflow for large inputs. - Negative inputs are valid (e.g., `-3` → `9`). - Method name `printSquare` is descriptive, aligning with best practices. Use-cases: - Educational example for methods with parameters. - Utility method for math-related applications. - Starter function to extend into more math operations (cube, power, etc.). - Interview prep for **basic function implementation**. Short key: class-function cc5-print square. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent b48f970 commit 4a09e17

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed
Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
/*Printing Output from Functions
2-
A Method can also directly print output to the console using print statements.
3-
However, this doesn't provide any data back to the caller in a way that can be used elsewhere in the program.
4-
The primary purpose here is to display information, not to provide data for further processing.
5-
6-
Check the example below which gives the same output as the code above:*/
7-
8-
public class FunctionCC5
9-
{
10-
public static void main(String[] args)
11-
{
1+
public class FunctionCC5 {
2+
public static void main(String[] args) {
123
printSquare(5);
134
}
145

15-
// User-defined method to print the square of a number
16-
public static void printSquare(int num)
17-
{
6+
// User-defined method to print the square of a number.
7+
public static void printSquare(int num) {
188
int squareResult = num * num;
199
System.out.println("Square of " + num + " is: " + squareResult);
2010
}
21-
}
11+
}
12+
13+
/* Printing Output from Functions:
14+
A Method can also directly print output to the console using print statements.
15+
16+
However, this doesn't provide any data back to the caller in a way that can be used elsewhere in the program.
17+
The primary purpose here is to display information, not to provide data for further processing.
18+
19+
Check the example above which gives the same output as the above.
20+
*/

0 commit comments

Comments
 (0)