Практикум по программированию. Основы. Циклы. Факториал
From AsIsWiki
Contents |
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); } }
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; }
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))
Pascal
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>