TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Anthony Gordon
NA
9
0
find the prime number average between 1 - 100
Apr 12 2009 1:07 PM
Hey guys. I have an application where i have to find the prime number average between 1 and 100. it basically loops through every number between 1 and 100 and if the number is prime, it will add sum up the number and count each one it finds. I think I got it working correctly. Im not sure because I dont feel like manually going through every number between 1 and 100 and manually calculating it myself. So i am asking someone here. does anyone know a better way of testing this application out or atleast know if I am doing it right so I can continue. my code is below...
using System;
public class Lab5
{
public static void Main()
{
//Declare all required variables
double dblAverage; //the average should allow for decimal values
int intPrimeCount = 0; //this represents the total count of prime numbers found. Short would have also worked.
int intPrimeTotal = 0; //this represents the sum of all prime numbers found. Short would have also worked.
bool blnPrime;
int intDivisor; //use this variable in the Mod operation
short intNumberToBeTested; //this is the number between 2 and 100 which is being tested to find out if it is prime or not
// we all know that 1 is not prime
string strAverage;
for (intNumberToBeTested = 2; intNumberToBeTested <= 100; intNumberToBeTested++)
{
blnPrime = true; //assume the number is Prime and then try to prove otherwise
//to find if a number is prime what the following for next loop does is use the modulus operator
//and see if the result of this operation between the number being tested and all numbers less than it
//iz zero. If it is then the test failed as it means that the number being tested was divisible
//by a number other than 1 or itself. At this point we declare the number being tested not prime and exit the loop.
//Just a word of caution here. Using the Exit For statement may be efficient in that it stops the loop once a number
//is found not to be prime as there is no need to test it anymore. However this kind of affects the modularity of
//the for next loop and could just as well be left out.
for (intDivisor = 2; intDivisor < intNumberToBeTested; intDivisor++)
if (intNumberToBeTested % intDivisor == 0)
blnPrime = false;
//exit;
if (blnPrime == true) //If the number was found to be prime than update our variables
{
intPrimeCount = intPrimeCount + 1;
intPrimeTotal = intPrimeTotal + intNumberToBeTested;
}
}
dblAverage = intPrimeTotal / intPrimeCount; //find the average
strAverage = Convert.ToString(dblAverage); //converts the number to a string and displays it in the provided label
//display the average correct to two decimal places
Console.WriteLine(dblAverage);
Console.ReadLine();
}
}
Reply
Answers (
1
)
Hoew to made a singleton Idea with multi forms
Y we use web parts