Практикум по программированию. Основы. Циклы. Простые числа
From AsIsWiki
(Difference between revisions)
(→Python) |
|||
(4 intermediate revisions by one user not shown) | |||
Line 14: | Line 14: | ||
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("---------------------"); | System.out.println("---------------------"); | ||
− | System.out. | + | System.out.print(" Введите число: "); |
− | + | long n = in.nextLong(); | |
− | + | ||
− | + | long i = 2; | |
− | + | ||
− | + | ||
− | + | while ((i <= n / 2) && (n % i != 0)) { | |
− | + | i++; | |
− | + | } | |
− | + | String no = i <= n / 2 ? "не " : ""; | |
+ | |||
+ | System.out.println("---------------------"); | ||
+ | System.out.printf(" Число %d - %sпростое\n", n, no); | ||
} | } | ||
} | } | ||
Line 62: | Line 57: | ||
cout << " Введите число: "; | cout << " Введите число: "; | ||
cin >> n; | cin >> n; | ||
− | |||
− | |||
− | |||
− | |||
long i = 2; | long i = 2; | ||
Line 73: | Line 64: | ||
} | } | ||
− | + | string no = i <= n / 2 ? "не " : ""; | |
− | + | ||
− | + | ||
− | cout << " | + | cout << "---------------------\n"; |
+ | printf(" Число %ld - %sпростое\n\n", n, no.data()); | ||
return 0; | return 0; | ||
Line 83: | Line 73: | ||
</source> | </source> | ||
+ | |||
+ | ==Python== | ||
+ | |||
+ | <source lang="cpp"> | ||
+ | # Python 3 | ||
+ | |||
+ | print('\n Поиск простых чисел') | ||
+ | print('---------------------') | ||
+ | |||
+ | n = int(input(' Введите число: ')) | ||
+ | |||
+ | i = 2 | ||
+ | while (i <= n / 2) and (n % i != 0): | ||
+ | i += 1 | ||
+ | |||
+ | no = "не " if i <= n / 2 else "" | ||
+ | |||
+ | print('---------------------') | ||
+ | print(' Число %ld - %sпростое' % (n, no)) | ||
+ | </source> | ||
==Pascal== | ==Pascal== | ||
Line 93: | Line 103: | ||
<source lang="js"> | <source lang="js"> | ||
+ | <html lang="ru"> | ||
+ | <head> | ||
+ | <meta charset="UTF-8"> | ||
+ | <script> | ||
+ | function calc() { | ||
+ | var n = document.getElementById("nId").value; | ||
+ | |||
+ | var i = 2; | ||
+ | |||
+ | while ((i <= n / 2) && (n % i != 0)) { | ||
+ | i++; | ||
+ | } | ||
+ | |||
+ | var no = i <= n / 2 ? "не " : ""; | ||
+ | |||
+ | document.getElementById("resultId").innerHTML = | ||
+ | " Число " + n + " - " + no + "простое"; | ||
+ | } | ||
+ | </script> | ||
+ | </head> | ||
+ | <body> | ||
+ | |||
+ | <p>Поиск простых чисел</p> | ||
+ | <hr> | ||
+ | <p>Введите число: <input id="nId" size="5"></p> | ||
+ | <hr> | ||
+ | <p id="resultId"></p> | ||
+ | |||
+ | <button onclick="calc()">Рассчитать</button> | ||
+ | |||
+ | </body> | ||
+ | </html> | ||
</source> | </source> | ||
Latest revision as of 09:24, 23 November 2017
Contents |
[edit] Java
import java.util.Scanner; public class Task06 { 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 i = 2; while ((i <= n / 2) && (n % i != 0)) { i++; } String no = i <= n / 2 ? "не " : ""; System.out.println("---------------------"); System.out.printf(" Число %d - %sпростое\n", n, no); } }
[edit] C++
// g++ 4.2 #include <iostream> using namespace std; int main() { long n; cout << "\n Поиск простых чисел\n"; cout << "---------------------\n"; cout << " Введите число: "; cin >> n; long i = 2; while ((i <= n / 2) && (n % i != 0)) { i++; } string no = i <= n / 2 ? "не " : ""; cout << "---------------------\n"; printf(" Число %ld - %sпростое\n\n", n, no.data()); return 0; }
[edit] Python
# Python 3 print('\n Поиск простых чисел') print('---------------------') n = int(input(' Введите число: ')) i = 2 while (i <= n / 2) and (n % i != 0): i += 1 no = "не " if i <= n / 2 else "" print('---------------------') print(' Число %ld - %sпростое' % (n, no))
[edit] Pascal
[edit] JavaScript
<html lang="ru"> <head> <meta charset="UTF-8"> <script> function calc() { var n = document.getElementById("nId").value; var i = 2; while ((i <= n / 2) && (n % i != 0)) { i++; } var no = i <= n / 2 ? "не " : ""; document.getElementById("resultId").innerHTML = " Число " + n + " - " + no + "простое"; } </script> </head> <body> <p>Поиск простых чисел</p> <hr> <p>Введите число: <input id="nId" size="5"></p> <hr> <p id="resultId"></p> <button onclick="calc()">Рассчитать</button> </body> </html>