Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
257 changes: 254 additions & 3 deletions CSharpExercises/Exercises.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,56 @@ public static string HelloWorld()
/* Alright - your turn now! */

// 1. Create a method called SayHello that accepts a string representing a name and returns a welcome message (E.g. "Hello John!")
public static string SayHello(string name)
{
return ("Hello " + name + "!");
}
// 2. Create a method called Sum that accepts two integers and returns their sum.
public static int Sum(int num1, int num2)
{
return (num1 + num2);
}
// 3. Create a method called Divide that accepts two decimals and returns the result of dividing the two numbers as a decimal.
public static decimal Divide(decimal num1, decimal num2)
{
return (num1 / num2);
}
// 4. Create a method called CanOpenCheckingAccount that accepts an integer representing a customers age, returning true if the age is greater than or equal to 18, or false if the argument is less than 18.
public static bool CanOpenCheckingAccount(int age)
{
if (age >= 18)
{
return true;
}
else {
return false;
}
}
// 5. Create a method called GetFirstName that accepts a string representing a full name (e.g. "John Smith"), and returns only the first name.
public static string GetFirstName(string fullname)
{
string firstname = fullname.Split(' ')[0];
return firstname;
}
// 6. Create a method called ReverseStringHard that accepts a string and returns the string in reverse. (No built in functions allowed)
public static string ReverseStringHard(string phrase)
{
char[] s = phrase.ToCharArray();
string reverse = String.Empty;
for (int i = s.Length - 1; i > -1; i--)
{
reverse += s[i];

}
return reverse;
}
// 7. Create a method called ReverseStringEasy that accepts a string and returns the string in reverse. (Using only built in functions)
public static string ReverseStringEasy(string phrase)
{
char[] charArray = phrase.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
// 8. Create a method called PrintTimesTable that accepts an integer and returns the times table as a string for that number up to the 10th multiplication.
/* e.g. 10 should return
* 10 * 1 = 10
Expand All @@ -36,10 +80,59 @@ public static string HelloWorld()
* 10 * 8 = 80
* 10 * 9 = 90
* 10 * 10 = 10 */
public static string PrintTimesTable(int number)
{

//StringBuilder sb = new StringBuilder();

// 9. Create a method called ConvertKelvinToFahrenheit that accepts a double representing a temperature in kelvin and returns a double containing the temperature in Fahrenheit.
//for (int i = 1; i <= 10; i++)
//{

// sb.AppendLine($"{number} * {i} = {i * number}");

//}

//Console.WriteLine(sb.ToString());
//Console.ReadLine();
//return sb.ToString();


string timestable = String.Empty;

for (int i = 1; i < 10; i++)
{

timestable += $"{number} * {i} = {i * number}\r\n";
}
return timestable += $"{number} * 10 = " + number * 10;

}


// 9. Create a method called ConvertKelvinToFahrenheit that accepts a double representing a temperature in kelvin and returns a double containing the temperature in Fahrenheit.\
public static double ConvertKelvinToFahrenheit(double temp)
{
//Console.WriteLine(((temp - 273) * 9d/ 5) + 32);
//Console.ReadLine();
double result = temp * 9.0 / 5.0 - 459.67;
return Math.Round(result, 2);
}
// 10. Create a method called GetAverageHard that accepts an array of integers and returns the average value as a double. (No built in functions allowed)
public static double GetAverageHard(int[] args)
{
double sum = 0;
for (int i = 0; i < args.Length; i++)
{
sum += args[i];
}

return sum/args.Length;
}
// 11. Create a method called GetAverageEasy that accepts an array of integers and returns the average value as a double. (Using only built in functions)
public static double GetAverageEasy(int[] args)
{
return args.Average();
}
// 12. Create a method called DrawTriangle that accepts two integers - number and width and returns a string containing a drawn triangle using the number parameter.
/* e.g. Number: 8, Width: 8 should return
* 88888888
Expand All @@ -50,21 +143,153 @@ public static string HelloWorld()
* 888
* 88
* 8 */
public static string DrawTriangle(int number, int width)
{
string triangle = string.Empty;

for (int i = 0; i < width; i++)
{

for (int j = 0; j < width; j++)
{
if (j <= ((width - 1) - i))
{
triangle += number;
}
//else
//{
// triangle += " ";
//}
}

if (i != (width - 1))
{
triangle += "\r\n";
}
}

//string triangle = "";
//int rows = width;
//for (int i = 0; i < rows - 1; i++)
//{
// for (int j = 0; j < width; j++)
// {
// triangle += number;
// }
// triangle += "\r\n";
// width--;
//}
//return triangle + number;
//Console.WriteLine(triangle);
//Console.Write(Constants.TriangleFor8and5);
//Console.ReadLine();
return triangle;
}

// 13. Create a method called GetMilesPerHour that accepts a double representing distance and three integers representing hours, minutes and seconds. The method should return the speed in MPH rounded to the nearest whole number as a string. (e.g. "55MPH")


// 13. Create a method called GetMilesPerHour that accepts a double representing distance and three integers representing hours, minutes and seconds. The method should return the speed in MPH rounded to the nearest whole number as a string. (e.g. "55MPH")
public static string GetMilesPerHour(double num, int hours, int minutes, int seconds)
{
double time = hours + minutes / 60.0 + seconds / 3600.0;
double speed = Math.Round(num / time, 0);
return $"{speed}MPH";

}

// 14. Create a method called IsVowel that accepts a char parameter and returns true if the parameter is a vowel or false if the parameter is a consonant.
public static bool IsVowel(char c)
{
bool vowel = false;
char[] vArray = new char[] {'a', 'e', 'i', 'o', 'u' };
for (int i = 0; i < vArray.Length; i++)
{
if (c == vArray[i])
{
vowel = true;
}
}
return vowel;
}
// 15. Create a method called IsConsonant that accepts a char parameter and returns true if the parameter is a consonant or false if the parameter is a vowel.
public static bool IsConsonant(char c)
{
bool vowel = true;
char[] vArray = new char[] { 'a', 'e', 'i', 'o', 'u' };
for (int i = 0; i < vArray.Length; i++)
{
if (c == vArray[i])
{
vowel = false;
}
}
return vowel;
}
// 16. The Collatz conjecture, named after Lothar Collatz of Germany, proposed the following conjecture in 1937.
// Beginning with an integer n > 1, repeat the following until n == 1. If n is even, halve it. If n is odd, triple it and add 1. Following these steps, the function will always arrive at the number 1.
// Create a method called CollatzConjecture that accepts an integer and returns the number of steps required to get to n == 1 as an integer.

public static int CollatzConjecture(int num)
{
int steps = 0;
while (num != 1)
{
if (num % 2 == 0)
{
num /= 2;
}
else
{
num = num * 3 + 1;
}
steps++;
}
return steps;
}
// 17. Create a method called GetNext7Days that accepts a DateTime object and returns an array of DateTime objects containing the next 7 days (including the given day).
public static DateTime[] GetNext7Days(DateTime date)
{
int numDays = 7;
DateTime[] nextDays= new DateTime[numDays];

for (int i = 0; i < nextDays.Length; i++)
{
nextDays[i] = date;
date = date.AddDays(1);
}
return nextDays;
}
// 18. Create a method called IsInLeapYear that accepts a DateTime object and returns true if the date falls within a leap year and false if not. (No built in functions allowed)
public static bool IsLeapYear(int date)
{
bool isLeap = false;
if (date % 4 == 0 && (date % 100 != 0 || date % 400 == 0))
{
return true;
}
return isLeap;
}
// 19. Create a method called MortgageCalculator that accepts 2 decimals representing loan balance and interest rate, an integer representing loan term in years, and an integer representing the payment period.
/* Payment periods: 1 - Monthly, 2 - Bi-Monthly (Every 2 months) */
public static double MortgageCalculator(decimal loanBalance, decimal rate, int years, int paymentPeriod)
{
double monthlyPayment = 0;
double lb = decimal.ToDouble(loanBalance);
double r = decimal.ToDouble(rate);

int y = years;
int n = paymentPeriod;
if (n == 12)
{
double numPayments = y * 12;
monthlyPayment = ((r / 1200) + (r / 1200) / (Math.Pow(1 + (r / 1200), numPayments) - 1)) * lb;
}
else
{
double numPayments = y * 6;
monthlyPayment = ((r / 600) + (r / 600) / (Math.Pow(1 + (r / 600), numPayments) - 1)) * lb;
}
return Math.Round(monthlyPayment, 2);
}
// 20. Create a method called DuckGoose that accepts an integer. Iterate from 1 to this integer, building a string along the way.
// If the current number in the iteration:
// Is divisible by 3, append "Duck" + Environment.NewLine; to the string.
Expand Down Expand Up @@ -93,6 +318,32 @@ public static string HelloWorld()
* 19
* Goose
*/
public static string DuckGoose(int num)
{
string result = string.Empty;

for (int i = 1; i <= num; i++)
{
if (i % 3 == 0)
{
result += "Duck";
}
if (i % 5 == 0)
{
result += "Goose";
}
if (i % 3 != 0 && i % 5 != 0)
{
result += $"{i}";
}
if (i != num)
{
result += Environment.NewLine;
}
}

return result;
}

// If you've finished all these challenges, sign up for CodeWars.com and try to complete a few C# challenges there!
}
Expand Down
Loading