1. Write a program to list all even numbers less than or equal to the number n. Take the value of n as input from user
Answers
Answer:
Explanation:
class JavaExample {
public static void main(String args[]) {
int n = 100;
System.out.print("Even Numbers from 1 to "+n+" are: ");
for (int i = 1; i <= n; i++) {
//if number%2 == 0 it means its an even number
if (i % 2 == 0) {
System.out.print(i + " ");
}
}
}
}
//Java program to print EVEN numbers from 1 to N.
Explanation:
//Java program to print EVEN numbers from 1 to N.
import java.util.*;
public class Even{
public static void main(String []args)
{
int n=0,i=0;
Scanner X = new Scanner(System.in);
System.out.print("Enter value n : ");
n = X.nextInt();
for(i=1; i<n; i++)
{
if(i%2==0)
System.out.print(i+" ");
}
System.out.println();
}
}