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.
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();
}
}
Comments
Leave a comment