a) S = 1 + 2 + 3 + ...+ n
b) S = 1/2 + 1/3 + ......+ n/(n+1)
c) S = -1 +2 -3 + 4 - .....+ (-1)^n.n
Bài Giải
/*
Name: Tinh tong cac day so
Copyright: None
Author: Tran Anh
Description: http://www.code.tavn.net
*/
#include <conio.h>
#include <stdio.h>
main()
{
//Trang tri chuong trinh
int i;
printf("\t");
for (i=1;i<30;i++) printf("* ");
printf("\n\t\t CHUONG TRINH TINH TOAN CAC DANG LIEN TUC\n\n");
//Het doan chuong trinh trang tri
printf("\t Cac dang phep toan: \n");
printf("\t 1. S = 1 + 2 + 3 + ... + n \n");
printf("\t 2. S = 1/2 + 2/3 + ... + n/(n+1) \n");
printf("\t 3. S = -1 + 2 - 3 + 4 ... + (-1)^n.n \n");
int sty,n;
do
{
printf("\n\t - Ban chon loai phep tinh thu: ");
scanf("%d",&sty);
if (sty!=1&&sty!=2&&sty!=3) printf("\t\a * * * Ban nhap sai (Chi co the 1, 2, hoac 3)");
}
while (sty!=1&&sty!=2&&sty!=3);
printf("\n\t - Nhap gia tri N= "); scanf("%d",&n);
int sum=0;
float fsum=0,j;
if (sty==1)
{
for(i=1;i<=n;i++)
sum+=i;
}
if (sty==2)
{
for (i=1;i<=n;i++)
fsum+=(float(i)/float(i+1));
}
if (sty==3)
{
for (i=1;i<=n;i++)
if (i%2==0)
sum+=i;
else
sum-=i;
}
if (sty!=2)
printf("\n\t=> Tong day so la: %d",sum);
else
printf("\n\t=> Tong day so la: %2.2f",fsum);
printf("\n\n\n\n\t\t\t\t http://www.code.tavn.net");
getch();
return 0;
}
Name: Tinh tong cac day so
Copyright: None
Author: Tran Anh
Description: http://www.code.tavn.net
*/
#include <conio.h>
#include <stdio.h>
main()
{
//Trang tri chuong trinh
int i;
printf("\t");
for (i=1;i<30;i++) printf("* ");
printf("\n\t\t CHUONG TRINH TINH TOAN CAC DANG LIEN TUC\n\n");
//Het doan chuong trinh trang tri
printf("\t Cac dang phep toan: \n");
printf("\t 1. S = 1 + 2 + 3 + ... + n \n");
printf("\t 2. S = 1/2 + 2/3 + ... + n/(n+1) \n");
printf("\t 3. S = -1 + 2 - 3 + 4 ... + (-1)^n.n \n");
int sty,n;
do
{
printf("\n\t - Ban chon loai phep tinh thu: ");
scanf("%d",&sty);
if (sty!=1&&sty!=2&&sty!=3) printf("\t\a * * * Ban nhap sai (Chi co the 1, 2, hoac 3)");
}
while (sty!=1&&sty!=2&&sty!=3);
printf("\n\t - Nhap gia tri N= "); scanf("%d",&n);
int sum=0;
float fsum=0,j;
if (sty==1)
{
for(i=1;i<=n;i++)
sum+=i;
}
if (sty==2)
{
for (i=1;i<=n;i++)
fsum+=(float(i)/float(i+1));
}
if (sty==3)
{
for (i=1;i<=n;i++)
if (i%2==0)
sum+=i;
else
sum-=i;
}
if (sty!=2)
printf("\n\t=> Tong day so la: %d",sum);
else
printf("\n\t=> Tong day so la: %2.2f",fsum);
printf("\n\n\n\n\t\t\t\t http://www.code.tavn.net");
getch();
return 0;
}
Không có nhận xét nào:
Đăng nhận xét