Usind for and while loops better
Trying to design an application which reads an integer and prints the sum
of all even integers between 2 and the input value. Can anybody help me
with the last bit?!
import java.util.Scanner;
public class IntergerValue {
// main method
public static void main(String[] args) {
// Data fields
int a;
// end
Scanner sc = new Scanner(System.in);
System.out.println("Enter an interger greater than 1");
a = sc.nextInt();
if (a <= 1) {
System.out.println("Input Value must not be less than 2");
}
while (a > 1) {
int sum = 0;
if (a % 2 == 0) {
sum += a;
a = a - 1;
}
System.out.println(sum);
}
}
}
No comments:
Post a Comment