My Fizz Buzz Solution
My Fizz Buzz Solution
Solution 1: Using Ternary Operator
for (int i = 1; i < 101; i++)
{
string result = "";
result = i % 3 == 0 ? "Fizz" : ""; // check if i is divisible by 3
result += i % 5 == 0 ? "Buzz" : "";// check if i is divisible by 5
Console.Write("\n" + i.ToString() + " - " + result);
}
{
string result = "";
result = i % 3 == 0 ? "Fizz" : ""; // check if i is divisible by 3
result += i % 5 == 0 ? "Buzz" : "";// check if i is divisible by 5
Console.Write("\n" + i.ToString() + " - " + result);
}
Solution 2: Using IF Operator
for (int i = 1; i < 101; i++)
{
string result = "";
if (i % 3 == 0) // check if i is divisible by 3
{
//give result the value fizz
result = "Fizz";
}
if (i % 5 == 0) // check if i is divisible by 5
{
//gets the value of result and add to the buzz value giving an output of fizz or fizzbuzz
result += "Buzz";
}
//show result;
Console.Write(" \n" + i.ToString() + " - " + result);
}
{
string result = "";
if (i % 3 == 0) // check if i is divisible by 3
{
//give result the value fizz
result = "Fizz";
}
if (i % 5 == 0) // check if i is divisible by 5
{
//gets the value of result and add to the buzz value giving an output of fizz or fizzbuzz
result += "Buzz";
}
//show result;
Console.Write(" \n" + i.ToString() + " - " + result);
}
Fabonacci Function c#
|
|
Prevent a Class to be inherited C#
The sealed can be used to prevent a class to be inherited.
sample:
sealed class FileClass
{
sealed FileClass(string filepath)
{
Filepath = filepath;
}
private string _filepath;
public string Filepath
{
get { return _filepath;}
set { _filepath = Filepath; }
}
}
Display All Server Files in Server using C# and ASP.NET
DirectoryInfo di = newDirectoryInfo(Server.MapPath("/"));
FileInfo[] fi = di.GetFiles();
fi = di.GetFiles("*.*");
foreach (FileInfo fi1 in di.GetFiles())
{
Response.Write( fi1.FullName.ToString());
Response.Write("<br />");
}
Subscribe to:
Posts (Atom)

