Skip to content

Commit ec5d2ec

Browse files
committed
feat: Compute factorial of a number using a for loop
🧠 Logic: - Read user input `n`. - Initialize `fact` as 1 (to hold the factorial result). - Loop from 1 to `n`, multiplying `fact` by the loop counter `i` at each step. - After the loop, print the final factorial value. Signed-off-by: Somesh diwan <[email protected]>
1 parent fded6ba commit ec5d2ec

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

Section8LoopAB/src/ForLoopAB6.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ public class ForLoopAB6 {
77
public static void main(String[] args) {
88
Scanner sc = new Scanner(System.in);
99
System.out.println("Enter a Number: ");
10+
1011
int n = sc.nextInt();
12+
1113
long fact =1;
12-
for(int i =1; i<=n; i++)
13-
{
14+
for(int i =1; i<=n; i++) {
1415
fact = fact * i;
1516
}
1617
System.out.println("Factorial of number is: "+fact);

0 commit comments

Comments
 (0)