3.25 LAB: Exact change Write a program with total change amount as an integer input, and output the change using the fewest coins, one coin type per line. The coin types are dollars, quarters, dimes, nickels, and pennies. Use singular and plural coin names as appropriate, like 1 penny vs. 2 pennies. Ex: If the input is: 0 (or less than 0), the output is: no change Ex: If the input is: 45 the output is: 1 quarter 2 dimes

Respuesta :

Answer:

C program used for the question given below

Explanation:

#include<stdio.h>

void ExactChange(int UserValue , int coinVals[]){

  int deno[] = {100,25,10,5,1};

  int i;

  for (i = 0; i <5; i++) {

while (UserValue >= deno[i]) {

UserValue -= deno[i];

coinVals[i]++;

}

}

}

int main(){

  int n;

  int coinVals[5];

  int i;

  for(i=0;i<5;i++)coinVals[i]=0;

 

  printf("Enter value : ");

  scanf("%d",&n);

 

  ExactChange(n,coinVals);

 

  if(coinVals[0]==0&&coinVals[1]==0&&coinVals[2]==0&&coinVals[3]==0&&coinVals[4]==0)

  printf("No change\n");

 

  else{

     

      if(coinVals[0]>0)printf("%d %s\n",coinVals[0],coinVals[0]>1?"dollars":"dollar");

      if(coinVals[1]>0)printf("%d %s\n",coinVals[1],coinVals[1]>1?"quaters":"quarter");

      if(coinVals[2]>0)printf("%d %s\n",coinVals[2],coinVals[2]>1?"dimes":"dime");

      if(coinVals[3]>0)printf("%d %s\n",coinVals[3],coinVals[3]>1?"nickels":"nickel");

      if(coinVals[4]>0)printf("%d %s\n",coinVals[4],coinVals[4]>1?"pennies":"penny");

     

  }

  return 0;

}

The code is in Java.

The code uses if-else structure to check the amount of the pennies. It also uses nested if-else structure to check the amount of the each coin type. If there are corresponding values for any coin type, it converts it to that coin and prints the result.

Comments are used to explain the each line.

//Main.java

import java.util.Scanner;

public class Main{

public static void main(String[] args) {

 //Scanner object to get input from the user

 Scanner input = new Scanner(System.in);

 //Declaring the variables

 int pennies, nickels, dimes, quarters, dollars;

       

 //Getting the pennies from the user

 pennies = input.nextInt();

 

 /*

  * Checking if pennies is smaller than or equal to 0.

  * If it is, print "no change".

  * Otherwise, convert the pennies to corresponding values for dollars, quarters, dimes, nickels and pennies.

  * For example, since there are 100 pennies in a dollar, we divide the pennies to 100 to find the dollar value in pennies.

  * Then, we use modulo to get the remaining pennies.

  */

 if(pennies <= 0)

  System.out.println("no change");

 else {

  dollars = pennies / 100;

  pennies %= 100;

  quarters = pennies / 25;

  pennies %= 25;

  dimes = pennies / 10;

  pennies %= 10;

  nickels = pennies / 5;

  pennies %= 5;

 

  /*

   * After the conversion of all values, we check if the values are greater than 0.

   * This way, we find if a coin type is found in our pennies.

   * For example, if dollars value is 0, then we do not need to consider dollars because we do not have any dollars in our pennies.

   */

 

  /*

   * Then, we check if the coin type is equal to 1 or not.

   * This way, we decide whether it is plural or not.

   * And finally, we print the result.

   */

 

  if(dollars > 0){

   if(dollars == 1)

    System.out.println(dollars + " dollar");

   else

    System.out.println(dollars + " dollars");

  }

  if(quarters > 0){

   if(quarters == 1)

    System.out.println(quarters + " quarter");

   else

    System.out.println(quarters + " quarters");

  }

  if(dimes > 0){

   if(dimes == 1)

    System.out.println(dimes + " dime");

   else

    System.out.println(dimes + " dimes");

  }

  if(nickels > 0){

   if(nickels == 1)

    System.out.println(nickels + " nickel");

   else

    System.out.println(nickels + " nickels");

  }

  if(pennies > 0){

   if(pennies == 1)

    System.out.println(pennies + " penny");

   else

    System.out.println(pennies + " pennies");

  }

 }

}

}

You may see another example that uses nested if in the following link:

brainly.com/question/21891519