173 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

East Point College | BBA Admi...
Apply
NBA Accredited | AICTE Approved
TAPMI MBA 2025 | Technology M...
Apply
MBA Admission Open in Technology Management and AI & Data Science | NAAC A++ | Institution of Eminence | Assured Scholarships
Amity University, Noida Law A...
Apply
700+ Campus placements at top national and global law firms, corporates, and judiciaries
East Point College | MBA Admi...
Apply
NBA Accredited | AICTE Approved
Entrepreneurship Development ...
Apply
PGDM- E Admissions 2021 | Among Top 25 Entrepreneurship Schools in India
Chitkara University MBA Admis...
Apply
NAAC A+ Accredited | 100% CAMPUS RECRUITMENT
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