# Java Program to Find Sum of First N Odd numbers and Even numbers

In this article, we are going to write a java program to find the sum of first n Odd and Even numbers. A number can be divided into two categories based on whether when it is divided by ‘2’ gives remainder ‘0’ or ‘1’. Odd numbers are the numbers which cannot be divided by ‘2’ or these numbers give remainder as 1 when they are divided by ‘2’. In other terms which can be written in the form of ‘2n+1’and Even numbers are numbers which can be divided by 2 i.e., these numbers give remainder as 0 when they are divided by ‘2’. In other terms which can be written in the form of ‘2n’

Examples

Input: n = 5
Output: Sum of first 5 Odd Numbers is: 25
Sum of first 5 Even Numbers is: 30


Explanation: As the Input is 5, the first 5 odd numbers are 1, 3,5,7,9 and sum = 1+3+5+7+9 =25, and the first 5 even numbers are 2,4,6,8,10 and sum = 2+4+6+8+10 = 30.

Input: n = 7
Output: Sum of first 5 Odd Numbers is: 49
Sum of first 5 Even Numbers is: 56


Explanation: As the Input is 5, the first 5 odd numbers are 1, 3,5,7,9,11,13 and sum = 1+3+5+7+9+11+13 = 49, and the first 5 even numbers are 2,4,6,8,10 ,12,14and sum = 2+4+6+8+10+12+14 = 56.

We will discuss two different approaches for finding the sum of n odd numbers and n even numbers.

## Approach 1: Using the for Loop

In this approach we will use the java for loop to find the sum of n odd numbers and n even numbers. We follow the below steps −

• Initialise an integer n.

• Using the for loop, keep on adding the odd numbers and even numbers until the condition becomes false and print the sum.

The for loop is an iterative statement in java which executes the code until the condition fails.

### Syntax

Below refers to the syntax of for- loop −

for (initialization; condition; updation) {
// code
}

• initialization − We need to initialize the loop with a value and it is executed only once.

• condition − We need to specify a condition which specifies how many times the loop will be executed.The loop will be executed until this condition is true.

• updation − We need to specify the value by which the loop should be incremented. It updates the loop initialization value.

### Example

In the below example, we use a for loop and it iterates from i = 1 to 2 * n so that we will increment by 2 and add the values to the oddSum variable and get sum of Odd numbers. Now, write another loop and this loop iterates from i = 2 to 2*n and we increment every time by 2 and add the values to the evenSum variable and get the sum of Even numbers. Thus, we print oddSum and evenSum finally

import java.util.*;
public class Main {
public static void main(String[] args) {
int n = 5;
int oddSum = 0;
for (int i = 1; i <= 2 * n; i += 2) {
oddSum += i;
}
int evenSum = 0;
for (int i = 2; i <= 2 * n; i += 2) {
evenSum += i;
}
System.out.println("Sum of first " + n + " Odd Numbers is: " + oddSum);
System.out.println("Sum of first " + n + " Even Numbers is: " + evenSum);
}
}


### Output

Sum of first 5 Odd Numbers is: 25
Sum of first 5 Even Numbers is: 30


Time Complexity: O(2*N) Auxiliary Space: O(1)

## Approach 2: Using Formulae

In this approach, we will find the sum of n even and odd numbers using the Arithmetic Progression formulae.

### Formulae

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


### Illustrations

Let us consider n = 4;
First 4 even numbers are 2,4,6,8.
Using formula ⇒  4(4+1) = 4*5= 20
So the sum of first four even numbers is 2+4+6+8 = 20.
First 4 odd numbers are 1,3,5,7.
So sum of first four  odd numbers is  1+3+5+7=16
Using formula ⇒  4*4=16


### Algorithm

• Initialize an integer n.

• Initialize two variables oddSum and evenSum with zero.

• Now, use formule and find oddSum and evenSum.

• Print oddSum and evenSum

### Example

In the below example, we use the formulae to find sum of first N even and odd numbers. We define two variables oddSum and evenSum and store values in these variables respectively. We finally print oddSum and evenSum.

import java.util.*;
public class Main {
public static void main(String[] args) {
int n = 5;
int oddSum = n * n;
int evenSum = n * (n + 1);
System.out.println("Sum of first " + n + " Odd Numbers: " + oddSum);
System.out.println("Sum of first " + n + " Even Numbers: " + evenSum);
}
}


### Output

Sum of first 5 Odd Numbers: 25
Sum of first 5 Even Numbers: 30


Time Complexity: O(1) Auxiliary Space: O(1)

Thus, in this article we have discussed different approaches of how to print Sum of first n Odd numbers and even numbers.

Updated on: 11-Apr-2023

1K+ Views