You are here: Home | Forum | Programming Challenges?
You are currently viewing our boards as a guest which gives you limited access to view most of the discussions, articles and other free features. By joining our Virgin Media community you will have full access to all discussions, be able to view and post threads, communicate privately with other members (PM), respond to polls, upload your own images/photos, and access many other special features. Registration is fast, simple and absolutely free so please join our community today.
Yeah but C# is just a hacked version of java, so technically sun owns your ass
I think you will find that Oracle owns Sun's burro (or very soon will), so you are now all Larry's beyatches....
__________________ Thank you for calling the Abyss.
If you have called to scream, please press 1 to be transferred to the Void, or press 2 to begin your stare. If my post is in bold and this colour, it's a Moderator Request.
for (var i = 1; i < 100; i++)
{
if (i % 3 && i % 5 == 0)
Console.WriteLine(i + " is a multiple of both 3 and 5");
if (i % 3 == 0)
Console.WriteLine(i + " is a multiple of 3");
if (i % 5 == 0)
Console.WriteLine(i + " is a multiple of 5");
}
VB.NET:
Spoiler:
Code:
Dim i As Integer
For i = 1 To 100
If i Mod 3 AndAlso i Mod 5 = 0 Then
Console.WriteLine(i + " is a multiple of both 3 and 5")
End If
If i Mod 3 = 0 Then
Console.WriteLine(i + " is a multiple of both 3")
End If
If i Mod 5 = 0 Then
Console.WriteLine(i + " is a multiple of both 3")
End If
Next i
PHP:
Spoiler:
PHP Code:
<?php for ($i=1; $i<100; $i++) { if ($i % 3 == 0) { if ($i % 5 == 0) { echo($i." is a multiple of both 3 and 5<br />"); } else { echo($i." is a multiple of 3<br />"); } } if ($i % 5 == 0) { if ($i % 3 == 0) { echo($i." is a multiple of both 3 and 5<br />"); } else { echo($i." is a multiple of 5<br />"); } } } ?>
BASIC: (Not actually 100% this works, hah!)
Spoiler:
Code:
10 FOR I$ = 1 TO 100
20 IF I$ % 3 AND I$ % 5 = 0 THEN GOTO 60
30 IF I$ % 3 = 0 THEN GOTO 70
40 IF I$ % 5 = 0 THEN GOTO 80
50 NEXT I$
60 PRINT I$ "is a multiple of both 3 and 5"
70 PRINT I$ "is a multiple of 3"
80 PRINT I$ "is a multiple of 5"
90 END
We need another challenge, of the Monty Hall Type...
I've got another one. It's a mortgage calculator. Say you have a mortgage of £100,000. Specify an interest rate and monthly repayment amount. What you want to determine is the number of months required to pay off the balance, keeping in mind that you will accrue interest on the outstanding balance. My solution is 8 lines long. You can check your solution against the calculators floating around on the web.
__________________
Remember kids: We are blessed with a listening, caring government.
I've got another one. It's a mortgage calculator. Say you have a mortgage of £100,000. Specify an interest rate and monthly repayment amount. What you want to determine is the number of months required to pay off the balance, keeping in mind that you will accrue interest on the outstanding balance. My solution is 8 lines long. You can check your solution against the calculators floating around on the web.
Cool. I'll do that soonish. Is this flat rate interest or is this ongoing? So is the interest calculated by a percentage of the total amount? (i.e 10% of 100,000)?
BTW, Here's my attempt at the Monty Hall solution. I did via Object-Orientated programming, but its probably a bit more cumbersome than Damien's attempt. Still at least its good simple intro it using it as most of the important base principals are used. Its not pure OO though but thereabouts
Spoiler:
Code:
namespace PCTestMontyHall
{
//class to represent the door
class Door
{
public int doorNumber;
public Boolean open = false;
public String prize;
//constructor, this happens when a door is created
public Door(int doorNumber, String prize)
{
this.doorNumber = doorNumber;
this.prize = prize;
}
//this is called when we Console.WriteLine a door. We have to override the default ToString() method
public override String ToString()
{
//conditional shorthand. If open is true then it sets "Open" to status. If not it sets "Closed" to status
String status = open ? "Open" : "Closed";
return "Door " + doorNumber + ": " + prize + " (" + status + ")";
}
}
class Program
{
//create a random number generator
public static Random random = new Random();
static void Main(string[] args)
{
//set initial counters and parameters
int ferraris = 0;
int goats = 0;
int gamesToPlay = 1000;
bool contestantSwapping = false;
//play the game
for (int i = 0;i<gamesToPlay;i++)
{
//true parameter means contestant will swap, false means he doesn't.
string result = PlayGame(contestantSwapping);
if (result == "Ferrari")
{
ferraris++;
}
else
{
goats++;
}
}
//print out statistics
Console.WriteLine("Contestant swapping? " + contestantSwapping);
float ferrariPercentage = ((float)ferraris / gamesToPlay) * 100;
float goatPercentage = ((float)goats / gamesToPlay) * 100;
Console.WriteLine(String.Format("\n\nFerraris: {0} {1}%\nGoats: {2} {3}%", ferraris, ferrariPercentage, goats, goatPercentage));
//Hit enter to quit
Console.ReadLine();
}
//the actual game
static string PlayGame(Boolean swapChoice)
{
//create an array to hold the doors
Door[] doors = new Door[3];
//set contestant's choice of door randomly
int contestantsChoice = random.Next(0, 3);
//add door objects to the array of doors
for (int i = 0;i < doors.Length; i++)
{
doors[i] = new Door(i+1,"Goat");
}
//choose which door a Ferrari should be behind
int ferrari = random.Next(0, 3);
doors[ferrari].prize = "Ferrari";
//host has to remove a choice
int removeChoice;
//keep chosing a door at random until the door contains a goat and isn't the same as the contestants choice
do
{
removeChoice = random.Next(0, 3);
} while ((doors[removeChoice].prize == "Ferrari")|| (removeChoice == contestantsChoice));
//"open" the door
doors[removeChoice].open = true;
//if contestant is swapping, then choose the other door with a goat
int oldChoice = contestantsChoice;
if (swapChoice)
{
do
{
contestantsChoice = random.Next(0, 3);
} while ((doors[contestantsChoice].open == true) || (contestantsChoice == oldChoice));
}
//return the prize as a result
return doors[contestantsChoice].prize;
}
//print out the status of the doors. I added this as a debug whilst writing but illustrates overriding the default ToString method
static void ShowDoors(Door[] doors)
{
Console.WriteLine("Doors:");
for (int i = 0; i < doors.Length; i++)
{
Console.WriteLine(doors[i]);
}
}
}
}
Result:
Contestant swapping? False
Ferraris: 330 33%
Goats: 670 67%
Contestant swapping? True
Ferraris: 671 67.1%
Goats: 329 32.9%
I'll have a go at the mortgage calculator tonight probably.
Nice Punky. It's only not 'proper' OO because it's limited in scope (i.e the project is too small) to use many features. OO becomes more central to your application structure in bigger applications anyway. I mean all you can do is make door an object really...(which you did, smart).
You probably know this but when accessing a variable outside of it's containing class you should make it a property..
//c++
#include <iostream>
using namespace std;
int main() {
float interest;
float mortgage=100000;
float repayment;
long i=1;
cout << "Please enter the interest rate in number format sans % mark.\n";
cin >> interest;
cout << "Ok, Interest rate is set to " << interest << "% \nPlease enter the Mortgage payback amount per month sans delimiters.\n";
cin >> repayment;
do {
mortgage = mortgage - repayment;
if (i % 12 == 0){
mortgage = mortgage*(1+(interest/100));
}
++i;
} while (mortgage > 0);
cout << "100,000 repaid at " << repayment << " per month would take " << i << " months to pay off";
}