Write a program to Print all even number.
Take x and y as input. Write a function that takes in x and y as integer parameters and prints all the even numbers between x and y (x and y both inclusive)
Input Format
first line take an integer input from user as x.
second line take an integer input from user as y.
Constraints
1<=x<=1000
1<=y<=10^4
Output Format
Print all even number between given intervals.
Sample Input 0
1
10
Sample Output 0
2 4 6 8 10
Explanation 0
Print all even numbers between 1 and 10
ALSO READ: Print nth Tribonacci number
Using Functions
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
static int printEven(int x, int y){
int i=x;
while(i<=y) {
if(i%2==0)
System.out.print(i+" ");
i++;
}
return i;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
int y=sc.nextInt();
printEven(x, y);
}
}
Without using functions
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
int y=sc.nextInt();
for(int i=x; i<=y; i++) {
if(i%2==0)
System.out.print(i+" ");
}
}
}