Simple program

Simple program #7 (Print next prime number)

Enter a number: 11
Next prime number: 13

The program is written in C# programming language and will accept a number as input. The logic of the program is to find the succeeding prime number.
 
Simple program #7 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 Simple7   //Print next prime number
    {
        static void Main(string[] args)
        {
            Console.Write("Enter a number: ");
            int n = Convert.ToInt32(Console.ReadLine());
            int i = 0;
            int j = 2;

            Console.Write("Next prime number: ");
            for (i = n + 1; i < 300000; i++)
            {
                for (j = 2; j < i; j++)
                {
                    if (i % j == 0)
                    {
                        break;
                    }
                }
                if (i == j || i == 1)
                {
                    Console.WriteLine(i);
                    break;
                }
            }

            Console.ReadKey();
        }
    }
}

Output:
The input number here is 11. The program will find the succeeding prime number. The output is shown below:

Enter a number: 11
Next prime number: 13
Press any key to continue . . .


Note: This program is design to find prime number only between 2 to 300000.

For any query, comment us below.

Skip to Main Table Simple program

Previous - Simple program #6

Next – Simple program #8

Click imagination hunt to read latest blogs.


Keep learning and sharing...

Comments

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