By GokiSoft.com| 10:08 05/10/2021|
C Sharp

Tìm dãy số nguyên tố - Lập trình C# - Loop trong C#

Nhập vào số N -> in ra các số nguyên tố <= N

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Phí Văn Long [T1907A]
Phí Văn Long

2020-05-20 05:26:55



using System;

namespace SoNguyenTo
{
    class Program
    {
        static void Main(string[] args)
        {
            int N;

            Console.WriteLine("Nhap so N = ");
            N = Int32.Parse(Console.ReadLine());

            for(int i = 1; i <= N;i++)
            {
                if (checkSNT (i)) 
                {
                    Console.Write("Cac so nguyen to la : {0}",i);
                }
            }
        }
        static bool checkSNT(int num)
        {
            //Neu num chia het cho 1 trong cac so chay tu 2 -> num/2 
            //Num kp la so nguyen to
            //num la so nguyen to : khi no chia het cho 1 va chinh num

            int max = num / 2;
            for (int i = 2; i <= max; i++)
            {
                if(num % i == 0)
                {
                    return false;
                }
            }
            return true;
        }
    }
}



lê văn phương [T1907A]
lê văn phương

2020-05-19 03:53:02



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

namespace vd3
{
    class Program
    {
        static void Main(string[] args)
        {
            int N;
            Console.WriteLine("Nhap so N =");
            N = Int32.Parse(Console.ReadLine());

            for (int i = 1; i <= N; i++)
            {
                if (checkSNT(i))
                {
                    Console.Write("{0},", i);
                }
            }
            Console.ReadKey();
        }
        static bool checkSNT(int num)
        {
            int max = num / 2;
            for (int i =2; i <=max; i++)
            {
                if(num % i == 0)
                {
                    return false;
                }
            }
            return true;
        }

    }
}



Lê Minh Bắc [T1907A]
Lê Minh Bắc

2020-05-19 03:35:36



using System;

namespace Lession2
{
    class SNT
    {
        static void Main(string[] args)
        {
            int n;
            Console.Write("nhap n: ");
            n = Int32.Parse(Console.ReadLine());
            Console.Write("Day so Nguyen To <= {0}: ", n);
            for (int i = 1; i <= n; i++)
            {
                if (check(i))
                {
                    Console.Write("{0} ", i);
                }
            }

            Console.ReadKey();
        }
        static bool check(int n)
        {
            int max = n / 2;
            for (int i = 2; i <= max; i++)
            {
                if (n % i == 0)
                {
                    return false;
                }
            }
            return true;
        }
    }
}




nguyễn văn huy [T1907A]
nguyễn văn huy

2020-05-19 02:53:21



using System;

namespace lesson
{
    class Program
    {
        static void Main(string[] args)
        {
            int n;
            Console.WriteLine("nhập n");
            n = Int32.Parse(Console.ReadLine());
            for(int i = 1; i <= n; i++)
            {
                if (check(i))
                {
                    Console.Write("{0}", +i);
                }
            }
        }
        static bool check(int a)
        {
           int b = a / 2;
            for (int i = 2; i<= b; i++)
            {
                if (a % i == 0)
                {
                    return false;
                }
            }
            return true;
        }
    }
}



NguyenHuuThanh [T1907A]
NguyenHuuThanh

2020-05-18 06:30:48



using System;
using System.Globalization;

namespace Primenumber
{
    class Program
    {
        static void Main(string[] args)
        {
            int Primenumber = 2;
            int n;
            n = int.Parse(Console.ReadLine());
            while ( Primenumber < n)
            {
                if (isPrime(Primenumber) == true)
                {
                    Console.WriteLine(Primenumber);
                }
                Primenumber++;
            }

        }

        static bool  isPrime(int n)
        {
            if ( n < 2 )
            {
                return false;
            }
            else
            {
                for ( int i = 2; i < Math.Sqrt(n); i++)
                {
                    if ( n % i == 0)
                    {
                        return false;
                    }
                }
            }

            return true;
        }
    }
}



