Практикум по программированию. Основы. Циклы. Поиск элементов ряда
From AsIsWiki
(Difference between revisions)
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(" 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 y = 0; | |
− | + | int i = 0; | |
− | + | ||
− | + | ||
− | + | while (y <= x) { | |
− | + | y += 1.0 / ++i; | |
− | + | } | |
− | + | System.out.println("-------------------------------"); | |
+ | System.out.printf(" Y = %.12f\n", y); | ||
} | } | ||
} | } | ||
Line 59: | Line 58: | ||
cout << " Введите границу поиска: "; | cout << " Введите границу поиска: "; | ||
cin >> x; | cin >> x; | ||
− | |||
− | |||
double y = 0; | double y = 0; | ||
Line 69: | Line 66: | ||
} | } | ||
+ | cout << "-------------------------------\n"; | ||
printf(" Y = %.12f\n\n", y); | printf(" Y = %.12f\n\n", y); | ||
Revision as of 11:51, 21 March 2016
Contents |
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); } }
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; }
Pascal
JavaScript