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

From AsIsWiki
(Difference between revisions)
Jump to: navigation, search
 
(4 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(" 1, 1 + 1/2, 1 + 1/2 + 1/3 ...");
 
         System.out.println(" 1, 1 + 1/2, 1 + 1/2 + 1/3 ...");
 
         System.out.println("-------------------------------");
 
         System.out.println("-------------------------------");
  
         System.out.print(" Введите границу поиска: ");  
+
         System.out.print(" Введите границу поиска: ");
double x = in.nextDouble();
+
        double x = in.nextDouble();
  
         System.out.println("-------------------------------");
+
         double y = 0;
       
+
        int i = 0;
double y = 0;
+
int i = 0;
+
  
while (y <= x) {
+
        while (y <= x) {
    y += 1.0 / ++i;
+
            y += 1.0 / ++i;
}
+
        }
  
System.out.printf(" Y = %.12f\n", y);
+
        System.out.println("-------------------------------");
 +
        System.out.printf(" Y = %.12f\n", y);
 
     }
 
     }
 
}
 
}
</pre>
+
</source>
  
  
 
==C++==
 
==C++==
  
<pre>
+
<source lang="cpp">
</pre>
+
// g++ 4.2
 +
 
 +
#include <iostream>
 +
 
 +
using namespace std;
 +
 
 +
int main() {
 +
   
 +
    double x;
 +
   
 +
    cout << "\n Поиск элементов ряда:\n";
 +
    cout << " 1, 1 + 1/2, 1 + 1/2 + 1/3 ...\n";
 +
    cout << "-------------------------------\n";
 +
   
 +
    cout << " Введите границу поиска: ";
 +
    cin >> x;
 +
   
 +
    double y = 0;
 +
    int i = 0;
 +
   
 +
    while (y <= x) {
 +
        y += 1.0 / ++i;
 +
    }
 +
   
 +
    cout << "-------------------------------\n";
 +
    printf(" Y = %.12f\n\n", y);
 +
   
 +
    return 0;
 +
}
 +
</source>
 +
 
 +
 
 +
==Python==
 +
 
 +
<source lang="cpp">
 +
# Python 3
 +
 
 +
print('\n Поиск элементов ряда:')
 +
print(' 1, 1 + 1/2, 1 + 1/2 + 1/3 ...')
 +
print('-------------------------------')
 +
 
 +
x = float(input(' Введите границу поиска: '))
 +
 
 +
y = 0; i = 1
 +
while y <= x:
 +
    y += 1 / i
 +
    i += 1
 +
 
 +
print('-------------------------------')
 +
print(' Y = %.12f' % y)
 +
</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 x = document.getElementById("xId").value;
 +
 
 +
            var y = 0, i = 0;
 +
 
 +
            while (y <= x) {
 +
                y += 1 / ++i;
 +
            }
 +
 
 +
            document.getElementById("resultId").innerHTML = "Y = " + y.toFixed(12);
 +
        }
 +
    </script>
 +
</head>
 +
<body>
 +
 
 +
<p>Поиск элементов ряда: 1, 1 + 1/2, 1 + 1/2 + 1/3 ...</p>
 +
<hr>
 +
<p>Введите границу поиска: <input id="xId" size="5"></p>
 +
<hr>
 +
<p id="resultId"></p>
 +
 
 +
<button onclick="calc()">Рассчитать</button>
 +
 
 +
</body>
 +
</html>
 +
</source>
  
  

Latest revision as of 08:46, 21 November 2017

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


Contents

[edit] Java

import java.util.Scanner;

public class Task03 {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        System.out.println();
        System.out.println(" Поиск элементов ряда:");
        System.out.println(" 1, 1 + 1/2, 1 + 1/2 + 1/3 ...");
        System.out.println("-------------------------------");

        System.out.print(" Введите границу поиска: ");
        double x = in.nextDouble();

        double y = 0;
        int i = 0;

        while (y <= x) {
            y += 1.0 / ++i;
        }

        System.out.println("-------------------------------");
        System.out.printf(" Y = %.12f\n", y);
    }
}


[edit] C++

// g++ 4.2

#include <iostream>

using namespace std;

int main() {
    
    double x;
    
    cout << "\n Поиск элементов ряда:\n";
    cout << " 1, 1 + 1/2, 1 + 1/2 + 1/3 ...\n";
    cout << "-------------------------------\n";
    
    cout << " Введите границу поиска: ";
    cin >> x;
    
    double y = 0;
    int i = 0;
    
    while (y <= x) {
        y += 1.0 / ++i;
    }
    
    cout << "-------------------------------\n";
    printf(" Y = %.12f\n\n", y);
    
    return 0;
}


[edit] Python

# Python 3

print('\n Поиск элементов ряда:')
print(' 1, 1 + 1/2, 1 + 1/2 + 1/3 ...')
print('-------------------------------')

x = float(input(' Введите границу поиска: '))

y = 0; i = 1
while y <= x:
    y += 1 / i
    i += 1

print('-------------------------------')
print(' Y = %.12f' % y)


[edit] Pascal



[edit] JavaScript

<html lang="ru">
<head>
    <meta charset="UTF-8">
    <script>
        function calc() {
            var x = document.getElementById("xId").value;

            var y = 0, i = 0;

            while (y <= x) {
                y += 1 / ++i;
            }

            document.getElementById("resultId").innerHTML = "Y = " + y.toFixed(12);
        }
    </script>
</head>
<body>

<p>Поиск элементов ряда: 1, 1 + 1/2, 1 + 1/2 + 1/3 ...</p>
<hr>
<p>Введите границу поиска: <input id="xId" size="5"></p>
<hr>
<p id="resultId"></p>

<button onclick="calc()">Рассчитать</button>

</body>
</html>



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

Personal tools
Namespaces

Variants
Actions
Navigation
Tools