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