170 Views

write a java code on sum of even numbers


arukondavarsha 8th Nov, 2021
Answer (1)
piyushkurwade 8th Nov, 2021

Hi,

The code for sum of even numbers in java is given below:

import java.util.*;

import java.lang.*;

public class Jtp{

static int fun(int n)

{

int i, sum = 0;

for (i = 2; i <= n; i+=2) {

sum += i;

}

return sum;

}

public static void main(String argc[])

{

System.out.println("Enter any number: ");

Scanner sc = new Scanner(System.in);

int n = sc.nextInt();

System.out.println("sum of all even numbers from 1 to " + n + " is :" + fun(n));

}

}

Hope it helps.

Related Questions

CLAT Current Affairs with GK ...
Apply
Stay updated with current affairs & check your preparation with the CLAT General Knowledge Mock Tests Ebook
CLAT English Language Mock Tests
Apply
Free Ebook - CLAT 2025 English Language questions with detailed solutions
ICFAI Business School-IBSAT 2024
Apply
9 IBS Campuses | Scholarships Worth Rs 10 CR
CLAT Legal Reasoning Mock Tests
Apply
Free Ebook - CLAT 2025 legal reasoning questions with detailed solutions
GIBS Business School Bangalor...
Apply
100% Placements with 220+ Companies
Great Lakes PGPM & PGDM 2025
Apply
Admissions Open | Globally Recognized by AACSB (US) & AMBA (UK) | 17.3 LPA Avg. CTC for PGPM 2024
View All Application Forms

Download the Careers360 App on your Android phone

Regular exam updates, QnA, Predictors, College Applications & E-books now on your Mobile

150M+ Students
30,000+ Colleges
500+ Exams
1500+ E-books