Практикум по программированию. Основы. Циклы. НОД и НОК
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(" двух натуральных чисел"); | ||
System.out.println("----------------------------------"); | System.out.println("----------------------------------"); | ||
− | + | System.out.print(" Введите два натуральных числа: "); | |
+ | long n1 = in.nextLong(); | ||
+ | long n2 = in.nextLong(); | ||
− | + | long a = n1, b = n2; | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | while (a != b) { | |
− | + | if (a > b) { | |
+ | a = a - b; | ||
+ | } else { | ||
+ | b = b - a; | ||
+ | } | ||
+ | } | ||
− | + | long nod = a; | |
− | + | long nok = n1 * n2 / nod; | |
+ | |||
+ | System.out.println("----------------------------------"); | ||
+ | System.out.printf(" НОД(%d, %d) = %d\n", n1, n2, nod); | ||
+ | System.out.printf(" НОК(%d, %d) = %d\n", n1, n2, nok); | ||
} | } | ||
} | } | ||
Line 69: | Line 68: | ||
cout << " Введите два натуральных числа: "; | cout << " Введите два натуральных числа: "; | ||
cin >> n1 >> n2; | cin >> n1 >> n2; | ||
− | |||
− | |||
long a = n1, b = n2; | long a = n1, b = n2; | ||
Line 84: | Line 81: | ||
long nod = a; | long nod = a; | ||
long nok = n1 * n2 / nod; | long nok = n1 * n2 / nod; | ||
− | + | ||
+ | cout << "----------------------------------\n"; | ||
printf(" НОД(%ld, %ld) = %ld\n", n1, n2, nod); | printf(" НОД(%ld, %ld) = %ld\n", n1, n2, nod); | ||
printf(" НОК(%ld, %ld) = %ld\n\n", n1, n2, nok); | printf(" НОК(%ld, %ld) = %ld\n\n", n1, n2, nok); |
Revision as of 11:07, 22 March 2016
Contents |
Java
import java.util.Scanner; public class Task07 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println(); System.out.println(" Наибольший общий делитель (НОД)"); System.out.println(" и наименьшее общее кратное (НОК)"); System.out.println(" двух натуральных чисел"); System.out.println("----------------------------------"); System.out.print(" Введите два натуральных числа: "); long n1 = in.nextLong(); long n2 = in.nextLong(); long a = n1, b = n2; while (a != b) { if (a > b) { a = a - b; } else { b = b - a; } } long nod = a; long nok = n1 * n2 / nod; System.out.println("----------------------------------"); System.out.printf(" НОД(%d, %d) = %d\n", n1, n2, nod); System.out.printf(" НОК(%d, %d) = %d\n", n1, n2, nok); } }
C++
// g++ 4.2 #include <iostream> using namespace std; int main() { long n1, n2; cout << "\n Наибольший общий делитель (НОД)\n"; cout << " и наименьшее общее кратное (НОК)\n"; cout << " двух натуральных чисел\n"; cout << "----------------------------------\n"; cout << " Введите два натуральных числа: "; cin >> n1 >> n2; long a = n1, b = n2; while (a != b) { if (a > b) { a = a - b; } else { b = b - a; } } long nod = a; long nok = n1 * n2 / nod; cout << "----------------------------------\n"; printf(" НОД(%ld, %ld) = %ld\n", n1, n2, nod); printf(" НОК(%ld, %ld) = %ld\n\n", n1, n2, nok); return 0; }
Pascal
JavaScript