
C++ Напишите программу, которая в последовательности натуральных чисел находит
среднее арифметическое чисел, кратных 8, или сообщает, что таких чисел нет (выводит «NO»). Программа получает на вход натуральные числа, количество введённых чисел неизвестно,последовательность чисел заканчивается числом 0 (0 – признак окончания ввода, не входит в последовательность). Количество чисел не превышает 100. Введённые числа не превышают 300. Программа должна вывести среднее арифметическое чисел, кратных 8, или вывести «NO», если таких чисел нет. Значение выводить с точностью до десятых. C++

Ответы на вопрос

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int num, counter = 0;
int sum = 0;
cin >> num;
while(num)
{
if(num % 8 == 0)
{
counter++;
sum += num;
}
cin >> num;
}
if(counter == 0)
cout << "NO";
else
cout << round((sum / (float)counter) * 10) / 10;
return 0;
}



Program to Find the Average of Numbers Divisible by 8
To create a program that calculates the average of numbers divisible by 8 from a sequence of natural numbers, we can use C++ to achieve this. The program will take a sequence of natural numbers as input, and the sequence will end with the input of the number 0. The program will then calculate the average of the numbers divisible by 8 and output the result with a precision of up to one decimal place.
Here's a sample C++ program to achieve this:
```cpp
#include
int main() { int num, count = 0, sum = 0;
while (true) { cin >> num; if (num == 0) { break; } if (num % 8 == 0) { sum += num; count++; } }
if (count == 0) { cout << "NO" << endl; } else { float average = (float)sum / count; cout << "Average of numbers divisible by 8: " << fixed << setprecision(1) << average << endl; }
return 0; } ```
This program uses a while loop to continuously read input numbers until 0 is encountered. It checks if each number is divisible by 8 and calculates the sum and count of such numbers. Finally, it outputs the average of the numbers divisible by 8 or "NO" if there are no such numbers in the sequence.
Note: The program assumes that the input numbers are within the specified range and are valid natural numbers.
Feel free to modify and use this program according to your specific requirements.


Похожие вопросы
Топ вопросов за вчера в категории Информатика







Последние заданные вопросы в категории Информатика
-
Математика
-
Литература
-
Алгебра
-
Русский язык
-
Геометрия
-
Английский язык
-
Химия
-
Физика
-
Биология
-
Другие предметы
-
История
-
Обществознание
-
Окружающий мир
-
География
-
Українська мова
-
Информатика
-
Українська література
-
Қазақ тiлi
-
Экономика
-
Музыка
-
Право
-
Беларуская мова
-
Французский язык
-
Немецкий язык
-
МХК
-
ОБЖ
-
Психология
-
Физкультура и спорт
-
Астрономия
-
Кыргыз тили
-
Оʻzbek tili