Computer Science, asked by nitishamadeshiya, 1 year ago

can anyone solve this java programming

Attachments:

Answers

Answered by QGP
1

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.");

   }

 }

}


Attachments:

nitishamadeshiya: super
QGP: Thank You :)
nitishamadeshiya: ur wlcome
Similar questions