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# BT1396

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

Liên kết rút gọn:

https://gokisoft.com/1396

Bình luận

avatar
hieuvm0512 [community,C2010L]
2021-10-02 08:33:43



using System;

namespace SoNguyenTo
{
    class Program
    {
        static void Main(string[] args)
        {
            int n,kt,j;
            int i = 2;
            Console.WriteLine("Nhap vao N");
            n = Convert.ToInt32(Console.ReadLine());
            while (n>=i)
            {
                 kt = 1;
                 j = 2;
                 while(j<=i/2){
                     if(i%j==0){
                         kt = 0;
                         break;
                     }
                     j++;
                 }
                 if(kt == 1){
                     Console.Write(i + " ");
                 }
                 i++;

            }
            
        }
    }
}


avatar
Trần Việt Đức Anh [C2010L]
2021-10-01 12:19:42

#Ex1396


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

using System;

using System;

namespace Ex1396
{
    internal class Program
    {
        private static bool checkPrime(int a)
        {
            if (a == 2) 
            return true;

            for (int i = 2; i <= Math.Sqrt(a); i++)
            {
                if (a % i == 0) return false;
            }
            return true;
        }

        private static void Main(string[] args)
        {
            int n;
            n = Convert.ToInt32(Console.ReadLine());
            for (int i = 2; i <= n; i++)
            {
                if (checkPrime(i)) 
                Console.Write("{0} ", i);
            }
            Console.ReadKey();
        }
    }
}


avatar
Đào Mạnh Dũng [C2010L]
2021-09-30 14:18:10

using System;

class NumEle

{

    public static void Main()

    {

        Console.Write("nhap n :");

        int n = int.Parse(Console.ReadLine());


        for (int i=1;i<=n;i++)

        {

            if (isNumEle(i))

            {

                Console.WriteLine(i);

            }

        }


    }

    public static bool isNumEle(int num)

    {

        int number;

        int bien_dem = 0;


        number = num;


        for (int i = 1; i <= number; i++)


            if (number % i == 0)

                bien_dem++;


        if (bien_dem == 2)

            return true;

        else

            return false;



    }


}

avatar
Phạm Đăng Khoa [community,C2010L]
2021-09-30 13:23:59



using System;

namespace Lesson01
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, n;
            Console.WriteLine("Nhap vao n:");
            n = int.Parse(Console.ReadLine());
            for(i = 0; i<=n; i++)
            {
                Console.WriteLine("i={0}", i);
            }
        }
    }
}


avatar
Do Xuan Dien [community,C2010G]
2021-09-27 10:55:05


#Program.cs


using System;

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

            for (int i = 1; i <= N; i++)
            {
                if (checkPrime(i))
                {
                    Console.Write("{0} , ", i);
                }

            }
        }

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

            return true;
        }
    }
}


avatar
Do Trung Duc [T2008A]
2021-05-18 04:45:33



using System;

namespace Timsonguyento
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Nhap so tu nhien N");
            int N = Int32.Parse(Console.ReadLine());
          
            for (int i = 1; i < N; i++)
            {
               int count = 0;
                for(int j = 2; j < i; j++)
                {
                    if(i % j == 0)
                    {
                        count++;
                    }
                }
                if (count == 0)
                {
                    Console.WriteLine(i);
                    Console.WriteLine("");
                }

            }

        }
    }
}


avatar
Triệu Văn Lăng [T2008A]
2021-05-17 08:20:07



using System;

namespace bai1396
{
    class DaySoNT
    {         
        static void Main(string[] args) 
        {
            Console.WriteLine("Hello World!");
            Console.WriteLine("Nhap so n = ");
            int n = Int32.Parse(Console.ReadLine());

            if (n < 2)
            {
                Console.WriteLine("So nguyen to phai lon hon 1");
            } else
            {
                Console.WriteLine("So vua nhap la " + n);
                Console.WriteLine("Day so nguyen to nho hon " + n + " la: ");
                for(int i = 2; i<=n; i++)
                {
                    if(i%2 != 0)
                    {
                        Console.WriteLine(i);
                    }
                }
            }

            
        }
    }
}


avatar
Nguyễn Tiến Đạt [T2008A]
2021-05-14 09:35:26



using System;

namespace ptb1
{
    internal class Program
    {
        private static bool checkPrime(int a)
        {
            if (a == 2) return true;
            for(int i = 2; i <= Math.Sqrt(a); i++)
            {
                if (a % i == 0) return false;
            }
            return true;
        }
        private static void Main(string[] args)
        {
            int n;
            n = Convert.ToInt32(Console.ReadLine());
            for(int i = 2; i <= n; i++)
            {
                if (checkPrime(i)) Console.Write("{0} ", i);
            }
        }
    }
}


avatar
hainguyen [T2008A]
2021-05-14 09:12:42



using System;

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

            for (int i = 0; i < n; i++)
            {
                    Console.WriteLine(i);
            }
        }
    }
}


avatar
vuong huu phu [T2008A]
2021-05-14 09:08:11



using System;

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

            if (n < 2)
            {
                Console.WriteLine("Nhap lai");
            }
            else
            {
                Console.WriteLine("So ngyen to la : " );
                int i;
                for (i = 2; i < n; i++)
                {
                    if (i % 2 != 0)
                    {
                        Console.WriteLine(i);
                    }
                }
            }
        }
    }
}