Monday, August 11, 2014

Câu 1.7: Viết hàm kiểm tra tính nguyên tố của một số nguyên dương. Viết chương trình nhập vào từ bàn phím số nguyên dương n, áp dụng hàm trên hãy cho biết n có phải là số nguyên tố hay không?

// bai7.cpp : Defines the entry point for the console application.
// code Du~ng NV

#include"stdafx.h"
#include<conio.h>
#include<stdio.h>

intktsnto(int n)
{
    int i;
                if(n==0 || n==1) return 0;
                for(i=2;i*i<=n;i++) if (n%i==0) return 0;
                return1;
}
int_tmain(int argc, _TCHAR* argv[])
{
                intn;
                printf("\nNhap N=   ");
                scanf("%d",&n);
                if(ktsnto(n)) printf("\n%5d La so nguen to",n);
                elseprintf("\n%5d khong phai la so nguen to",n);
                getch();
                return0;

}

No comments:

Post a Comment