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

From AsIsWiki
(Difference between revisions)
Jump to: navigation, search
(Python)
 
(6 intermediate revisions by one user not shown)
Line 8: Line 8:
 
==Java==
 
==Java==
  
<pre>
+
<source lang="java">
 
import java.util.Scanner;
 
import java.util.Scanner;
  
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.println("---------------------");
 
         System.out.println("---------------------");
  
         System.out.print(" Введите число: ");  
+
         System.out.print(" Введите число: ");
long n = in.nextLong();
+
        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.println("---------------------");
 +
        System.out.printf(" Число %d - %sпростое\n", n, no);
 +
    }
 +
}
 +
</source>
  
        System.out.printf(" Число %d - ", n);
 
       
 
long i = 2;
 
  
while ((i <= n / 2) && (n % i != 0)) {
+
==C++==
    i++;
+
}
+
  
if (i <= n / 2) {
+
<source lang="cpp">
    System.out.print("не ");
+
// g++ 4.2
}
+
  
System.out.println("простое");
+
#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;
 
}
 
}
</pre>
+
</source>
  
  
==C++==
+
==Python==
  
<pre>
+
<source lang="cpp">
</pre>
+
# 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==
  
<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 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>
  
  

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>



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

Personal tools
Namespaces

Variants
Actions
Navigation
Tools