Answer to Question #3518 in Java | JSP | JSF for Y mukundareddy
2) Write a program to print the sum of the n even numbers by using for loop.
1
2012-03-06T10:48:24-0500
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int n, sum = 0;
Scanner in = new Scanner(System.in);
System.out.print("Enter a number n: ");
n = in.nextInt();
for(int i = 0; i <= n; i+=2){
sum += i;
}
System.out.println("sum = " + sum);
}
}
Need a fast expert's response?
Submit order
and get a quick answer at the best price
for any assignment or question with DETAILED EXPLANATIONS!
Learn more about our help with Assignments:
JavaJSPJSF
Comments
Leave a comment