Write two versions of a program that reads a positive integer n and prints the first n even numbers

In this tutorial, we will write a Java program to display even numbers from 1 to n which means if the value of n is 100 then this program will display the even values between 1 to 100.

Program to display even numbers from 1 to n where n is 100

In the following example we are displaying the even numbers from 1 to n, the value of n we have set here is 100 so basically this program will print the even numbers between 1 to 100.

If an integer number(never a fraction number) is exactly divisible by 2 which means it yields no remainder when divided by 2 then it is an even number. This same logic we are using here to find the even numbers. We are looping through 1 to n and checking each value whether it is evenly divisible by 2 or not, if it is then we are displaying it. To understand this program you should have the basic knowledge of for loop in Java and if statement.

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 + " "); } } } }

Output:

Even Numbers from 1 to 100 are: 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100

Related Java Examples

1. Java Program to print odd numbers from 1 to 100
2. Java program to check even or odd number
3. Java program to check if a given number is perfect square
4. Java Program to find GCD of two numbers

The program should work as follow:

Please type in a number: 5 1 5 2 4 3

My code doesn't do the same. I think there is should be the 2nd loop, but I don't really understand how can I do it. Could you possibly give me a hint or advice to solve this task. Thanks. My code looks like this:

num = int(input("Please type in a number:")) n=0 while num>n: a = num%10 num -= a num = num/10 print(a) n = n + 1 print(n)

2

Given an integer N, the task is to write Java Program to print the first N natural numbers in increasing order using two threads.

Prerequisite: Multithreading

Examples:

Input: N = 10
Output: 1 2 3 4 5 6 7 8 9 10

Input: N = 18
Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The idea is to create two threads and print even numbers with one thread and odd numbers with another thread. Below are the steps:

  • Create two threads T1 and T2 using the below syntax, where T1 and T2 are used to print odd and even numbers respectively.

Thread T1 = new Thread(new Runnable() {           public void run() { mt.printEvenNumber(); }

});

Thread T2 = new Thread(new Runnable() {           public void run() { mt.printOddNumber(); }

});

where, printOddNumber() is used to print all the odd numbers till N, printEvenNumber() is used to print all the even numbers till N.

  • Maintain a global counter variable and start both threads using the below function:

T1.start(); T2.start();

  • If the counter is even in the Thread T1, then wait for the thread T2 to print that even number. Otherwise, print that odd number, increment the counter and notify to the Thread T2 using the function notify().
  • If the counter is odd in the Thread T2, then wait for the thread T1 to print that odd number. Otherwise, print that even number, increment the counter and notify the Thread T1 using the function notify().

Below is the implementation of the above approach:

    public void printOddNumber()

                while (counter % 2 == 0) {

                        InterruptedException e) {

                System.out.print(counter + " ");

    public void printEvenNumber()

                while (counter % 2 == 1) {

                        InterruptedException e) {

    public static void main(String[] args)

        Thread t1 = new Thread(new Runnable() {

        Thread t2 = new Thread(new Runnable() {

Output: 1 2 3 4 5 6 7 8 9 10

Time Complexity: O(N) 

Auxiliary Space: O(1)


Article Tags :

Given a number n. The problem is to find the sum of first n even numbers.
Examples: 
 

Input : n = 4 Output : 20 Sum of first 4 even numbers = (2 + 4 + 6 + 8) = 20 Input : n = 20 Output : 420

Naive Approach: Iterate through the first n even numbers and add them.
 

    for (int i = 1; i <= n; i++) {

    cout << "Sum of first " << n

         << " Even numbers is: " << evenSum(n);

    static int evenSum(int n)

        for (int i = 1; i <= n; i++) {

    public static void main(String argc[])

        System.out.println("Sum of first " + n +

print("sum of first ", n, "even number is: ",

    static int evenSum(int n)

        for (int i = 1; i <= n; i++) {

    public static void Main()

        Console.WriteLine("Sum of first " + n

         + " Even numbers is: " + evenSum(n));

    for ($i = 1; $i <= $n; $i++) {

    echo "Sum of first ".$n." Even numbers is: ".evenSum($n);

        for (let i = 1; i <= n; i++) {

    document.write("Sum of first " + n +

    " Even numbers is: " + evenSum(n));

Output Sum of first 20 Even numbers is: 420

Time Complexity: O(n)

Auxiliary Space: O(1)

Efficient Approach: By applying the formula given below. 
 

Sum of first n even numbers = n * (n + 1).

Proof: 
 

Sum of first n terms of an A.P.(Arithmetic Progression) = (n/2) * [2*a + (n-1)*d].....(i) where, a is the first term of the series and d is the difference between the adjacent terms of the series. Here, a = 2, d = 2, applying these values to eq.(i), we get Sum = (n/2) * [2*2 + (n-1)*2] = (n/2) * [4 + 2*n - 2] = (n/2) * (2*n + 2) = n * (n + 1)

    cout << "Sum of first " << n

         << " Even numbers is: " << evenSum(n);

    static int evenSum(int n)

    public static void main(String argc[])

        System.out.println("Sum of first " + n +

print("sum of first", n, "even number is: ",

    static int evenSum(int n)

    public static void Main()

        Console.WriteLine("Sum of first " + n

        + " Even numbers is: " + evenSum(n));

echo "Sum of first " , $n,

document.write("Sum of first " + n +

Output Sum of first 20 Even numbers is: 420

Time Complexity: O(1).

Space Complexity: O(1) since using constant variables

Another method:

In this method, we have to calculate the Nth term,

The formula for finding Nth term ,Tn = a+(n-1)d, here, a= first term, d= common difference, n= number of term

And then we have to apply the formula for finding the sum, 

the formula is, Sn=(N/2) * (a + Tn), here a= first term, Tn= last term, n= number of term

This formula also can be applied for the sum of odd numbers, but the series must have a same common difference.

    cout << "Sum of first " << n

         << " Even numbers is: " << evenSum(n);

  public static int evenSum(int n)

  public static void main(String[] args)

    System.out.println("Sum of first "+n+" Even numbers is: "+evenSum(n));

    return (int)(n/2) * (2 + tn);

if __name__ == "__main__" :

    print("Sum of first", n ,"Even numbers is:", evenSum(n));

    static int evenSum(int n)

    public static void Main()

        Console.Write("Sum of first "+n+" Even numbers is: "+evenSum(n));

    document.write("Sum of first "+n+" Even numbers is: "+evenSum(n));

Output Sum of first 20 Even numbers is: 420

Time Complexity: O(1).

Auxiliary Space: O(1) since using constant variables
 


Article Tags :