SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Barbara Dolye
Chapter:
Data Types And Expressions
Exercise:
Programming Exercises
Question:7 | ISBN:9781285096261 | Edition: 4

Question

Write a program that prints the number of quarters, dimes, nickels, and pennies that a customer should get back as change. Run your program once by performing a compile-time initialization using 92 cents for the value to be converted. Go into your source code and change the 92 to 27. Rerun the application.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Correct_change
{
    class Program
    {
         static void Main(string[] args)
        {
            int totalCents;
            String strCents;
            Console.WriteLine("Enter total number of cents:");
            strCents = Console.ReadLine();
            totalCents = Int32.Parse(strCents);
            int quarters = totalCents / 25;
            totalCents %= 25;
            int dimes = totalCents / 10;
            totalCents %= 10;
            int nickels = totalCents / 5;            
            int pennies = totalCents % 5;

            Console.WriteLine("quarters :"+quarters+"\ndimes :"+dimes
                               +"\nnickels :"+nickels+"\npennies :"+pennies);

            Console.ReadKey();
        }
        
    }

}

Output:

Enter total number of cents:
92
quarters :3
dimes :1
nickels :1
pennies :2

Enter total number of cents:
27
quarters :1
dimes :0
nickels :0
pennies :2

 

0 0

Discussions

Post the discussion to improve the above solution.