Практикум по программированию. Основы. Циклы. Сумма квадратов чисел
From AsIsWiki
(Difference between revisions)
Line 66: | Line 66: | ||
return 0; | return 0; | ||
} | } | ||
+ | </source> | ||
+ | |||
+ | |||
+ | ==Python== | ||
+ | |||
+ | <source lang="cpp"> | ||
+ | # Python 3 | ||
+ | |||
+ | print('\n Сумма квадратов чисел от 1 до N') | ||
+ | print('---------------------------------') | ||
+ | |||
+ | n = int(input(' Введите N: ')) | ||
+ | |||
+ | s = 0 | ||
+ | for i in range(1, n + 1): | ||
+ | s += i * i | ||
+ | |||
+ | print('---------------------------------') | ||
+ | print(' S = %ld' % s) | ||
</source> | </source> | ||
Latest revision as of 08:42, 21 November 2017
Contents |
[edit] Java
import java.util.Scanner; public class Task01 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println(); System.out.println(" Сумма квадратов чисел от 1 до N"); System.out.println("---------------------------------"); System.out.print(" Введите N: "); long n = in.nextLong(); long s = 0; for (long i = 1; i <= n; i++) { s += i * i; } System.out.println("---------------------------------"); System.out.printf(" S = %d\n", s); } }
[edit] C++
// g++ 4.2 #include <iostream> using namespace std; int main() { long n; cout << "\n Сумма квадратов чисел от 1 до N\n"; cout << "---------------------------------\n"; cout << " Введите N: "; cin >> n; long s = 0; for (long i = 1; i <= n; i++) { s += i * i; } cout << "---------------------------------\n"; printf(" S = %ld\n\n", s); return 0; }
[edit] Python
# Python 3 print('\n Сумма квадратов чисел от 1 до N') print('---------------------------------') n = int(input(' Введите N: ')) s = 0 for i in range(1, n + 1): s += i * i print('---------------------------------') print(' S = %ld' % s)
[edit] Pascal
[edit] JavaScript
<html lang="ru"> <head> <meta charset="UTF-8"> <script> function calc() { var n = document.getElementById("nId").value; var s = 0; for (var i = 1; i <= n; i++) { s += i * i; } document.getElementById("resultId").innerHTML = "S = " + s; } </script> </head> <body> <p>Сумма квадратов чисел от 1 до N</p> <hr> <p>Введите N: <input id="nId" size="5"></p> <hr> <p id="resultId"></p> <button onclick="calc()">Рассчитать</button> </body> </html>