C# Code to find the average of prime numbers between 1-100
-
:confused: Please is there any one out there that can help me I am having a very tough time trying to figure this crap out. I know I have to create a loop to find the prime numbers, but i can't figure it out. I have never done any programming before. This is my 4th week in my C# programming class and its going to fast for me. This website is supposed to have free code and tutorials. does anyone know the code for this? Please help I am lost! Ryan
-
:confused: Please is there any one out there that can help me I am having a very tough time trying to figure this crap out. I know I have to create a loop to find the prime numbers, but i can't figure it out. I have never done any programming before. This is my 4th week in my C# programming class and its going to fast for me. This website is supposed to have free code and tutorials. does anyone know the code for this? Please help I am lost! Ryan
okay I found a code that displays the prime numbers, but i need to find the average of the dispalyed prime numbers. Here is the code does anyone what to let me know how to find the average please. using System; namespace PrimeNumbers { /// /// Summary description for Class1. /// class PrimeNumbers { /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { // Ask the user for the quantity to check for Prime Numbers. Console.Write("Please enter the quantity to test for Prime Number: "); // Assign topNumber with User's Input. ulong topNumber = ulong.Parse(Console.ReadLine()); // Create an boolean array, numbers bool[] numbers = new bool[topNumber]; // Assign all the values in numbers with 'true'. for (ulong i = 0; i < topNumber; i++) { numbers[i] = true; } // Assign all those mutiples of 2 and 3 to be 'false'. // So that it facilitate you in finding the Prime Numbers. for (ulong i = 2; i < topNumber; i++) { if(numbers[i]) { for(ulong j = i * 2; j < topNumber; j += i) numbers[j] = false; } } // The counter to count how many Prime Numbers. ulong primes = 0; // For the remaining numbers which are still 'true' are Prime Numbers. for (ulong i = 1; i < topNumber; i++) { if(numbers[i]) { primes++; Console.Out.WriteLine(i); } } Console.Out.WriteLine(); Console.Out.WriteLine(primes + " out of " + topNumber + " prime numbers found."); Console.In.ReadLine(); } } } Ryan
-
okay I found a code that displays the prime numbers, but i need to find the average of the dispalyed prime numbers. Here is the code does anyone what to let me know how to find the average please. using System; namespace PrimeNumbers { /// /// Summary description for Class1. /// class PrimeNumbers { /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { // Ask the user for the quantity to check for Prime Numbers. Console.Write("Please enter the quantity to test for Prime Number: "); // Assign topNumber with User's Input. ulong topNumber = ulong.Parse(Console.ReadLine()); // Create an boolean array, numbers bool[] numbers = new bool[topNumber]; // Assign all the values in numbers with 'true'. for (ulong i = 0; i < topNumber; i++) { numbers[i] = true; } // Assign all those mutiples of 2 and 3 to be 'false'. // So that it facilitate you in finding the Prime Numbers. for (ulong i = 2; i < topNumber; i++) { if(numbers[i]) { for(ulong j = i * 2; j < topNumber; j += i) numbers[j] = false; } } // The counter to count how many Prime Numbers. ulong primes = 0; // For the remaining numbers which are still 'true' are Prime Numbers. for (ulong i = 1; i < topNumber; i++) { if(numbers[i]) { primes++; Console.Out.WriteLine(i); } } Console.Out.WriteLine(); Console.Out.WriteLine(primes + " out of " + topNumber + " prime numbers found."); Console.In.ReadLine(); } } } Ryan
Well, an average is the sum of the numbers divided by how many numbers there are. You already know how many primes there are (ulong primes), you just need another variable to total them inside the same loop (the last loop). Then, after the loop exists, compute the average. I'd rather not show you the exact code, because you're supposed to learn something. BTW, the comment "Assign all those mutiples of 2 and 3 to be 'false'" should either read "Assign multiples of 2 to be false for all numbers", or "assign multiples of 2, 3, ... up to topNumber to be false". And also, it's sort of pointless to assign multiples of i where i > topNumber / 2, since any multiples are outside of the range of the array. Most of programming is NOT coding, it's figuring out the algorithm. Good luck! Marc My website
-
okay I found a code that displays the prime numbers, but i need to find the average of the dispalyed prime numbers. Here is the code does anyone what to let me know how to find the average please. using System; namespace PrimeNumbers { /// /// Summary description for Class1. /// class PrimeNumbers { /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { // Ask the user for the quantity to check for Prime Numbers. Console.Write("Please enter the quantity to test for Prime Number: "); // Assign topNumber with User's Input. ulong topNumber = ulong.Parse(Console.ReadLine()); // Create an boolean array, numbers bool[] numbers = new bool[topNumber]; // Assign all the values in numbers with 'true'. for (ulong i = 0; i < topNumber; i++) { numbers[i] = true; } // Assign all those mutiples of 2 and 3 to be 'false'. // So that it facilitate you in finding the Prime Numbers. for (ulong i = 2; i < topNumber; i++) { if(numbers[i]) { for(ulong j = i * 2; j < topNumber; j += i) numbers[j] = false; } } // The counter to count how many Prime Numbers. ulong primes = 0; // For the remaining numbers which are still 'true' are Prime Numbers. for (ulong i = 1; i < topNumber; i++) { if(numbers[i]) { primes++; Console.Out.WriteLine(i); } } Console.Out.WriteLine(); Console.Out.WriteLine(primes + " out of " + topNumber + " prime numbers found."); Console.In.ReadLine(); } } } Ryan
Pffff well done. I make myself some work in the other thread to post you some code just to realize now that you already found most of what you were searching for. :mad: Please stick to ONE thread and dont hop around.