Home > Backend Development > C++ > body text

Print N rows of numbers so that the greatest common divisor between each pair of numbers is K

WBOY
Release: 2023-08-27 19:57:12
forward
1031 people have browsed it

GCD

GCD represents the greatest common divisor of two or more integers, excluding 0

For example, to find the greatest common divisor of 48 and 180

48 = 2 × 2 × 2 × 2 × 3

180 = 2 × 2 × 3 × 3 × 5

Print N rows of numbers so that the greatest common divisor between each pair of numbers is K

Greatest common divisor = 2 × 2 × 3 = 12.

In the given problem, N lines should be printed where the elements have the specified greatest common divisor

Input : N=2 GCD=2
Ouput : 2-4-6-10
14-16-18-22
Copy after login

Algorithm

START
Step 1 -> take input n(e.g. 2) and k(e.g. 2) as int values and i
Step 2-> Loop For i to 0 and i<n and i++
   Print (k * (6 * i + 1))
   Print (k * (6 * i + 2))
   Print (k * (6 * i +3))
   Print (k * (6 * i + 5))
   Print </p><p>
Step 3 -> end loop
STOP
Copy after login

The Chinese translation of Example

is:

Example

#include<stdio.h>
int main() {
   int i,n = 2, k = 2;
   for (i = 0; i < n; i++) {
      printf("%d-",(k * (6 * i + 1)));
      printf("%d-",(k * (6 * i + 2)));
      printf("%d-",(k * (6 * i + 3)));
      printf("%d",(k * (6 * i + 5)));
      printf("</p><p>");
   }
   return 0;
}
Copy after login

Output

If we run the above program, it will generate the following output.

2-4-6-10
14-16-18-22
Copy after login

The above is the detailed content of Print N rows of numbers so that the greatest common divisor between each pair of numbers is K. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template