Untitled
unknown
java
a month ago
1.1 kB
4
Indexable
import java.util.Scanner; public class SumOddOrEvenNumbers { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); //input values int low, high, x; low = scnr.nextInt(); high = scnr.nextInt(); x= scnr.nextInt(); int i; int result = 0; if (x!=1 && x!=2) { System.out.println("Invalid"); } //checking if x is 1 to add odd values if (x==1){ //while (result<=high) { for (i=low; i<=high; ++i) { if (i%2!=0){ result= result+i; } } System.out.println(result); } //checking if x is 2 to add odd values else if (x==2){ //while (result<=high) { for (i=low; i<=high; ++i) { if (i%2==0){ result= result+i; } } System.out.println(result); } } }
Editor is loading...
Leave a Comment