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:
Enter the price
44
Enter the money given
100
Your change is 56
Number of 50s returned is 1
Number of 5s returned is 1
Number of 1s returned is 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Giving_Change
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the price");
int price = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the money given");
int moneyGiven = int.Parse(Console.ReadLine());
int change = moneyGiven - price;
string message = "Your change is " + change + "\n";
int fifty = 0;
int twenty = 0;
int ten = 0;
int five = 0;
int two = 0;
int one = 0;
string msg50 = "";
string msg20 = "";
string msg10 = "";
string msg5 = "";
string msg2 = "";
string msg1 = "";
while (change > 0)
{
if (change >= 50)
{ change -= 50;
fifty += 1;
msg50 = "Number of 50s returned is " + fifty + "\n";
}
if (change >= 20)
{
change -= 20;
twenty += 1;
msg20 = "Number of 20s returned is " + twenty + "\n";
}
if (change >= 10)
{
change -= 10;
ten += 1;
msg10 = "Number of 10s returned is " + ten + "\n";
}
if (change >= 5)
{
change -= 5;
five += 1;
msg5 = "Number of 5s returned is " + five + "\n";
}
if (change >= 2)
{
change -= 2;
two += 1;
msg2 = "Number of 2s returned is " + two + "\n";
}
if (change == 1)
{
change -= 1;
one += 1;
msg1 = "Number of 1s returned is " + one;
}
}
string answer = message + msg50 + msg20 + msg10 + msg5 + msg2 + msg1;
Console.WriteLine(answer);
Console.Read();
}
}
}
Hi. I'm Aadi Gupta. I'm 13 and my main hobbies are badminton, cricket and cycling. I also do robotics and programming. Read my posts and tell me what you think!
Wednesday, 27 July 2016
Several Multiplication Tables
Display a range of multiplication tables, as long as the user asks for... The first input is the start of the range and the second number is the end. For example if you input 2 and then 6, the computer will display the multiplication tables of 2, 3, 4, 5 and 6. The third number represents how many numbers you want in each table. f\or example if you then put the third number as 7, the computer will display the tables from 2 to 6 and the table will go from 2x1 to 2x7 then move on to 3x1 etc.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Multiplication
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter your start point of the table ");
int number = int.Parse(Console.ReadLine());
Console.WriteLine("Enter your end point of the table ");
int amount = int.Parse(Console.ReadLine());
Console.WriteLine("till what number each table to show");
int length = int.Parse(Console.ReadLine());
int n = number;
int x = 1;
int a = 1;
while (number <= amount)
{
while ( x <= length)
{
Console.WriteLine("{0} x {1} = {2}", number, x, x*number);
x += 1;
}
number += 1;
x = 1;
}
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Multiplication
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter your start point of the table ");
int number = int.Parse(Console.ReadLine());
Console.WriteLine("Enter your end point of the table ");
int amount = int.Parse(Console.ReadLine());
Console.WriteLine("till what number each table to show");
int length = int.Parse(Console.ReadLine());
int n = number;
int x = 1;
int a = 1;
while (number <= amount)
{
while ( x <= length)
{
Console.WriteLine("{0} x {1} = {2}", number, x, x*number);
x += 1;
}
number += 1;
x = 1;
}
Console.ReadLine();
}
}
}
Subscribe to:
Posts (Atom)