Viết chương trình Pascal tính
s=1 + 1/2^2 + 1/3^2 + ... + 1/n^2
Hãy nhập câu hỏi của bạn vào đây, nếu là tài khoản VIP, bạn sẽ được ưu tiên trả lời.
a)
uses crt;
var s,i,n:integer;
begin
clrscr;
write('nhap n='); readln(n);
s:=0;
for i:=1 to n do
s:=s+i;
writeln(s);
readln;
end.
b)
uses crt;
var s,i,n:integer;
begin
clrscr;
write('nhap n='); readln(n);
s:=0;
i:=1;
while i<=n do
begin
s:=s+i;
inc(i);
end;
writeln(s);
readln;
end.
2:
#include <bits/stdc++.h>
using namespace std;
double s;
int n,i;
int main()
{
cin>>n;
s=0;
for (i=1; i<=n; i++)
s=s+(i*1.0)/((i+1)*1.0);
cout<<fixed<<setprecision(2)<<s;
return 0;
}
program tinhtong;
var n,i : integer;
S : real;
begin
write('Nhap n: ');
readln(n);
i:=1;
S:=0;
while i<=n do
begin
S:=S+1/i;
i:=i+1;
end;
writeln('Tong S= ',S);
readln;
end.
program tinh_tong_S;
var
n, i: integer;
S: real;
begin
write('Nhap n: ');
readln(n);
S := 0;
i := 1;
while i <= n do
begin
S := S + 1 / i;
i := i + 1;
end;
writeln('Tong S la: ', S:0:2);
readln;
end.
*while..do
Program HOC24;
var i,n: integer;
s: real;
begin
write('Nhap n: '); readln(n);
i:=1;
while i<=n do
begin
s:=s+1/sqr(i);
i:=i+1;
end;
write('S= ',s:6:2);
readln
end.
uses crt;
var i,n,s:longint;
begin
clrscr;
readln(n);
s:=0;
for i:=1 to n do
s:=s+sqr((2*i-1));
writeln(s);
readln;
end.
uses crt;
var n,i,s:integer;
begin
clrscr;
write('Nhap n='); readln(n);
s:=0;
i:=1;
while i<=n do
begin
s:=s+i;
inc(i);
end;
writeln(s);
readln;
end.
Lời giải:
program hotrotinhoc ;
var s: real ;
i,n : integer ;
begin
write('n='); readln(n);
for i:= 1 to n do
s:=s+1/(i*i) ;
write('s=',s);
readln
end.
uses crt;
var a:array[1..10000000] of longint;
i,n,d:longint;
s:real;
begin
clrscr;
s:=1;
write('Nhap n la ');readln(n);
for i:=2 to n do
s:=s+(1/(i*i));
write('Tong la ',s:0:2);
readln
end.
uses crt;
var a:array[1..10000000] of longint;
i,n,d:longint;
s:real;
begin
clrscr;
s:=1;
write('Nhap n la ');readln(n);
for i:=2 to n do
s:=s+(1/(i*i));
write('Tong la ',s:0:2);
readln
end.