You are awarded from the SDSU Bookstore 1000 You can spend t

You are awarded from the SDSU Bookstore $10.00! You can spend this money on pencils, pens, and notebooks. They currently have n pencils, m pens, and k notebooks is stock. The prices are as follows: pencils: $0.12 pens: $0.90 notebooks: $3.50 How many different combinations of items can be purchased? Not all of the money has to be spend, a combination is valid as long as the total price is less than or equal to the $10.00 limit, this includes not purchasing anything at all. Solving the problem in steps: Read in integer values for the number of pencils, pens, and notebooks in stock. (For pencils you would use: int n = scan.nextInt();) You don\'t need to worry about the composition of the combinations just the number of combinations so you will want to use a counter(for example int count = 0;) to track when a valid combination has occurred. You will be iterating through each combination to check if it is valid. Start with a loop for pencils. You can have a range of 0-n pencils. Therefore, your loop will start at 0 and end once n has been exceeded: for(int pencils = 0; pencils

Solution


import java.util.*;
import java.lang.*;
import java.io.*;


class Ideone
{
   public static void main (String[] args) throws java.lang.Exception
   {
       Scanner scan = new Scanner(System.in); //Input number of pencils,pens and notebooks

System.out.println(\"Enter the number of pencils,pens and notebooks\");
       int n = scan.nextInt();
       int m = scan.nextInt();
       int k = scan.nextInt();
      
      
       int count = 0;
      
       //nested loops
       for(int pencils = 0;pencils <=n;pencils++)
       for(int pens =0; pens <=m;pens++)
       for(int notebooks =0;notebooks <=k;notebooks++)
       {
           if((pencils*12 +pens*90 + notebooks*350 )<=1000) //total cost <=1000
           count++;
          
       }
       System.out.println(\"The number of combinations : \"+count);
   }
}

output:

Success time: 0.05 memory: 711680 signal:0

 You are awarded from the SDSU Bookstore $10.00! You can spend this money on pencils, pens, and notebooks. They currently have n pencils, m pens, and k notebook

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site