sunfresh

Bunny Ears

Jan 20th, 2023
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. Bunny Ears
  2. We have a number of bunnies and each bunny has two big floppy ears. We want to compute the total number of ears across all the bunnies recursively (without loops or multiplication).
  3.  
  4. Input
  5. On the first line you will be given the number of bunnies.
  6.  
  7. Output
  8. On the only output line you should print the number of their ears.
  9.  
  10. Constraints
  11. Bunnies can`t be negative :)
  12.  
  13. Sample tests
  14. Input
  15. 0
  16. Output
  17. 0
  18. Input
  19. 1
  20. Output
  21. 2
  22.  
  23. import java.util.Scanner;
  24.  
  25. public class BunnyYears {
  26. public static void main(String[] args) {
  27. Scanner scanner = new Scanner(System.in);
  28. int n = Integer.parseInt(scanner.nextLine());
  29. System.out.println(ears(n));
  30. }
  31.  
  32. public static int ears(int n){
  33. if(n == 0){
  34. return 0;
  35. }
  36. return 2+ears(n-1);
  37. }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment