Tuesday, May 22, 2012

Program To Display Prime numbers less than 100

This program is very useful for the begginers of C# to learn for Loop. Let us Answer the basic Question What is Prime Numbers Prime numbers are the numbers that are divisible only by itself and by one.
So here are the steps 

  1. First of all i will take a for loop that will run for all values between 1 to 100 these values are stored in the variable i
  2. Then i am taking a for loop that will take vaues between 2 and i. i will store this in variable k
  3. I this step i will take mode of i and k and store it in a variable rem 
  4. Count1 which will count the number of times the modulus is zero
  5. Then i am taking count2 this calculates the number of times mod is not zero
  6. If the number is prime then its mod to k will never be 0 so  i take a if loop and check the condition whether i has mod 0 . if it has never had a mod zero then it is a prime number
  7. Hence display the number  
 Please do comment in case of any doubts or any alternate programs


The Below Program Is Runned Using Visual Studio
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace prime_number_series
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 100; i++)
            {
                int count1 = 0;
                int count2 = 0;
                for (int k = 2; k < i - 1; k++)
                {
                    int rem = i%k;
                    if (rem == 0)
                    {
                        count1++;
                    }
                    else
                    {
                        count2++;
                    }
                }
                if (count1 == 0)
                {
                    Console.Write("{0} \t",i);
                }
               
            }

            Console.ReadLine();
        }
    }
   
}
Output


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace prime_number_series
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter a Number to Be Checked Whether It Is Prime Or Not");
            int i = Convert.ToInt32(Console.ReadLine());
           
            {
                int count1 = 0;
                int count2 = 0;
                for (int k = 2; k < i - 1; k++)
                {
                    int rem = i%k;
                    if (rem == 0)
                    {
                        count1++;
                    }
                    else
                    {
                        count2++;
                    }
                }
                if (count1 == 0)
                {
                    Console.Write("The Value You Have Entered Is A Prime Number");
                }
                else
                {
                    Console.WriteLine("The Value You Have Entered is Not a Prime number");
                }
                Console.ReadLine();
               
            }
          
        }
    }
   
}


  

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. In the top prime number between 0 and 100 section, it is showing 1 as prime number based on the code. I think 1 and 0 are not prime number. Therefore, if i is initialized as 2 (eg. int i = 2) then I believe it should be okay.

    ReplyDelete