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