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.

Monday, August 11, 2014
// bai2.cpp : Defines the entry point for the console application.
// code Dung NV

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

unsigned long giaithua(int n)
{
                int i;
                unsigned long gt=1;
                if (n==0 || n==1) return 1;
                for (i=2;i<=n;i++) gt=gt*i;
                return gt;
}

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

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

All comments [ 0 ]


Your comments