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 100In 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 100Related Java Examples1. Java Program to print odd numbers from 1 to 100
The program should work as follow: Please type in a number: 5 1 5 2 4 3My 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: MultithreadingExamples:
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:
Below is the implementation of the above approach:
Output: 1 2 3 4 5 6 7 8 9 10
Article Tags :
Given a number n. The problem is to find the sum of first n even numbers. Naive Approach: Iterate through the first n even numbers and add them.
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. Proof:
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.
Output Sum of first 20 Even numbers is: 420 Time Complexity: O(1). Auxiliary Space: O(1) since using constant variables Article Tags : |