Trương Công Vinh [T1907A]
Trương Công Vinh

2020-05-16 07:38:59



using System;

namespace songuyento
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            int n;
            Console.WriteLine("Nhap vao so N:");
            n = int.Parse(Console.ReadLine());

            Console.WriteLine("Day so nguyen to nho hon {0} :  ",n);
            for (int i = 0; i < n; i++)
            {
                int dem = 0;
                for (int j = 2; j < n; j++)
                {
                    if (i % j == 0)
                    {
                        dem++;
                    }
                }
                if (dem == 1)
                {
                    Console.Write("{0} , ",i);
                }
            }
        }
    }
}



Minh Nghia [T1907A]
Minh Nghia

2020-05-16 02:51:21



using System;

namespace SoNguyenTo
{
    class Program
    {
        static void Main(string[] args)
        {
            ListPrimes();
            Console.ReadLine();
        }
        public static bool CheckPrimeNumbers(int Number)
        {
            if(Number < 0)
            {
                return false;
            }
            for(int i = 2; i < Number; i++)
            {
                if(Number % i ==0)
                {
                    return false;
                }
            }
            return true;
        }
        public static void ListPrimes()
        {
            Console.Write("Enter the number n = ");
            int n = Convert.ToInt32(Console.ReadLine());

            for(int i = 2; i < n; i++)
            {
                bool TestResult = CheckPrimeNumbers(i);

                if (TestResult)
                {
                    Console.WriteLine(i);
                }
            }

            
            
        }
    }
}



hoangduyminh [T1907A]
hoangduyminh

2020-05-16 00:26:29



using System;

namespace SoNguyenTo
{
    class Program
    {
        static void Main(string[] args)
        {
            int n;
            Console.WriteLine("Nhap vao so N:");
            n = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Cac so nguyen to nho hon n:" + n);
            for(int i = 1; i < n; i++)
            {
                int t = 0;
                for(int j = 1; j < n; j++)
                {
                    if(i%j == 0)
                    {
                        t++;
                    }
                }
                if(t == 2) {
                    Console.WriteLine("" + i);
                }
            }
        }
    }
}



Đỗ Văn Huấn [T1907A]
Đỗ Văn Huấn

2020-05-15 12:03:57



using System;

namespace vidu1
{
    public class SoNguyenTo
    {
        static void Main(string[] args)
        {
            Console.WriteLine("NHap vao 1 so: ");
            int number = int.Parse(Console.ReadLine());
            Hienthi(number);
        }

        public static void Hienthi(int number)
        {
            for (int i = 1; i < number; i++)
            {
                bool check = Kiemtre(i);
                if (check)
                {
                    Console.WriteLine(i);
                }
            }
        }
    
        public static Boolean Kiemtre(int a)
        {
            if (a < 0)
            {
                Console.WriteLine("Khong ton tai so nguyen to trong khoang tu 0 den {0}.",a);
                return false;
            }
    
            for (int i = 2; i <= a; i++)
            {
                //nếu số nhập vào chia hết cho một số bất kì trong khoảng từ 2 đến a  thì không phải số nguyên tố
                if (a % i == 0)
                {
                    return false;
                }
            }
            return true;
        }
    }
    
}



Trần Mạnh Dũng [T1907A]
Trần Mạnh Dũng

2020-05-15 10:28:32



using System;

namespace songuyento
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter the start number of the array: ");
            int start = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter the end number of the array: ");
            int end = Convert.ToInt32(Console.ReadLine());
            Console.Write("Array of prime numbers from {0} to {1} : ", start, end);

            for (int num = start; num <= end; num++)
            {
                int ctr = 0;

                for (int i = 2; i <= num / 2; i++)
                {
                    if (num % i == 0)
                    {
                        ctr++;
                        break;
                    }
                }

                if (ctr == 0 && num != 1)
                    Console.Write("{0} ", num);
            }
            

            Console.ReadKey();
        }
    }
}