Solution of Challenge - 3



public class Problem3 
{
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int totalHops[] = new int[4];
        System.out.println("Enter four total no of hops ");
        for (int i = 0; i < 4; i++) 
        {
            totalHops[i] = sc.nextInt();
        }
        int count = 0;
        int totalDistance = 0;
        int stepHops[] = {20, 10, 5};
        for (int i = 0; i < 4; i++) 
        {
            for (int j = 0; j < totalHops[i]; j++) 
            {
                totalDistance = totalDistance + stepHops[count];
                ++count;
                if (count > 2) 
                {
                    count = 0;
                }
            }
            System.out.println("Total dostance cover by " + (i + 1) + " frog is " +               totalDistance);
            totalDistance = 0;
            count = 0;
        }
    }

}

Output:

Enter four total no of hops 
2
4
5
3
Total dostance cover by 1 frog is 30
Total dostance cover by 2 frog is 55
Total dostance cover by 3 frog is 65
Total dostance cover by 4 frog is 35


BUILD SUCCESSFUL (total time: 5 seconds)




No comments:

Post a Comment