Practice Exercises C# Sharp - Learn to program with performing exercises C# Sharp

Practice Exercises C# Sharp

Learn to program with performing exercises C# Sharp

Give change - Practice Exercises C# Sharp
Practice Exercises C# Sharp4,85320940

Give change - Practice Exercises C# Sharp


Lesson 2:

Flow Control


Exercise 2.34:

Give change


Objetive:

Create a C# program to return the change of a purchase, using coins (or bills) as large as possible. Assume we have an unlimited amount of coins (or bills) of 100, 50, 20, 10, 5, 2 and 1, and there are no decimal places. Thus, the execution could be something like this:

Price? 44
Paid? 100
Your change is 56: 50 5 1
Price? 1
Paid? 100
Your change is 99: 50 20 20 5 2 2


Source Code:


using System;
public class Exercise053
{
    public static void Main()
    {
        int price, paid, change;

        Console.Write("Price? ");
        price = Convert.ToInt32(Console.ReadLine());
        Console.Write("Paid? ");
        paid = Convert.ToInt32(Console.ReadLine());

        change = paid - price;
        Console.Write("Your change is {0}: ", change);
        while (change > 0)
        {
            if (change >= 50)
            {
                Console.Write("50 ");
                change -= 50;
            }
            else
            {
                if (change >= 20)
                {
                    Console.Write("20 ");
                    change -= 20;
                }
                else
                {
                    if (change >= 10)
                    {
                        Console.Write("10 ");
                        change -= 10;
                    }
                    else
                    {
                        if (change >= 5)
                        {
                            Console.Write("5 ");
                            change -= 5;
                        }
                        else
                        {
                            if (change >= 2)
                            {
                                Console.Write("2 ");
                                change -= 2;
                            }
                            else
                            {
                                Console.Write("1 ");
                                change -= 1;
                            }
                        }
                    }
                }
            }
        }
        Console.WriteLine();
    }
}
Exercisey 2.34

Share Now:







Privacy Policy:



Google uses associated advertising companies to serve ads when it visits our website. These companies may use the information they obtain from your visits to this and other websites (not including your name, address, email address, or phone number) to provide you with announcements about products and services that interest you. If you would like to learn more about this practice and know your options to prevent these companies from using this information. Click in... Privacy and Terms of Google.

Cookies

This site uses Google cookies to provide its services, to personalize advertisements and to analyze traffic. Google receives information about your use of this website. More information in... Privacy and Terms of Google.