Viết chương trình nhập vào các số nguyên cho đến khi nào gặp số 0 thì kết thúc. Tính tổng các số lẻ được nhập vào.
Viết chương trình nhập vào các số nguyên cho đến khi nào gặp số 0 thì kết thúc. Tính tổng các số lẻ được nhập vào.
refer
program dem;
uses crt;
var n,i,j,S: integer;
a:array[1..100] of integer;
begin
i:=0;
S:=0;
while(readln(n)) do
begin
if(n:=0) then break;
i:=i+1;
a[i]:=n;
end;
for j:=1 to i do
begin
if(a[j] mod 2 <> 0) then S:=S+a[j];
end;
write(‘Tong cua cac so le la: ‘,S);
readln;
Viết chương trình nhập vào các số nguyên từ bàn phím cho đến khi gặp số nguyên tố thì kết thúc nhập. Tính tổng các số chẵn trong dãy
uses crt;
var n,t:integer;
{------------------kiem-tra-so-nguyen-to----------------------------}
function ktnt(x:integer):boolean;
var i:integer;
kt:boolean;
begin
kt:=true;
for i:=2 to trunc(sqrt(x)) do
if x mod i=0 then
begin
kt:=false;
break;
end;
if kt=true then ktnt:=true
else ktnt:=false;
{----------------------chuong-trinh-chinh------------------------}
begin
clrscr;
t:=0;
repeat
write('Nhap n='); readln(n);
if (n mod 2=0) then t:=t+n;
until ktnt(n)=false;
writeln(t);
readln;
end.
Viết chương trình nhập vào nhiều số nguyên từ bàn phím, việc nhập kết thúc khi ta nhập vào số 0 hoặc nhập đủ 10 số. a) Đếm và in ra số các số đã nhập, không tính số 0. (VD: nhập 7, 4, 9, 0 -> Đã nhập 3 số) b)Tính và in ra tổng các số lẻ đã nhập
2.Viết chương trình nhập vào điểm theo thang điểm 10. Đổi sang thang điểm 4 và in kết quả ra màn hình. Biết từ 8.5 đến 10 thì đổi sang thang điểm 4 là 4. Từ 7 đến dưới 8.5 thì đổi sang thang điểm 4 là 3. Từ 5.5 đến dưới 7 thì đổi sang thang điểm 4 là 2. Từ 4 đến dưới 5.5 thì đổi sang thang điểm 4 là 1. Còn dưới 4 thì đổi sang thang điểm 4 là 0
mọi người code giùm ạ em cám ơn ngôn ngữ c
Bài 1:
#include <bits/stdc++.h>
using namespace std;
long long a[10],n,i,t;
int main()
{
n=0;
do
{
cin>>a[i];
}
while ((a[i]==0) or (n==10));
for (i=1; i<=n; i++) cout<<a[i]<<" ";
cout<<endl;
t=0;
for (i=1; i<=n; i++)
if (a[i]%2!=0) t=t+a[i];
cout<<t;
return 0;
}
Viết chương trình cho phép nhập vào nhiều số nguyên cho đến khi gặp số 0 thì kết thúc. Đếm xem trong các số vừa nhập có bao nhiêu số âm, bao nhiêu số dương. bằng Python
so_am = 0
so_duong = 0
while True:
n = int(input("Nhap vao mot so nguyen: "))
if n == 0:
break
elif n < 0:
so_am += 1
else:
so_duong += 1
print("So am: ", so_am)
print("So duong: ", so_duong)
Viết chương trình cho phép nhập vào số nguyên tu ban phim cho đến khi gặp số 0 thì kết thúc. Đếm xem trong các số vừa nhập có bao nhiêu số le so chan trong cac so vua nhap. bằng Python
count_even = 0
count_odd = 0
while True:
num = int(input("Nhap so nguyen: ")) //0 de kthuc
if num == 0:
break
if num % 2 == 0:
count_even += 1
else:
count_odd += 1
print("Số lẻ:", count_odd)
print("Số chẵn:", count_even)
Dùng câu lệnh while...do viết chương trình nhập các số nguyên kết thúc khi nhập bằng 0 . Tính tổng các số chẵn , tổng câc số lẻ đã nhập rồi in kết quả ra màn hình
uses crt;
var n,i,t1,t2:integer;
begin
clrscr;
t1:=0;
t2:=0;
repeat
readln(n);
if n mod 2=0 then t1:=t1+n
else t2:=t2+n;
until n=0;
writeln(t1,' ',t2);
readln;
end.
Dùng câu lệnh while...do viết chương trình nhập các số nguyên kết thúc khi nhập bằng 0. Tính tổng các số chẵn, tổng các só lẻ đã nhập, rồi in kết quả ra màn hình?
Viết chương trình nhập vào một dãy số nguyên cho đến khi gặp số 0 thì dừng lại. Tính tổng các số chẵn, thông báo ra màn hình.
Program HOC24;
var i,n: integer;
t: longint;
begin
write('Nhap so: ');
readln(n);
if n mod 2=0 then t:=n else t:=0;
while n<>0 do
begin
write('Nhap so: ');
readln(n);
if n mod 2=0 then t:=t+n;
end;
write('Ket qua la: ',t);
readln
end.
Viết chương trình nhập và tính tổng các số nguyên khi nào nhập vào số 0 thì dừng lại và thông báo tổng các số đã nhập
uses crt;
var a,t:integer;
begin
clrscr;
t:=0;
repeat
write('Nhap a='); readln(a);
t:=t+a;
until a=0;
writeln(t);
readln;
end.