print consecutive numbers

Solutions on MaxInterview for print consecutive numbers by the best coders in the world

showing results for - "print consecutive numbers"
Emilio
18 Jan 2019
1Numbers -- print consecutive numbers
2Write a function:
3that, given a positive integer N, prints the consecutive numbers from 1 to N,
4each on a separate line. However, any number divisible by 2, 3 or 5 should
5be replaced by the word Codility, Test or Coders respectively. If a number
6is divisible by more than one of the numbers: 2,3 or 5, it should be
7replaced by a concatenation of the respective words Codility, Test and
8Coders in this given order. For example, numbers divisible by both 2 and
93 should be replacée by CodilityTest and numbers divisible by all three
10numbers: 2,3 and 5, should be replaced by CodilityTestCoders.
11
12
13public static void solution(int N) {
14String result = "";
15for (int i = 1; i <= N; i++) {
16if(i %2 ==0 && i%3 == 0 && i %5==0)
17result += "CodilityTestCoders\n";
18else if(i %2 ==0 && i%3 == 0)
19result += "CodilityTest\n";
20else if(i % 2==0 && i %5==0)
21result += "CodilityCoders\n";
22else if(i % 3 == 0 && i % 5 ==0)
23result +="TestCoders\n";
24else if(i % 2 ==0)
25result += "Codility\n";
26else if (i % 5 == 0)
27result += "Coders\n";
28else if (i % 3 == 0)
29result += "Test\n";
30else
31result += i + "\n";
32}
33 
34System.out.println(result);
35}