We are providing online training of realtime Live project on Asp.Net MVC with Angular and Web API. For more information click here. If you have any query then drop the messase in CONTACT FORM

Friday, June 19, 2015

Write a program to check the prime numbers and Add the all prime numbers of store in Array.

    class Program
    {
        static void Main(string[] args)
        {
            int sum = 0, count = 0;
            Console.WriteLine("Enter the size of the array");
            int n = int.Parse(Console.ReadLine());
            int[] arr = new int[n];
            Console.WriteLine("Enter the Array Elements");
            for (int i = 0; i < n; i++)
            {
                arr[i] = int.Parse(Console.ReadLine());
            }           

            Console.WriteLine("Your array element");
            for (int i = 0; i < n; i++)
            {
                Console.Write(arr[i] + " ");
            }

            Console.WriteLine();
            Console.WriteLine("Your prime numbers are:");
            for (int i = 0; i < n; i++)
            {
                int num = 0;
                for (int j = 2; j < arr[i]; j++)
                {
                    if (arr[i] % j == 0)
                    {
                        num = 1;
                        break;
                    }
                }

                if (num == 0)
                {
                    Console.WriteLine(arr[i]);
                    sum = sum + arr[i];
                    count++;
                }
            }
            Console.WriteLine("The number of prime numbers is:{0}", count);
            Console.WriteLine("The sum of prime no. is:{0}", sum);

            Console.WriteLine();
            Console.ReadLine();

        }

    }

OUTPUT:

No comments: