Thursday, 9 January 2014

C# - Prime No. from 1 to 100 and find the sum of prime no.


I have given here the C# program to find the Prime No. from 1 to 100 and find the sum of prime no.

Source Code:--

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            bool isPrime = true;
            int w = 0;
            for (int i = 2; i <= 100; i++)
            {
                for (int j = 2; j <= 100; j++)
                {
                    if (i != j && i % j == 0)
                    {
                        isPrime = false;
                        break;
                    }
                }
                if (isPrime)
                {
                    Console.WriteLine("Prime: "+ i);
                     w = w + i;
                }
                isPrime = true;
            }
            Console.WriteLine("sum: " + w);
            Console.ReadLine();
        }
    }
}

No comments:

Post a Comment

Thanks for comments.