[Super beginner] Here's the n-win code for 2.

Asked 2 years ago, Updated 2 years ago, 19 views

import java.util.*; public class exam6_3_1 {

public static void main(String[] args) {
    // // TODO Auto-generated method stub
    System.out.print ("Enter a : ");
    Scanner sc = new Scanner(System.in);
    int a = sc.nextInt();
    System.out.println ("2's "+a+" multiplication = "+factor(a)));
}
public static int factor(int num) {
    if (num==0)
        return 1;
    else
        return 2*factor(num-1);
}

}

I wrote it by applying the Factory Al code that I made before. factoryal code → return n*factor (num-1); I just put 2 instead of n without thinking much, but it worked out. I don't understand why the problem is printed properly.

For example, if you enter 5, shouldn't you get 48 as 2*4*3*2*1... Masters, please.

java

2022-09-21 16:11

1 Answers

It's weird that coding doesn't work, but it's weird that it doesn't LOL What the factor() function does here is:

Therefore:

factor(5) = 2*(factor(4)) = 2*(2*(factor(3))) = ... = = 2*2*2*2*2*1 = 2^5


2022-09-21 16:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.