Solved Assignments and Coding Problems

   

Added on  2022-12-20

7 Pages1222 Words1 Views
Documentation
Problem 1:
Coding:
/*
* The task of the assignment is to determine whether number 0 through 9
* is even or odd
*/
using System;
namespace Assignment_3_Problem_1_solution
{
class Program
{
static void Main(string[] args)
{
// loop for X from 0 through 9
for (int X = 0; X <= 9; X++)
{
// check if the number is even
if (X % 2 == 0)
{
// display number is even
Console.WriteLine("Value " + X + " is an even number");
}
else
{
// display number is odd
Console.WriteLine("Value " + X + " is an odd number");
}
}
Console.ReadKey();
}
}
}
Output:
Problem 2:
Solved Assignments and Coding Problems_1
Coding:
/*
* The task of the assignment is to receive amount from the user until they pay
the bill they owed
*/
using System;
namespace Assignment_3_Problem_2_solution
{
class Program
{
static void Main(string[] args)
{
int BillAmount = 0;
// loop until a valid total bill amount is entered
while (BillAmount <= 0)
{
// prompt and get the bill amount
Console.Write("Enter the total due in whole dollars: ");
int.TryParse(Console.ReadLine(), out BillAmount);
}
int IllegalAttempCount = 0;
double RemainingAmount = BillAmount;
// loop until they user pays the full bill amount
while (RemainingAmount > 0)
{
// prompt and get the denomination
Console.Write(String.Format("I need {0:C} dollars: Enter a 50,
20, 10, 5 or 1: ", RemainingAmount));
String Denomination = Console.ReadLine();
// switch based on the denominatoion
switch (Denomination)
{
case "50":
RemainingAmount -= 50;
break;
case "20":
RemainingAmount -= 20;
break;
case "10":
RemainingAmount -= 10;
break;
case "5":
RemainingAmount -= 5;
break;
case "1":
RemainingAmount -= 1;
break;
default:
// increment illegal attemt count by 1
IllegalAttempCount++;
// display amount is illegal denomination
Solved Assignments and Coding Problems_2
Console.WriteLine("Illegal Denomination");
// if the illegal count is 2
if (IllegalAttempCount == 2)
{
// call the police and exit the application
Console.WriteLine("call the police");
System.Environment.Exit(1);
}
break;
}
}
// if the remaining amount is 0
if (RemainingAmount == 0)
{
// display thanking message
Console.WriteLine("Thanks for nothing");
}
else
{
// display thanking message for tip
Console.WriteLine("Thanks for the {0:C} tip!!", (-
1*RemainingAmount));
}
Console.ReadKey();
}
}
}
Output:
Problem 3:
Coding:
Solved Assignments and Coding Problems_3

End of preview

Want to access all the pages? Upload your documents or become a member.

Related Documents
Assignment 4 Solutions
|6
|1440
|1

Comparison of Iteration and Recursion in C#
|5
|903
|113

Temperature Recording Program: Java Implementation for Daily and Weekly Temperature Tracking
|7
|1100
|494

Questions on Stack Program
|11
|1305
|31