C# Prime Number Program Tutorial with Examples

C# Prime Number Program Tutorial with Examples

Today, We want to share with you C# Prime Number Program Tutorial with Examples.
In this post we will show you Prime Number Program in C#, hear for C# Program to check if a number is prime or not we will give you demo and example for implement.
In this post, we will learn about Check a Number is Prime Number or not in C# with an example.

Prime Number Program in C#

In this post, we will learn about Prime Number Program in C# with an example.

Now in this post, I will explain Prime Number Program in C#.Net with appropriate example. A prime number is a number which is greater than 1 and divided by 1 or itself. In other words, prime numbers cannot be divided by other numbers than itself or 1.

READ :  AngularJS UI Grid Sorting Filtering Paging Grouping Example

For example 2, 3, 5, 7, 11, 13, 17, 19, 23,29,31…. are the prime numbers.

Now create Console Application in Visual Studio and write below lines of code in it.

using System;
using System.Collections;
using System.Collections.Generic;

namespace infinityknow
{
    class Program
    {
        static void Main(string[] args)
        {
            // Program to check whether the given number is prime or not

            int number, k, m = 0, flag = 0;
            Console.Write("Enter the Number to check Prime: ");
            number = int.Parse(Console.ReadLine());
            m = number / 2;
            for (k = 2; k <= m; k++)
            {
                if (number % k == 0)
                {
                    Console.Write("Number is not Prime.");
                    flag = 1;
                    break;
                }
            }
            if (flag == 0)
            Console.Write("Number is Prime.");  

            Console.ReadKey(); // To retain the screen
        }
    }    
}

Read :

READ :  Set textarea directive value in Angular

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about C# Prime Number Program Tutorial.
I would like to have feedback on my Infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Add a Comment

Your email address will not be published. Required fields are marked *