Basic Programming C# Examples

C# Program to Find Next Prime number of Given number

The Program in C# Program to Find Next Prime number of Given number is given below:

using System;

namespace NextPrime
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Codeauri Family, enter the number here to find the next prime number: ");
            int number = int.Parse(Console.ReadLine());
            int nextPrime = FindNextPrime(number);
            Console.WriteLine("Okay, the next prime number is: " + nextPrime);
        }

        static int FindNextPrime(int number)
        {
            int nextNumber = number + 1;
            while (!IsPrime(nextNumber))
            {
                nextNumber++;
            }
            return nextNumber;
        }

        static bool IsPrime(int number)
        {
            if (number <= 1)
            {
                return false;
            }
            for (int i = 2; i <= Math.Sqrt(number); i++)
            {
                if (number % i == 0)
                {
                    return false;
                }
            }
            return true;
        }
    }
}

Output:

Hello Codeauri Family, enter the number here to find the next prime number:
7
Okay, the next prime number is: 11

Pro-Tips💡

Here are the step by step execution of above program:

  1. The first line using System; imports the System namespace, which provides access to the Console class.
  2. The Main method is the entry point of the program. It prompts the user to enter a number and reads the input using Console.ReadLine().
  3. The input is converted to an integer using int.Parse() and is passed as an argument to the FindNextPrime method.
  4. The FindNextPrime method takes an integer as input and returns the next prime number after that number. It starts with the number nextNumber as the input number plus one and repeatedly increases it by one until a prime number is found. The IsPrime method is called to check whether the current value of nextNumber is prime or not.
  5. The IsPrime method takes an integer as input and returns a boolean indicating whether the number is prime or not. It first checks if the number is less than or equal to 1, in which case it returns false. Then, it checks the number for divisibility by all integers from 2 to the square root of the number. If the number is divisible by any of them, it returns false. Otherwise, it returns true.
  6. Finally, the result from the FindNextPrime method is displayed on the console using Console.WriteLine.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

Leave a Reply

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

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X