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

From AsIsWiki
(Difference between revisions)
Jump to: navigation, search
 
(3 intermediate revisions by one user not shown)
Line 13: 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();
         System.out.println(" Сумма квадратов чисел от 1 до N ");
+
         System.out.println(" Сумма квадратов чисел от 1 до N");
 
         System.out.println("---------------------------------");
 
         System.out.println("---------------------------------");
  
         System.out.print(" Введите N: ");  
+
         System.out.print(" Введите N: ");
long n = in.nextLong();
+
        long n = in.nextLong();
 
+
        System.out.println("---------------------------------");
+
 
          
 
          
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);
 
     }
 
     }
 
}
 
}
Line 55: Line 54:
 
     cout << " Введите N: ";
 
     cout << " Введите N: ";
 
     cin >> n;
 
     cin >> n;
   
 
    cout << "---------------------------------\n";
 
 
      
 
      
 
     long s = 0;
 
     long s = 0;
Line 63: Line 60:
 
         s += i * i;
 
         s += i * i;
 
     }
 
     }
      
+
 
 +
     cout << "---------------------------------\n";
 
     printf(" S = %ld\n\n", s);
 
     printf(" S = %ld\n\n", s);
 
      
 
      
 
     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>
  
Line 80: Line 97:
  
 
<source lang="js">
 
<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>
 
</source>
  

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