C,
pasted
on Aug 17:
|
#include<stdio.h>
#include<conio.h>
int main()
{
int i, n;
float S;
S = 0; i = 1;
do
{
printf("\nNhap n: ");
scanf("%d", &n);
if(n < 1)
{
printf("\nN phai lon hon hoac bang 1. Xin nhap lai !");
}
}while(n < 1);
while(i <= n)
{
S = S + 1.0 / i; // phải nhớ là 1.0 / i
i++;
}
printf("i = %d", i);
printf("\nS = %f", S);
printf("\nTong 1 + 1/2 + ... + 1/%d la %.2f: ",n, S);
getch();
return 0;
}
|
Output:
|
Line 17: error: conio.h: No such file or directory
|
|
Create a new paste based on this one
Comments:
posted by
khanhtienfz
on Jun 24
Ko cần return 0 cũng được nhưng sẽ chính xác và đúng hơn nếu ta thêm vào.(0 có nghĩa tất cả hoạt động tốt)
reply
posted by
hellboynice102
on Feb 1
#include <iostream>
using namespace std;
int main ()
{
int n;
cin >> n;
double s = 0;
for (int i = 1; i <= n; i++)
{
s = (double) 1 / i + s;
}
cout << s;
return 0;
}
reply
posted by
incarnation16
on May 21
#include <stdio.h>
void SUM(int n)
{
float sum = 0;
for (int i = 1; i <= n; i++)
{
sum += 1.0 / i;
}
}
int main()
{
int n;
do
{
scanf("%d", &n);
} while (n < 1);
SUM(n);
printf("%.2f", SUM);
return 0;
}
Tai sao code k chay vay mn?
reply
posted by
klefo
on Jan 11
#include <stdio.h>
float SUM(int n)
{
float x = 0;
for (int i = 1; i <= n; i++)
{
x += 1.0 / i;
}
return x;
}
int main()
{
int n;
do
{
scanf("%d", &n);
if(n < 1)
{
printf("\nerror, wrong input !");
}
} while (n < 1);
float a = 0;
a = SUM(n);
printf("%.5f", a);
return 0;
}
cậu xem lại code này nhá
reply
posted by
klefo
on Jan 11
vì ta đặt i bằng 1 rồi nên chúng ta sẽ cộng bắt đầu từ 1/1 trở lên chứ không tính từ 1 nữa
reply