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

From AsIsWiki
(Difference between revisions)
Jump to: navigation, search
(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.print(" Введите число: ");
 
long n = in.nextLong();
 
  
 +
        System.out.println();
 +
        System.out.println(" Поиск простых чисел");
 
         System.out.println("---------------------");
 
         System.out.println("---------------------");
  
         System.out.printf(" Число %d - ", n);
+
         System.out.print(" Введите число: ");
          
+
         long n = in.nextLong();
long i = 2;
+
  
while ((i <= n / 2) && (n % i != 0)) {
+
        long i = 2;
    i++;
+
}
+
  
if (i <= n / 2) {
+
        while ((i <= n / 2) && (n % i != 0)) {
    System.out.print("не ");
+
            i++;
}
+
        }
  
System.out.println("простое");
+
        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;
   
 
    cout << "---------------------\n";
 
   
 
    printf(" Число %ld - ", n);
 
 
      
 
      
 
     long i = 2;
 
     long i = 2;
Line 73: Line 64:
 
     }
 
     }
 
      
 
      
     if (i <= n / 2) {
+
     string no = i <= n / 2 ? "не " : "";
        cout << "не ";
+
    }
+
 
      
 
      
     cout << "простое\n\n";
+
     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

 [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
26
27
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++

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
// 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

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

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
30
31
32
<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