From 656e5eafcee631f5bf8604144f4332fc9946c390 Mon Sep 17 00:00:00 2001 From: Happy Date: Thu, 13 Aug 2026 19:11:21 +0200 Subject: [PATCH 1/2] Created a program (PasswordRetryWhile), that prompts the user to enter a password. It keeps asking until the correct password is entered, using a While Loop. --- C#_Exercise_Suite.txt | 39 ++++++++++++++++++ CSharpPractice.sln | 40 +++++++++++++++++++ DoWhileLoopRollDice/Program.cs | 25 +++++++++++- PasswordRetryWhile/PasswordRetryWhile.csproj | 10 +++++ PasswordRetryWhile/Program.cs | 28 +++++++++++++ StarsTriangleNestedFor/Program.cs | 26 ++++++++++++ .../StarsTriangleNestedFor.csproj | 10 +++++ 7 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 C#_Exercise_Suite.txt create mode 100644 PasswordRetryWhile/PasswordRetryWhile.csproj create mode 100644 PasswordRetryWhile/Program.cs create mode 100644 StarsTriangleNestedFor/Program.cs create mode 100644 StarsTriangleNestedFor/StarsTriangleNestedFor.csproj diff --git a/C#_Exercise_Suite.txt b/C#_Exercise_Suite.txt new file mode 100644 index 0000000..f67902f --- /dev/null +++ b/C#_Exercise_Suite.txt @@ -0,0 +1,39 @@ +1.1.1. Write a console application (BasicVariables) that declares and initializes variables of appropriate data types with specified values. + +● Declare variables using explicit types (int, double, bool, string, decimal) first. +● Refactor them to use the implicit typing keyword var. +● Challenge: Why does the C# compiler require an initialization value when using var? Write your answer as a multi-line comment at the top of your program. + +● Required variables to declare: +o myAge initialized with your age. +o myHeight initialized with your height (e.g., 1.72). +o isPermanentResident initialized with your residency status (true/false). +o myNickname initialized with your nickname. +o myEstimatedNetWorth initialized with your estimated net worth. +o myLinkedIn initialized with your LinkedIn profile link. + +1.1.2. In the BasicVariables program Display the values of these variables on the console with descriptive labels using Console.WriteLine() method. + +1.1.3. In the BasicVariables program experiment with different formatting options +● Modify the height display using composite formatting ({0}, variable) format eg. ("Height is {0}", myHeight). +● Modify the name display utilizing explicit string concatenation (string + string)) eg. ("Nickname: " + myNickname). +● Modify the all others display line to string interpolation ($” {variable}”) format + +1.2. Interactive User Input [Ref: MSDN - Console Input] + +1.2.1. Modify the BasicVariables console application to prompt the user to enter all variables using Console.ReadLine(). +● Ensure you parse the incoming string data to the appropriate type safely (e.g., using int.Parse(), double.Parse(), decimal.Parse(), or bool.Parse()). + +1.3. Data Types and Casting [Ref: MSDN - Casting and Type Conversions] +1.3.1. Write a new program called (TypeAndCastCalculator) that performs arithmetic operations (addition +, division /) on two numeric (number1, number2) variables of type integer which entered by prompting user. + +● Store the result of addition in a sum variable and the result of division in a quotient variable. +● Convert inputs using decimal.Parse() or convert integer elements explicitly. +● Ensure you handle integer division safely by applying an explicit inline type-cast ((double) or (decimal)) to prevent fractional truncation before processing. + +1.4. ConstantVariables [Ref: MSDN - Constants] + + + + + diff --git a/CSharpPractice.sln b/CSharpPractice.sln index 776fce7..050b44b 100644 --- a/CSharpPractice.sln +++ b/CSharpPractice.sln @@ -97,6 +97,21 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WhileLoopPassword", "WhileL EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdditionalWhileLoop", "AdditionalWhileLoop\AdditionalWhileLoop.csproj", "{2AED649B-AFFD-47B8-8B1B-D1A0F37E9508}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DoWhileLoopGuessNumber", "DoWhileLoopGuessNumber\DoWhileLoopGuessNumber.csproj", "{D5FE483D-8B47-4E9E-98C3-2726C61681DB}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DoWhileLoopRollDice", "DoWhileLoopRollDice\DoWhileLoopRollDice.csproj", "{FF9DF193-6302-4B0B-B835-AF439942A61F}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DoWhileLoopAverageCalculator", "DoWhileLoopAverageCalculator\DoWhileLoopAverageCalculator.csproj", "{F4909178-5FA6-48B1-ACBA-9F4F2EBDCF23}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MultiplicationNestedForLoop", "MultiplicationNestedForLoop\MultiplicationNestedForLoop.csproj", "{702ADB90-089C-49CF-8920-5543C6D50FC5}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StarsTriangleNestedFor", "StarsTriangleNestedFor\StarsTriangleNestedFor.csproj", "{3BF25747-649B-49B0-AA1F-39601F662F45}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{8EC462FD-D22E-90A8-E5CE-7E832BA40C5D}" + ProjectSection(SolutionItems) = preProject + C#_Exercise_Suite.txt = C#_Exercise_Suite.txt + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -271,6 +286,26 @@ Global {2AED649B-AFFD-47B8-8B1B-D1A0F37E9508}.Debug|Any CPU.Build.0 = Debug|Any CPU {2AED649B-AFFD-47B8-8B1B-D1A0F37E9508}.Release|Any CPU.ActiveCfg = Release|Any CPU {2AED649B-AFFD-47B8-8B1B-D1A0F37E9508}.Release|Any CPU.Build.0 = Release|Any CPU + {D5FE483D-8B47-4E9E-98C3-2726C61681DB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D5FE483D-8B47-4E9E-98C3-2726C61681DB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D5FE483D-8B47-4E9E-98C3-2726C61681DB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D5FE483D-8B47-4E9E-98C3-2726C61681DB}.Release|Any CPU.Build.0 = Release|Any CPU + {FF9DF193-6302-4B0B-B835-AF439942A61F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FF9DF193-6302-4B0B-B835-AF439942A61F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FF9DF193-6302-4B0B-B835-AF439942A61F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FF9DF193-6302-4B0B-B835-AF439942A61F}.Release|Any CPU.Build.0 = Release|Any CPU + {F4909178-5FA6-48B1-ACBA-9F4F2EBDCF23}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F4909178-5FA6-48B1-ACBA-9F4F2EBDCF23}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F4909178-5FA6-48B1-ACBA-9F4F2EBDCF23}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F4909178-5FA6-48B1-ACBA-9F4F2EBDCF23}.Release|Any CPU.Build.0 = Release|Any CPU + {702ADB90-089C-49CF-8920-5543C6D50FC5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {702ADB90-089C-49CF-8920-5543C6D50FC5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {702ADB90-089C-49CF-8920-5543C6D50FC5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {702ADB90-089C-49CF-8920-5543C6D50FC5}.Release|Any CPU.Build.0 = Release|Any CPU + {3BF25747-649B-49B0-AA1F-39601F662F45}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3BF25747-649B-49B0-AA1F-39601F662F45}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3BF25747-649B-49B0-AA1F-39601F662F45}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3BF25747-649B-49B0-AA1F-39601F662F45}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -318,6 +353,11 @@ Global {9907FB36-1145-4272-843C-01F030EF855C} = {42676F42-8F68-4892-AAC5-C899BF2E2F8B} {240BC544-26C0-457D-A3FC-6AA46A8D2D19} = {42676F42-8F68-4892-AAC5-C899BF2E2F8B} {2AED649B-AFFD-47B8-8B1B-D1A0F37E9508} = {42676F42-8F68-4892-AAC5-C899BF2E2F8B} + {D5FE483D-8B47-4E9E-98C3-2726C61681DB} = {42676F42-8F68-4892-AAC5-C899BF2E2F8B} + {FF9DF193-6302-4B0B-B835-AF439942A61F} = {42676F42-8F68-4892-AAC5-C899BF2E2F8B} + {F4909178-5FA6-48B1-ACBA-9F4F2EBDCF23} = {42676F42-8F68-4892-AAC5-C899BF2E2F8B} + {702ADB90-089C-49CF-8920-5543C6D50FC5} = {42676F42-8F68-4892-AAC5-C899BF2E2F8B} + {3BF25747-649B-49B0-AA1F-39601F662F45} = {42676F42-8F68-4892-AAC5-C899BF2E2F8B} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {BD240BFE-F612-4D31-B438-DD8F1913314F} diff --git a/DoWhileLoopRollDice/Program.cs b/DoWhileLoopRollDice/Program.cs index b9a18d6..d9015ca 100644 --- a/DoWhileLoopRollDice/Program.cs +++ b/DoWhileLoopRollDice/Program.cs @@ -4,7 +4,30 @@ internal class Program { static void Main(string[] args) { - Console.WriteLine("Hello, World!"); + Console.WriteLine("=== Dice Roll Simulator ==="); + Console.WriteLine("Rolling the die until we hit a 6...\n"); + + Random randomDice = new Random(); + int rollCount = 0; + int currentRoll; + + + do + { + + currentRoll = randomDice.Next(1, 7); + + rollCount++; + + + Console.WriteLine($"Roll #{rollCount}: You rolled a [{currentRoll}]"); + + } while (currentRoll != 6); + + + Console.WriteLine("\n============================================="); + Console.WriteLine($"🎯 Success! It took a total of {rollCount} rolls to get a 6."); + } } } diff --git a/PasswordRetryWhile/PasswordRetryWhile.csproj b/PasswordRetryWhile/PasswordRetryWhile.csproj new file mode 100644 index 0000000..fd4bd08 --- /dev/null +++ b/PasswordRetryWhile/PasswordRetryWhile.csproj @@ -0,0 +1,10 @@ + + + + Exe + net9.0 + enable + enable + + + diff --git a/PasswordRetryWhile/Program.cs b/PasswordRetryWhile/Program.cs new file mode 100644 index 0000000..f4ccc4b --- /dev/null +++ b/PasswordRetryWhile/Program.cs @@ -0,0 +1,28 @@ +namespace PasswordRetryWhile +{ + internal class Program + { + static void Main(string[] args) + { + string setPassword = "SecretPassword123"; + + string enteredPassword = string.Empty; + + Console.WriteLine("=== Password Authentication Gateway ==="); + + while (enteredPassword != setPassword) + { + Console.Write("Enter the password: "); + enteredPassword = Console.ReadLine(); + + if (enteredPassword != setPassword) + { + Console.WriteLine("Incorrect password. Please try again.\n"); + } + } + + + Console.WriteLine("Access Granted!"); + } + } + } diff --git a/StarsTriangleNestedFor/Program.cs b/StarsTriangleNestedFor/Program.cs new file mode 100644 index 0000000..5e1b325 --- /dev/null +++ b/StarsTriangleNestedFor/Program.cs @@ -0,0 +1,26 @@ +namespace StarsTriangleNestedFor +{ + internal class Program + { + static void Main(string[] args) + { + Console.WriteLine("=== Nested Loop Star Triangle ===\n"); + + + int totalRows = 5; + + + for (int row = 1; row <= totalRows; row++) + { + + for (int col = 1; col <= row; col++) + { + + Console.Write("*"); + } + + Console.WriteLine(); + } + } + } +} diff --git a/StarsTriangleNestedFor/StarsTriangleNestedFor.csproj b/StarsTriangleNestedFor/StarsTriangleNestedFor.csproj new file mode 100644 index 0000000..2150e37 --- /dev/null +++ b/StarsTriangleNestedFor/StarsTriangleNestedFor.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + From 1ecc680a5da49663e16fc7588ee7c7817f194b13 Mon Sep 17 00:00:00 2001 From: Happy Date: Thu, 13 Aug 2026 20:41:18 +0200 Subject: [PATCH 2/2] Re-created the program (FactorialCalculator),that calculates the factorial of a number entered by the user using a For loop --- FactorialCalculator/FactorialCalculator.csproj | 10 ++++++++++ FactorialCalculator/Program.cs | 10 ++++++++++ 2 files changed, 20 insertions(+) create mode 100644 FactorialCalculator/FactorialCalculator.csproj create mode 100644 FactorialCalculator/Program.cs diff --git a/FactorialCalculator/FactorialCalculator.csproj b/FactorialCalculator/FactorialCalculator.csproj new file mode 100644 index 0000000..fd4bd08 --- /dev/null +++ b/FactorialCalculator/FactorialCalculator.csproj @@ -0,0 +1,10 @@ + + + + Exe + net9.0 + enable + enable + + + diff --git a/FactorialCalculator/Program.cs b/FactorialCalculator/Program.cs new file mode 100644 index 0000000..0dd78e6 --- /dev/null +++ b/FactorialCalculator/Program.cs @@ -0,0 +1,10 @@ +namespace FactorialCalculator +{ + internal class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello, World!"); + } + } +}