Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Bunny Ears
- 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).
- Input
- On the first line you will be given the number of bunnies.
- Output
- On the only output line you should print the number of their ears.
- Constraints
- Bunnies can`t be negative :)
- Sample tests
- Input
- 0
- Output
- 0
- Input
- 1
- Output
- 2
- import java.util.Scanner;
- public class BunnyYears {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine());
- System.out.println(ears(n));
- }
- public static int ears(int n){
- if(n == 0){
- return 0;
- }
- return 2+ears(n-1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment