Skip to content

Commit bf0c63a

Browse files
committed
feat: Add FunctionsCC3 class with calculateSum using user input
WHAT the code does: - Defines a `FunctionsCC3` class with a static method `calculateSum(int num1, int num2)` that returns the sum of two numbers. - Uses `Scanner` to read two integers from the user. - Calls `calculateSum()` with the input values and prints the result in a formatted message. WHY this matters: - Demonstrates combining **user input handling** with method calls. - Shows how to pass values dynamically to methods instead of hardcoding. - Reinforces the distinction between returning results vs printing inside the method. HOW it works: 1. Program prompts for `"Enter first number:"` and `"Enter second number:"`. 2. Reads values `a` and `b` via `Scanner`. 3. Passes them into `calculateSum(a, b)` → computes and returns sum. 4. Prints result in format: `"Sum = X"`. Tips & gotchas: - `Scanner` should be closed after use (`sc.close()`). - Input validation (e.g., handling non-integer input) is not included. - Works only for integers — could be extended to handle doubles. - For larger numbers, `long` may be safer to prevent overflow. Use-cases: - Educational example for **method with return value + user input**. - Foundation for calculator-style programs. - Can be extended with additional operations (subtract, multiply, divide). - Useful for practicing console-based interactive applications. Short key: class-functions cc3-calculate sum-user input. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 9b13f8e commit bf0c63a

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed
Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
//Print the returned value directly.
1+
import java.util.Scanner;
22

33
public class FunctionsCC3 {
4-
public static int calculateSum(int num1, int num2)
5-
{
4+
public static int calculateSum(int num1, int num2) {
65
return num1 + num2;
76
}
7+
88
public static void main(String[] args) {
9-
System.out.println(calculateSum(3, 4));
9+
Scanner sc = new Scanner(System.in);
10+
System.out.print("Enter first number: ");
11+
int a = sc.nextInt();
12+
13+
System.out.print("Enter second number: ");
14+
int b = sc.nextInt();
15+
16+
System.out.println("Sum = " + calculateSum(a, b));
1017
}
11-
}
18+
}

0 commit comments

Comments
 (0)