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
Tags:
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]](https://www.gravatar.com/avatar/5db166b7b74443c5c221e4c0068d6da9.jpg?s=80&d=mm&r=g)
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]](https://www.gravatar.com/avatar/a07ddfb51e1e7189c76b4e42dbdbcddc.jpg?s=80&d=mm&r=g)
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]](https://www.gravatar.com/avatar/22abcac77d8ca5e01144e240abb48d22.jpg?s=80&d=mm&r=g)
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]](https://www.gravatar.com/avatar/b107d14d7d43c142b68c12c377262371.jpg?s=80&d=mm&r=g)
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]](https://www.gravatar.com/avatar/035e4f4fed661b8e1c3e066e43cd5e41.jpg?s=80&d=mm&r=g)
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]](https://www.gravatar.com/avatar/223a7e3a46f4a747f81b921fe023fcc4.jpg?s=80&d=mm&r=g)
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]](https://www.gravatar.com/avatar/ecca255d725eed36a872105205af1b8e.jpg?s=80&d=mm&r=g)
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]](https://www.gravatar.com/avatar/33675cc9fc3762fd323389a179aa047f.jpg?s=80&d=mm&r=g)
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]](https://www.gravatar.com/avatar/04c40301dd027839d265b3c3c9dc6e6b.jpg?s=80&d=mm&r=g)
Đỗ 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]](https://www.gravatar.com/avatar/ee4661d1f9133c241d6c8287c7ea8ceb.jpg?s=80&d=mm&r=g)
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();
}
}
}