Вопрос задан 16.02.2019 в 20:53. Предмет Информатика. Спрашивает Лосев Владислав.

Написать программу на С++. Написать подпрограмму, которая принимает один аргумент и выводит на

экран факториал переданного ей числа.
0 0
Перейти к ответам

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

Внимание! Ответы на вопросы дают живые люди. Они могут содержать ошибочную информацию, заблуждения, а также ответы могут быть сгенерированы нейросетями. Будьте внимательны. Если вы уверены, что ответ неверный, нажмите кнопку "Пожаловаться" под ответом.
Отвечает Ковальчук Владислав.
#include <iostream>
using namespace std; 

void fac(int n) {
   int s=1;  
   for (int i=1; i<=n; i++) s*=i;
   cout<<s<<endl;
}    

int main() {   
    int n;
    cout<<"number: ";  cin>>n;
    fac(n);
   system("pause");
    return 0;
}


0 0
Отвечает нейросеть ChatGpt. Будьте внимательны, ответы нейросети могут содержать ошибочные или выдуманные данные.

Writing a C++ Program to Calculate Factorial

To write a C++ program that calculates the factorial of a given number, you can follow the steps below.

1. Include Necessary Libraries: - Include the necessary libraries for input/output operations and mathematical calculations.

```cpp #include using namespace std; ```

2. Define the Main Function: - Define the main function where the program execution begins.

```cpp int main() { // Program logic will go here return 0; } ```

3. Accept User Input: - Accept the input from the user, which is the number for which the factorial needs to be calculated.

```cpp int num; cout << "Enter a number: "; cin >> num; ```

4. Calculate the Factorial: - Write a loop to calculate the factorial of the input number.

```cpp int factorial = 1; for (int i = 1; i <= num; i++) { factorial *= i; } ```

5. Display the Result: - Output the factorial of the input number.

```cpp cout << "Factorial of " << num << " = " << factorial << endl; ```

Putting it all together, the complete C++ program to calculate the factorial of a given number looks like this:

```cpp #include using namespace std;

int main() { int num; cout << "Enter a number: "; cin >> num;

int factorial = 1; for (int i = 1; i <= num; i++) { factorial *= i; }

cout << "Factorial of " << num << " = " << factorial << endl;

return 0; } ```

This program prompts the user to enter a number, calculates its factorial, and then displays the result.

0 0

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

Последние заданные вопросы в категории Информатика

Задать вопрос