Monday, August 11, 2014

Câu 1.6: Viết hàm tính tổng s = 1^3 + 2^3 + ... + n^3, với n 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 tính và in ra màn hình tổng tương ứng

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

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

unsignedlong tong(intn)
{
                inti;
                unsignedlong s=0;
                for(i=1;i<=n;i++) s+=i*i*i;
                returns;
}

int_tmain(int argc, _TCHAR* argv[])
{
                intn;
                printf("\nNhap N=   ");
                scanf("%d",&n);
                printf("\n Tong s = %lu",tong(n));
                getch();
                return0;

}

No comments:

Post a Comment