175 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

MAHE Bengaluru Law 2025
Apply
Accorded Institution of Eminence by MoE, Govt. of India | NAAC A++ Grade | Ranked #4 India by NIRF 2024
Chandigarh University Admissi...
Apply
Ranked #1 Among all Private Indian Universities in QS Asia Rankings 2025 | Scholarships worth 210 CR
MAHE Bengaluru BBA/ IPM 2025
Apply
Top Business School accredited by AACSB & AMBA | Ranked #4 by NIRF, NAAC A++ Accredited
TAPMI MBA 2025 | Technology M...
Apply
MBA Admission Open in Technology Management and AI & Data Science | NAAC A++ | Institution of Eminence | Assured Scholarships
Sanskriti University LLM Admi...
Apply
Best innovation and research-driven university of Uttar Pradesh
Maya Devi University LLM admi...
Apply
43.6 LPA Highest Package | 5.48 LPA Average Package | 150+ Courses in UG, PG, Ph.D
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