By GokiSoft.com| 19:20 04/10/2023|
Lập Trình C

Bài thi lập trình C - 60 phút

Tags:

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

5

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

GokiSoft.com
GokiSoft.com

2023-10-04 13:34:48


#main.c


#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
/**
	0: Khong phai la so nguyen to
	1: So nguyen to
*/
int isPrime(int number) {
	if(number < 2) return 0;
	int i;
	
	//Toi uu i < number
	for(i=2;i<number;i++) {
		if(number % i == 0) {
			return 0;
		}
	}
	
	return 1;
}

//void prime(int number) {
//	if(number < 2) return;
//	int i;
//	
//	//Toi uu i < number
//	for(i=2;i<number;i++) {
//		if(number % i == 0) {
//			return;
//		}
//	}
//	
//	printf("\n%d", number);
//}

int main(int argc, char *argv[]) {
	int n, m, i, checked;
	
	printf("\nNhap so n = ");
	scanf("%d", &n);
	printf("\nNhap so m = ");
	scanf("%d", &m);
	
	if(n > m) {
		//swap gia tri n & m
		n = n + m;
		m = n - m;
		n = n - m;
	}
	
	printf("\nDanh sach so nguyen to: ");
	for(i=n;i<=m;i++) {
		checked = isPrime(i);
		if(checked == 1) {
			printf("\n%d", i);
		}
//		prime(i);
	}
	
	return 0;
}