Simple program

Simple program #6 (Check whether a number is prime or not)

-1
Smallest prime number is 2
39
Not prime number

The program is written in C# programming language and will accept a number as input. The logic of the program is to check whether the number is prime or not.
 
Simple program #6 by www.imaginationhunt.com
Simple program

Note: If you are new to C# and Console Application. Try to code First C# Program

Note: Read articles on how to use Loops and Conditions.

Let’s find out a simple and easy way to code the program.

Practical Implementation:

using System;

namespace patternProblem.Simple_program
{
    class Simple6   //checking number prime or not
    {
        static void Main(string[] args)
        {
            Console.Write("Enter a number: ");
            int n = Convert.ToInt32(Console.ReadLine());
            int i = 0;
           
            if (n < 2)
            {
                Console.WriteLine("Smallest prime number is 2");
            }
            for (i = 2; i < n; i++)
            {
                if (n % i == 0)
                {
                    Console.WriteLine("Not Prime number");
                    break;
                }
            }
            if (n == i)
            {
                Console.WriteLine("Prime number");
            }

            Console.ReadKey();
        }
    }
}

Output:
The input number here is 3. The program will check with the number is prime or not. The output is shown below:

Enter a number: 39
Not Prime number
Press any key to continue . . .


For any query, comment us below.

Skip to Main Table Simple program

Previous - Simple program #5

Next – Simple program #7

Click imagination hunt to read latest blogs.


Keep learning and sharing...

Comments

  1. It’s too informative blog and I am getting conglomerations of info’s about Dot Net Training in Chennaicertification. Thanks for sharing; I would like to see your updates regularly so keep blogging.Learn more course details.... Hadoop Training in Chennai
    Android Training in Chennai

    ReplyDelete

Post a Comment

Popular posts from this blog

SSC CGL 2016 Tier-1 Marks / Result

DATA TYPES IN C#

Pattern printing program

IS STRING MUTABLE OR IMMUTABLE IN .NET?

Top Mutual Funds