Program to print Even Numbers till "n" numbers (Java)

So today we are going to write program that prints all the even number till n numbers. So this is a very simple program and it does not require much logic to write this program, as even numbers are those which are divisible by 2 that's it.
So lets get started...
Q. Write a program to print all Even Number till n numbers. The integer n is to be entered by the user.
[Any integer that can be divided exactly by 2 is an Even Number.]
For Example: 2, 4, 6, 8, 10, etc...

Solution:
 import java.util.*;
 class PrintEvenNumbers
 {
     public static void main(String[] args)
     {
         Scanner sc = new Scanner(System.in);
         System.out.println("Enter a number: ");
         int n = sc.nextInt();
         System.out.println("Even Numbers are: ");
         for(int i = 1;i <= n;i++)
         {
             if(i%2 == 0)
             {
                 System.out.println(i);
             }
         }
     }
 }

Code Written in BlueJ

Output:
Enter a number: 
10
Even Numbers are: 
2
4
6
8
10

Output in BlueJ

Variable description used in program:
int n - It is used to accept "n" number from the user.

So that is it for the print Even Number till "n" numbers program. Hope you find it useful and if you have any query do comment in the comment box and I will try to solve your query. Will see you on next program.

Thank You.
Program to print Even Numbers till "n" numbers (Java) Program to print Even Numbers till "n" numbers (Java) Reviewed by Get2Know on September 23, 2020 Rating: 5

No comments:

Business

Powered by Blogger.