Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# Code to find the average of prime numbers between 1-100

C# Code to find the average of prime numbers between 1-100

Scheduled Pinned Locked Moved C#
csharphelpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    diabolusgorgon
    wrote on last edited by
    #1

    :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

    D 1 Reply Last reply
    0
    • D diabolusgorgon

      :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

      D Offline
      D Offline
      diabolusgorgon
      wrote on last edited by
      #2

      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

      M R 2 Replies Last reply
      0
      • D diabolusgorgon

        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

        M Offline
        M Offline
        Marc Clifton
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • D diabolusgorgon

          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

          R Offline
          R Offline
          Robert Rohde
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups