program to print n even numbers with comma seperator
input5
output24.6.8.10
Sample input 1
Copy
Sample output 1
5
246.8.10
Answers
Answered by
0
Answer:
for(int i=0;i<=n*2;i++){
if(i%2==0){
System.out.println(i+",");
}
Explanation:
Multiply the input with 2 then put inside a for loop check the condition for even and print it
Answered by
1
Question:-
Write a program to print n even numbers with comma as separator.
Program:
Here is your program in Java.
import java.util.*;
class Even
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("How many even numbers? ");
int n=sc.nextInt();
int a=2;
for(int i=1;i<=n;i++,a+=2)
{
System.out.print(a);
if(i!=n)
System.out.print(", ");
}
}
}
Output:-
How many numbers? 5
2,4,6,8,10
Similar questions