Monday, August 11, 2014

Câu 1.2: Viết hàm tính giai thừa của một số nguyên không âm. 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 in ra màn hình giai thừa của n.

// bai2.cpp : Defines the entry point for the console application.
// code Dung NV

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

unsignedlong giaithua(intn)
{
                inti;
                unsignedlong gt=1;
                if(n==0 || n==1) return 1;
                for(i=2;i<=n;i++) gt=gt*i;
                returngt;
}

int_tmain(int argc, _TCHAR* argv[])
{
                intn;
                printf("\nNhap N=   ");
                scanf("%d",&n);
                printf("\nGiai thua  %3d!  =  %lu",n,giaithua(n));
                getch();
                return0;

}

No comments:

Post a Comment