Câu 1.4: Viết hàm tính tổng s = 2 + 4 + 6 + … + 2*n, 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.

Monday, August 11, 2014


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

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

unsigned long tong(int n)
{
                int i;
                unsigned long s=0;
                for(i=1;i<=n;i++) s+=2*i;
                return s;
}
int _tmain(int argc, _TCHAR* argv[])
{
                int n;
                printf("\nNhap N=   ");
                scanf("%d",&n);
                printf("\n Tong s = %lu",tong(n));
                getch();
                return 0;

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

All comments [ 0 ]


Your comments