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. Need help making a basic program in C# [modified]

Need help making a basic program in C# [modified]

Scheduled Pinned Locked Moved C#
csharphelplounge
3 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.
  • C Offline
    C Offline
    cmh623
    wrote on last edited by
    #1

    I am trying to make a program in C# that will find the average of all prime numbers 1 to 100 and print the output to the screen. I am very lost in this, as I am new to the whole programming world. I have been unable to come up with any code that works so any help with this would be greatly appreciated. Thanks! here is what I have so far: The general layout of the code will possibly look like: assign a var for isPrime as bool (which is used as a flag that will be changed if the program does a calc that has no remainder. ) assign a var that keeps track of number of primes it gets ex double totalPrimes assign a var that adds up the number when decides if prime of not ex double runningTotal assign number you will check assign number you will use to divide by then while (numberToCheck <=100) { -set isPrime to true again(always start with it as true) -set the divideBy var back to value 2 while (divideBy < numberToCheck) { -then you do if statement to check if the calc has remainder or not -and if it doesn't then change isPrime false (since no remainder means it is not prime) -then increment divideBy number and try the next number -if after all calculations are done and isPrime is still true, then the nubmer is prime -then provide an if statement to check if number is prime or not if so then add the number to the runningTotal and chalk up another for the total of primes, -if not prime then increment that number and run through the first loop -- modified at 21:08 Monday 21st May, 2007

    C M 2 Replies Last reply
    0
    • C cmh623

      I am trying to make a program in C# that will find the average of all prime numbers 1 to 100 and print the output to the screen. I am very lost in this, as I am new to the whole programming world. I have been unable to come up with any code that works so any help with this would be greatly appreciated. Thanks! here is what I have so far: The general layout of the code will possibly look like: assign a var for isPrime as bool (which is used as a flag that will be changed if the program does a calc that has no remainder. ) assign a var that keeps track of number of primes it gets ex double totalPrimes assign a var that adds up the number when decides if prime of not ex double runningTotal assign number you will check assign number you will use to divide by then while (numberToCheck <=100) { -set isPrime to true again(always start with it as true) -set the divideBy var back to value 2 while (divideBy < numberToCheck) { -then you do if statement to check if the calc has remainder or not -and if it doesn't then change isPrime false (since no remainder means it is not prime) -then increment divideBy number and try the next number -if after all calculations are done and isPrime is still true, then the nubmer is prime -then provide an if statement to check if number is prime or not if so then add the number to the runningTotal and chalk up another for the total of primes, -if not prime then increment that number and run through the first loop -- modified at 21:08 Monday 21st May, 2007

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      OK, sounds like a first homework assignment to me. How do you define a prime number ? How does that definition help you to work out if a number is a prime number ? Once you know it's a prime, what do you need to do with it, if you want to calculate an average ?

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      1 Reply Last reply
      0
      • C cmh623

        I am trying to make a program in C# that will find the average of all prime numbers 1 to 100 and print the output to the screen. I am very lost in this, as I am new to the whole programming world. I have been unable to come up with any code that works so any help with this would be greatly appreciated. Thanks! here is what I have so far: The general layout of the code will possibly look like: assign a var for isPrime as bool (which is used as a flag that will be changed if the program does a calc that has no remainder. ) assign a var that keeps track of number of primes it gets ex double totalPrimes assign a var that adds up the number when decides if prime of not ex double runningTotal assign number you will check assign number you will use to divide by then while (numberToCheck <=100) { -set isPrime to true again(always start with it as true) -set the divideBy var back to value 2 while (divideBy < numberToCheck) { -then you do if statement to check if the calc has remainder or not -and if it doesn't then change isPrime false (since no remainder means it is not prime) -then increment divideBy number and try the next number -if after all calculations are done and isPrime is still true, then the nubmer is prime -then provide an if statement to check if number is prime or not if so then add the number to the runningTotal and chalk up another for the total of primes, -if not prime then increment that number and run through the first loop -- modified at 21:08 Monday 21st May, 2007

        M Offline
        M Offline
        Muammar
        wrote on last edited by
        #3

        Sorry but "Lurker" usually means "Home work poster" here:~


        Smile: A curve that can set a lot of things straight! (\ /) (O.o) (><)

        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