Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Number Checker App in C#! 🔢

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!

What This Code Does 🛠️

This little program offers the following functionalities:

  1. 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).

  2. 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.

How to Run This Code 🚀

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:

  1. Save the Code: Save the provided C# code as a file named Program.cs inside a folder (you can name it something like NumberCheckerApp).

  2. Open Your Terminal or Command Prompt: Open your terminal (on Linux/macOS) or command prompt (on Windows) and navigate to the NumberCheckerApp folder where you saved the Program.cs file. You can use the cd command (change directory). For example:

    cd NumberCheckerApp
  3. Run the Magic Command: Type the following command and press Enter:

    dotnet run

    This command tells .NET to build and run your C# code.

  4. 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.

A Peek Inside the Code 👀

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 (IsPrime method): This part of the code has a function called IsPrime that 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 (CalculateFactorial method): This section has a function called CalculateFactorial that 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 uses Console.ReadLine() to get input from you and int.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 uses Console.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!

Have Fun Exploring Numbers! 🎉

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! 😊

About

Number Checker with C#. With simple for loop and if conditions

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages