In: Computer Science
Problem 9: Duck Duck Goose JAVA
Duck, duck, goose is a child's game where kids sit in a circle,
facing inward, while another
player, walks around calling each a "duck" until finally calling
one a "goose". Given the number
of kids seated in a circle, and the number of ducks called out,
determine which child is the
goose.
Facts:
● The player circles the group in clockwise rotation
● Label the children numerically.
○ The first duck called is labeled: child 0
○ To the left of child 0 is child n-1 , where n is the number of
children
Input
You are given two positive integer inputs. The first represents the
number of kids in the circle.
The second represents the number of ducks that are called before
goose.
Output
Print as an integer which child was selected as goose.
Sample Input Sample output
7 15 1
4 4 0
12 6 6
DuckDuckGoose.java
import java.util.Scanner;
public class DuckDuckGoose {
public static void main(String[] args) {
// Scanner class object to get the
input from user
Scanner input=new
Scanner(System.in);
System.out.print("Enter the number
of kids in the circle: ");
int numberOfKids=input.nextInt();
// accept the number of kids from user
System.out.print("Enter the number
of ducks called before goose: ");
int numberOfDucks=input.nextInt();
// accept the number of ducks called before goose
int gooseSelected;
// if goose called in first
round
if(numberOfKids>=numberOfDucks)
gooseSelected=numberOfKids-numberOfDucks;
else
// if goose
called in other then first round
gooseSelected=numberOfDucks%numberOfKids;
// print the child number which was
selected as goose
System.out.println("\nChild number
who was selected as goose is "+gooseSelected);
input.close();
}
}
Sample run 1
Sample run 2
Sample run 3