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

From AsIsWiki
(Difference between revisions)
Jump to: navigation, search
(Created page with "<div style='max-width:700px;text-align:justify;'> Циклы __FORCETOC__ ==Java== <p...")
 
 
(6 intermediate revisions by one user not shown)
Line 1: Line 1:
 
<div style='max-width:700px;text-align:justify;'>
 
<div style='max-width:700px;text-align:justify;'>
[[Практикум по программированию. Основы. Циклы|Циклы]]
+
[[Практикум по программированию. Основы. Циклы|Задачи]] ·
 +
[[Практикум по программированию. Основы. Циклы. Факториал|Дальше]]
  
 
__FORCETOC__
 
__FORCETOC__
Line 6: Line 7:
 
==Java==
 
==Java==
  
<pre>
+
<source lang="java">
 
import java.util.Scanner;
 
import java.util.Scanner;
  
Line 12: Line 13:
  
 
     public static void main(String[] args) {
 
     public static void main(String[] args) {
       
+
 
 
         Scanner in = new Scanner(System.in);
 
         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();
 
  
 +
        System.out.println();
 +
        System.out.println(" Сумма квадратов чисел от 1 до N");
 
         System.out.println("---------------------------------");
 
         System.out.println("---------------------------------");
 +
 +
        System.out.print(" Введите N: ");
 +
        long n = in.nextLong();
 
          
 
          
long s = 0;
+
        long s = 0;
  
for (long i = 1; i <= n; i++) {
+
        for (long i = 1; i <= n; i++) {
    s += i * i;
+
            s += i * i;
}
+
        }
  
System.out.printf(" S = %d\n", s);
+
        System.out.println("---------------------------------");
 +
        System.out.printf(" S = %d\n", s);
 
     }
 
     }
 
}
 
}
</pre>
+
</source>
  
  
 
==C++==
 
==C++==
  
<pre>
+
<source lang="cpp">
</pre>
+
// 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;
 +
}
 +
</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>
  
  
 
==Pascal==
 
==Pascal==
  
<pre>
+
<source lang="delphi">
</pre>
+
</source>
 +
 
 +
 
 +
==JavaScript==
 +
 
 +
<source lang="js">
 +
<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>
 +
</source>
  
  
 
----
 
----
[[Практикум по программированию. Основы. Циклы|Циклы]]
+
[[Практикум по программированию. Основы. Циклы|Задачи]] ·
 +
[[Практикум по программированию. Основы. Циклы. Факториал|Дальше]]
 
</div>
 
</div>

Latest revision as of 08:42, 21 November 2017

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


Contents

 [hide

[edit] Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// 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

1
2
3
4
5
6
7
8
9
10
11
12
13
# 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

1
 


[edit] JavaScript

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<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>



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

Personal tools
Namespaces

Variants
Actions
Navigation
Tools