print consecutive numbers in java

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

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