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. What would be the proper way to comment within this code?

What would be the proper way to comment within this code?

Scheduled Pinned Locked Moved C#
question
4 Posts 4 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

    using System; public class AvgPrime { public static void Main() { double c=0,y=0,p=0; int i,j; bool isPrime; for(i=2; i <= 100; i++) { isPrime = true; for(j=2; j <= i/2; j++) { if(i % j == 0) { isPrime = false; } } if(isPrime) { y = y + 1; c = c + i; } } p = c/y; Console.WriteLine("The avg of all prime numbers from 1 to 100 is {0}",p); } }

    M L E 3 Replies Last reply
    0
    • C cmh623

      using System; public class AvgPrime { public static void Main() { double c=0,y=0,p=0; int i,j; bool isPrime; for(i=2; i <= 100; i++) { isPrime = true; for(j=2; j <= i/2; j++) { if(i % j == 0) { isPrime = false; } } if(isPrime) { y = y + 1; c = c + i; } } p = c/y; Console.WriteLine("The avg of all prime numbers from 1 to 100 is {0}",p); } }

      M Offline
      M Offline
      mav northwind
      wrote on last edited by
      #2

      Oh come on, if this isn't homework, what else is? Some people never learn... :(

      Regards, mav -- Black holes are the places where God divided by 0...

      1 Reply Last reply
      0
      • C cmh623

        using System; public class AvgPrime { public static void Main() { double c=0,y=0,p=0; int i,j; bool isPrime; for(i=2; i <= 100; i++) { isPrime = true; for(j=2; j <= i/2; j++) { if(i % j == 0) { isPrime = false; } } if(isPrime) { y = y + 1; c = c + i; } } p = c/y; Console.WriteLine("The avg of all prime numbers from 1 to 100 is {0}",p); } }

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        How about adding some explanatory text lines that start with a double slash? Things I would like to know: - why are c and y of type double? - why is i incremented by one? - why is j limited to i/2? - why does the for(j) loop continue when a divisor has been found? - why are most identifiers single-character? :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        this months tips: - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use PRE tags to preserve formatting when showing multi-line code snippets


        1 Reply Last reply
        0
        • C cmh623

          using System; public class AvgPrime { public static void Main() { double c=0,y=0,p=0; int i,j; bool isPrime; for(i=2; i <= 100; i++) { isPrime = true; for(j=2; j <= i/2; j++) { if(i % j == 0) { isPrime = false; } } if(isPrime) { y = y + 1; c = c + i; } } p = c/y; Console.WriteLine("The avg of all prime numbers from 1 to 100 is {0}",p); } }

          E Offline
          E Offline
          echuck66
          wrote on last edited by
          #4

          using // or /* */...

          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