InterviewSolution
| 1. |
Can anyone solve this java programming |
|
Answer» Here is the Java Program which accomplishes the task. We take the INPUTS, run a loop in the given range. Then we check the divisibility of each number by 2 and ADD to the appropriate sum variables. import java.util.Scanner; public class Brainly { public static void MAIN(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter lower limit (m): "); int m = sc.nextInt(); System.out.print("Enter UPPER limit (n): "); int n = sc.nextInt(); if(n>m) { int sumOfOddNumbers = 0; int sumOfEvenNumbers = 0; for(int i=m;i<=n;i++) { if(i%2==0) { sumOfEvenNumbers += i; } else { sumOfOddNumbers += i; } } System.out.println("Sum of Odd Numbers = "+sumOfOddNumbers); System.out.println("Sum of Even Numbers = "+sumOfEvenNumbers); } else { System.out.println("Invalid Inputs."); } } } |
|