Sunday, July 12, 2009

Need major help with a C# code?

I need to code a C# program with visual express 2005 that will find the average of all prime numbers between 2-100 and print the result at the end. Im having trouble figuring out how its going to sort out the prime numbers. therefore, i cant even get this lab started. i asked my professor and he gave me a response that didnt really help. here it is.





1. Loop intI from 2 to 100. (By definition, 1 is NOT a prime number.)


2. For each intI, do the following:


...a. Set blnPrime = True


...b. Loop intJ from 1 to (intI-1).


...c. For each intJ, do the following:


......(1) Check to see if intI divided by intJ would give a remainder of zero. If so, it'll mean that the number is divisible by that number, so the number is NOT a prime. If number is not a prime, we break out of this loop.


...d. If number is a prime, accumulate it in the sum total of all prime numbers, and add 1 to the count of prime numbers.


3. Print out all results and the average.





this is due tonight, someone please help!!

Need major help with a C# code?
using System;


using System.Collections.Generic;


using System.Text;





namespace prime


{


class Program


{


static bool isPrime(int n)


{


for (int i = 2 ; i %26lt; n; i++)


{


if ((n % i) == 0)


return false;


}


return true;


}





static void Main(string[] args)


{


int avg = 0;


int cpt = 0;


for (int i = 2; i %26lt; 101; i++)


{


if (isPrime(i))


{


avg += i; cpt++;


}


}


Console.Write(avg / cpt);


}


}


}


No comments:

Post a Comment