Interviews - C# Corner

Bhushan Gupta
how to get sum of 1 to 100 prime number?
By Bhushan Gupta in C# onOct 28 2013
  • Bhushan Gupta
    Oct, 2013 28

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace pm
    {
        class Primes
        {
            static void Main(string[] args)
            {

              int n = 2, totalPrimeNumbers = 1, x;
                double sumOfPrimes = 2, average;

                while (n <= 100)
                {
                    // test if n is prime
                    for (x = 2; x < n; x++)
                    {
                        if ((n % x) != 0)
                        {
                            sumOfPrimes = sumOfPrimes + n;
                            totalPrimeNumbers++;
                            // change value of x to end for loop
                            x = n + 1;
                        }
                    }
                    // increase n by 1 to test next number
                    n++;
                }
                Console.WriteLine("Sum - " + sumOfPrimes);

            }
            }
    }

    • 1

Most Popular Companies

Most Popular Job Functions

MOST LIKED QUESTIONS