Introduction :
Prime number is a number that is greater than 1 and divided by 1 or itself. In other words, prime numbers can’t be divided by other numbers than itself or 1. For example 2, 3, 5, 7, 11, 13, 17…. are the prime numbers.
Here is source code of the C# Program to Check a Number is Prime Number or not, if so then Display its Largest Factor. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.
C# Program to Check a Number is Prime Number or not
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace prime { class Program { static void Main(string[] args) { int i, n; Console.WriteLine("Enter the Number:"); n = Convert.ToInt32(Console.ReadLine()); for (i = 2; i <=2; i++) { if (n % i == 0) break; } if (i > 2) { Console.WriteLine("prime number:", n); } else { Console.WriteLine("Not Prime Number:", n); } } } }
OutPut of Program :
Enter the Number : 17 Number is Prime.
Enter the Number : 22 Not Prime Number
Learn Recommended posts :
C# Program to find sum of elements in a given array
C# program to find maximum and minimum element in array
How do you reverse a string in C#
C# program to Check String is palindrom or not
How to print Pyramid Pattern in C#
How do you count the no of digits or character in C#
Factorial Number Program in C# using Recursion
Convert string to char array in C#
Finding the biggest of three numbers in C#
C# Program to Check Whether the Entered Number is an Armstrong Number or Not
2 Comments