Untitled

mail@pastecode.io avatar
unknown
csharp
a year ago
1.4 kB
2
Indexable
Never
using System;
namespace Program
{
    class Test
    {
        static void Main(string[] args)
        {
            Console.Clear();
            System.Console.Write("n = ");
            int n = Convert.ToInt32(Console.ReadLine());

            int[] a = new int[n];

            input(a, n);
            output(a, n);

            System.Console.Write("\ncac so nguyen to trong mang la: ");

            for (int i = 0; i < n; i++)
            {
                if (isPrime(a[i]))
                {
                    System.Console.Write(a[i] + " ");
                }
            }
        }
        static void input(int[] a, int n)
        {
            for (int i = 0; i < n; i++)
            {
                System.Console.Write($"a[{i}] = ");
                a[i] = Convert.ToInt32(Console.ReadLine());
            }
        }
        static void output(int[] a, int n)
        {
            System.Console.Write("Day vua nhap la: ");
            for (int i = 0; i < n; i++)
            {
                System.Console.Write(a[i] + " ");
            }
        }
        static bool isPrime(int n)
        {
            if (n < 2) return false;
            for (int i = 2; i <= Math.Sqrt(n); i++)
            {
                if (n % i == 0)
                {
                    return false;
                }
            }
            return true;
        }
    }
}