// Fig. 7.25 Prime Numbers Write an app that displays all the prime numbers less
ID: 3600025 • Letter: #
Question
// Fig. 7.25 Prime Numbers Write an app that displays all the prime numbers less than 10,000
Why won't my code show numbers less than 7309?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PrimeNumbers
{
class Program
{
static void Main(string[] args)
{
// variable to count the prime numbers
int count = 0;
Console.WriteLine("Prime numbers between 2 and 10,000 are: ");
// loop to check between 2 and 10,000
for (int i = 2; i <= 10000; i++)
{
// check if the number found is prime
if (IsPrime(i))
{
// display number
Console.WriteLine(i);
count++; // increment count by 1
}
}
Console.WriteLine($"Prime numbers are {count}");
}
// method to find prime numbers
static bool IsPrime(int number)
{
bool yes = true;
int prime = (int)Math.Sqrt((double)number); // test using square root of number
// run loop to check if number has any factor
for (int i = 2; i <= prime; i++)
{
// check if a factor is found
if (number % i == 0)
{
yes = false;
return yes;
}
}
return yes;
}
}
}
Explanation / Answer
Hi
I dont see any issue with the code. it is working fine. May be issue with your console.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PrimeNumbers
{
class Program
{
static void Main(string[] args)
{
// variable to count the prime numbers
int count = 0;
Console.WriteLine("Prime numbers between 2 and 10,000 are: ");
// loop to check between 2 and 10,000
for (int i = 2; i <= 10000; i++)
{
// check if the number found is prime
if (IsPrime(i))
{
// display number
Console.WriteLine(i);
count++; // increment count by 1
}//7309
}
Console.WriteLine($"Prime numbers are {count}");
}
// method to find prime numbers
static bool IsPrime(int number)
{
bool yes = true;
int prime = (int)Math.Sqrt((double)number); // test using square root of number
// run loop to check if number has any factor
for (int i = 2; i <= number/2; i++)
{
// check if a factor is found
if (number % i == 0)
{
yes = false;
return yes;
}
}
return yes;
}
}
}
Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.