Answer to Question #315139 in Java | JSP | JSF for mich

Question #315139

The first line will contain a message prompt to input the value of n.



The second line contains the sum of all odd numbers from 0 to n.


1
Expert's answer
2022-03-21T08:22:54-0400


import java.util.Scanner;


public class App {


	/**
	 * The start point of the program
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner keyBoard = new Scanner(System.in);
		System.out.print("Enter n: ");
		int n = keyBoard.nextInt();
		int sumOddNumbers = 0;
		for (int i = 0; i <= n; i++) {
			if (i % 2 != 0) {
				sumOddNumbers += i;
			}
		}
		System.out.println("Sum of odd numbers = " + sumOddNumbers);


		keyBoard.close();


	}


}

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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS