Monday, July 28, 2014

C# Program to Display All the Prime Numbers Between 1 to 10

This Program checks for Primenumbers . I'm display first 10 prime numbers, Prime numbers are those numbers which are divisible by 1 and itself . We see how prime numbers are computed in the .NET Framework.
Note :
  • I will show you in a simple way, the first 10 prime numbers .

Algorithm :

1. Declare an integer variable, count, and assign the value 0 to it .
2. Declare an integer variable, num, and assign the value 2 to it .
3. Repeat until count becomes equal to 10:
  • If num is a prime number :
  1. Display num .
  2. Set count=count+1 .
  • Set num=num +1 .


Here is source code of the C# Program to Display All the Prime Numbers Between 1 to 10. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below :

Source Code :

using System; 

class prime_number
{
   public static void Main()
    {
       int count=0;
       int num=2;
       Console.WriteLine("---- First 10 Prime Number ----");       

      while(count!=10)
      {
         if(num%2!=0)
           {
             Console.WriteLine("Prime : " + num);
             count++;
           }
             num++;
      }
    }
}




OUTPUT : 


---- First 10 Prime Number  ----
Prime: 3
Prime: 5
Prime: 7
Prime: 9
Prime: 11
Prime: 13
Prime: 15
Prime: 17
Prime: 21
Prime: 19



Here is source code of the C# Program to Check the number is prime or not . The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below :

using System; 

class prime_number
{
   public static void Main()
    {
       Console.WriteLine("---- Check the number is prime or not prime ----");       
        
       Console.Write("\n Enter a number : ");

       int num = Convert.ToInt32(Console.ReadLine());

         if(num%2!=0)
           {
             Console.WriteLine(num + " Is prime");
             
           }

         else
           {
             Console.WriteLine(num + " Not prime");
           }
             
      
   }
}




OUTPUT : 


---- Check the number is prime or not prime ----       

Enter a number : 5

5 Is prime .


Summary :

Here we looked at how the .NET Framework determines whether a number is a prime number. We described the basic algorithmic, which provides optimized logic for testing number factors .

4 comments:

  1. I wonder how 9 and 15 is coming out as prime, as prime numbers must be divisible by 1 and itself.

    ReplyDelete
  2. You know . what is prime numbers . Above odd numbers are written.

    ReplyDelete
  3. CasinoDaddy: What is online gaming for a - DrmCad
    CasinoDaddy is an 의왕 출장마사지 online gambling 울산광역 출장마사지 website that's been around since 2018. It's 시흥 출장샵 been operating since 2016, 전주 출장안마 and is now live on 과천 출장샵 iOS and Android.

    ReplyDelete