Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
how do i print prime and not prime number from 300 to 400 by using for loop and if statement.
Posted 12-Feb-13 17:48pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
Smile | :)
 

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...
 

 
public static bool IsPrime(this int number)
{
    return (Enumerable.Range(1,number).Where(x => number % x == 0).Count() == 2);
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hello, this is logic to find out prime numbers between 300 to 400.
 
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();
        }
    }
 
}
  Permalink  

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

  Print Answers RSS
0 Maciej Los 310
1 OriginalGriff 285
2 Afzaal Ahmad Zeeshan 200
3 Sergey Alexandrovich Kryukov 195
4 BillWoodruff 180
0 OriginalGriff 6,499
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,193
3 Manas Bhardwaj 4,657
4 Maciej Los 4,120


Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 12 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100