Bài 1: Viết chương trình tính các tổng sau: (với n là số tự nhiên được nhập từ bàn phím)
a) A = 1 + 2 + 3 + . . . + n
b C = 2 + 4 + 6 + . . . + n (n: số chẵn)
c E = 1 + 3 + 5 + . . . + n (n: số lẻ)
Bài 1: Viết chương trình tính các tổng sau: (với n là số tự nhiên được nhập từ bàn phím)
a) A = 1 + 2 + 3 + . . . + n
b C = 2 + 4 + 6 + . . . + n (n: số chẵn)
c E = 1 + 3 + 5 + . . . + n (n: số lẻ)
a)
Program Hotboy ;
Uses crt;
Var s,n,i : integer ;
Begin
Write(‘nhao so n’); readln(n);
For i:=1 to n do
S:= s+i;
i:=i+1 ;
Write(‘tong la’,s);
Readln
End.
b) Program Hotboy ;
Uses crt;
Var s,n,i : integer ;
Begin
Write(‘nhao so n’); readln(n);
For i:=2 to n do
S:= s+i;
i:=i+2;
Write(‘tong la’,s);
Readln
End.
c) Program Hotboy ;
Uses crt;
Var s,n,i : integer ;
Begin
Write(‘nhao so n’); readln(n);
For i:=1 to n do
S:= s+i;
i:=i+2 ;
Write(‘tong la’,s);
Readln
End.
a)
Program FNG;
Uses crt;
Var n,i,s: longint;
Begin
Clrscr;
Write(‘Nhap n: ‘); Readln(n);
For i:=1 to n do
s:=s+i;
Write(‘S = ‘,s);
Readln
End.
b)
Program FNG;
Uses crt;
Var n,i,s: longint;
Begin
Clrscr;
Write(‘Nhap n: ‘); Readln(n);
For i:=1 to n do
If i mod 2 = 0 then
s:=s+i;
Write(‘S = ‘,s);
Readln
End.
c)
Program FNG;
Uses crt;
Var n,i,s: longint;
Begin
Clrscr;
Write(‘Nhap n: ‘); Readln(n);
For i:=1 to n do
If i mod 2 = 1 then
s:=s+i;
Write(‘S = ‘,s);
Readln
End.