Практикум по программированию. Основы. Циклы. Факториал

From AsIsWiki
(Difference between revisions)
Jump to: navigation, search
 
Line 67: Line 67:
 
     return 0;
 
     return 0;
 
}
 
}
 +
</source>
 +
 +
 +
==Python==
 +
 +
<source lang="cpp">
 +
# Python 3
 +
 +
print('\n Расчет факториала')
 +
print('-------------------')
 +
 +
n = int(input(' Введите число: '))
 +
 +
p = 1
 +
for i in range(1, n + 1):
 +
    p *= i
 +
 +
print('-------------------')
 +
print(' %ld! = %ld' % (n, p))
 
</source>
 
</source>
  

Latest revision as of 08:44, 21 November 2017

Назад · Задачи · Дальше


Contents

[edit] Java

import java.util.Scanner;

public class Task02 {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        System.out.println();
        System.out.println(" Расчет факториала");
        System.out.println("-------------------");

        System.out.print(" Введите число: ");
        long n = in.nextLong();

        long p = 1;

        for (long i = 1; i <= n; i++) {
            p *= i;
        }

        System.out.println("-------------------");
        System.out.printf(" %d! = %d\n", n, p);
    }
}


[edit] C++

// g++ 4.2

#include <iostream>

using namespace std;

int main() {
    
    long n;
    
    cout << "\n Расчет факториала\n";
    cout << "-------------------\n";
    
    cout << " Введите число: ";
    cin >> n;
    
    long p = 1;
    
    for (long i = 1; i <= n; i++) {
        p *= i;
    }

    cout << "-------------------\n";
    printf(" %ld! = %ld\n\n", n, p);
    
    return 0;
}


[edit] Python

# Python 3

print('\n Расчет факториала')
print('-------------------')

n = int(input(' Введите число: '))

p = 1
for i in range(1, n + 1):
    p *= i

print('-------------------')
print(' %ld! = %ld' % (n, p))


[edit] Pascal



[edit] JavaScript

<html lang="ru">
<head>
    <meta charset="UTF-8">
    <script>
        function calc() {
            var n = document.getElementById("nId").value;

            var p = 1;

            for (var i = 1; i <= n; i++) {
                p *= i;
            }

            document.getElementById("resultId").innerHTML = n + "! = " + p;
        }
    </script>
</head>
<body>

<p>Расчет факториала</p>
<hr>
<p>Введите число: <input id="nId" size="5"></p>
<hr>
<p id="resultId"></p>

<button onclick="calc()">Рассчитать</button>

</body>
</html>



Назад · Задачи · Дальше

Personal tools
Namespaces

Variants
Actions
Navigation
Tools