Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how do i print prime and not prime number from 300 to 400 by using for loop and if statement.
Posted

You take a paper, a pen and you write : 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397
:-)


more seriously here is a function to check if a number is a prime or not but beware you can only use it with relatively small numbers as it is not particularly optimized...



C#
public static bool IsPrime(this int number)
{
    return (Enumerable.Range(1,number).Where(x => number % x == 0).Count() == 2);
}
 
Share this answer
 
Hello, this is logic to find out prime numbers between 300 to 400.

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

            }

            Console.ReadLine();
        }
    }

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900