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?

Monday, August 11, 2014
// bai7.cpp : Defines the entry point for the console application.
// code Du~ng NV

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

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

}
Chia sẻ bài viết ^^
Other post

All comments [ 0 ]


Your comments