Hello! 👋 Welcome to this simple yet useful C# console application that helps you explore the fascinating world of numbers. It has two main features: checking for prime numbers and calculating factorials!
This little program offers the following functionalities:
-
Prime Number Checker 🧐: You can enter a positive whole number (like 20), and the app will then check all the numbers from 2 up to your entered number to see which ones are prime. A prime number is a special number greater than 1 that can only be perfectly divided by 1 and itself (e.g., 2, 3, 5, 7, 11).
-
Factorial Calculator 🤯: You can input a non-negative whole number (like 5), and the app will calculate its factorial. The factorial of a number is the product of all positive whole numbers up to that number. For example, the factorial of 5 (written as 5!) is 5 * 4 * 3 * 2 * 1 = 120. The app has a limit (up to 20) to avoid very large numbers that might not be stored accurately.
To run this application, you'll need to have .NET 6 or a later version installed on your computer (which you likely do if you're working with C#!). Here's how to get it running:
-
Save the Code: Save the provided C# code as a file named
Program.csinside a folder (you can name it something likeNumberCheckerApp). -
Open Your Terminal or Command Prompt: Open your terminal (on Linux/macOS) or command prompt (on Windows) and navigate to the
NumberCheckerAppfolder where you saved theProgram.csfile. You can use thecdcommand (change directory). For example:cd NumberCheckerApp -
Run the Magic Command: Type the following command and press Enter:
dotnet run
This command tells .NET to build and run your C# code.
-
Follow the Prompts! 📝: Once the application starts, it will first ask you to enter a positive integer to check for prime numbers. Type a number and press Enter. It will then list all the prime and non-prime numbers up to the number you entered.
Next, it will ask you to enter a non-negative integer to calculate its factorial. Type a number (between 0 and 20 for best results) and press Enter. The app will then display the calculated factorial.
Finally, it will tell you to press any key to exit the application.
Let's break down the main parts of the code:
namespace ConsoleApp: This is like a container that helps organize your code.public class Program: This is the main blueprint for your application.public static void Main(string[] args): This is the heart of the program where the execution begins. It handles the interactions with you, asking for input and displaying the results.- Prime Number Checking (
IsPrimemethod): This part of the code has a function calledIsPrimethat takes a number and determines if it's prime or not. It does this by checking if the number can be divided evenly by any number other than 1 and itself. It's quite efficient as it only checks up to the square root of the number. - Factorial Calculation (
CalculateFactorialmethod): This section has a function calledCalculateFactorialthat calculates the factorial of a given non-negative integer. It multiplies all the whole numbers from 1 up to the given number. It also includes checks to prevent issues with negative numbers or very large numbers. - Input Handling (
Console.ReadLine(),int.TryParse()): The code usesConsole.ReadLine()to get input from you andint.TryParse()to safely convert your input into numbers. This helps prevent errors if you type something that isn't a valid number. - Output (
Console.WriteLine()): The program usesConsole.WriteLine()to display messages and results on your console screen. - Comments (
/// <summary> ... </summary>,// ...): The code includes comments that explain what different parts of the code are doing. This makes it easier to understand!
This simple app is a great way to learn about prime numbers and factorials. Feel free to run it with different inputs and see the results. Enjoy playing with numbers! 😊