Wednesday, October 11, 2017

First Five Values Divisible

Three numbers A, B and C are passed as input. The program must print the first five values that are divisible by A, B and C.

Input Format:
The first line denotes the value of A. The second line denotes the value of B. The third line denotes the value of C.

Output Format:
The first line contains the first five numbers divisible by A, B and C (with each value separated by a space).

Boundary Conditions: 1 <= A <= 9999 1 <= B <= 9999 1 <= C <= 9999

Example Input/Output 1:
Input:
2
3
4
Output:
12 24 36 48 60

Example Input/Output 2:
Input:
4
6
8
Output:
24 48 72 96 120

Solution 1:
import fractions
x,y,z=int(input()),int(input()),int(input())
l=int(x*y/fractions.gcd(x,y))
l=int(l*z/fractions.gcd(l,z))
for i in range(1,6):
    print(i*l,end=' ')

Solution 2:
#include <bits/stdc++.h>
using namespace std;
int main()
{
 int i,x,y,z,l;
 cin>>x>>y>>z;
 l=x*y/__gcd(x,y);
 l=l*z/__gcd(l,z);
 for(i=1;i<6;i++)
 cout<<i*l<<" ";
}
Share: