Computer Science, asked by sahasubhadip2006, 3 months ago

Write a program in Java to accept a number then calculate and display the sum of all even digits and Odd digits with proper message.
Example: N = 124573
Sum of Even digits = 6
Sum of Odd digits = 16​


anveshshekhar12: Not much sure about java
will Python do?
sahasubhadip2006: no
sahasubhadip2006: can you pls do
anveshshekhar12: yes \
sahasubhadip2006: it's urgent
anveshshekhar12: i can try

Answers

Answered by anveshshekhar12
1

import java.util.*;  

 

class GFG {  

 

   // Function to return the reverse of a number  

   static int reverse(int n)  

   {  

       int rev = 0;  

       while (n != 0) {  

           rev = (rev * 10) + (n % 10);  

           n /= 10;  

       }  

       return rev;  

   }  

 

   // Function to find the sum of the odd  

   // and even positioned digits in a number  

   static void getSum(int n)  

   {  

       n = reverse(n);  

       int sumOdd = 0, sumEven = 0, c = 1;  

 

       while (n != 0) {  

 

           // If c is even number then it means  

           // digit extracted is at even place  

           if (c % 2 == 0)  

               sumEven += n % 10;  

           else

               sumOdd += n % 10;  

           n /= 10;  

           c++;  

       }  

 

       System.out.println("Sum odd = " + sumOdd);  

       System.out.println("Sum even = " + sumEven);  

   }  

 

   // Driver code  

   public static void main(String args[])  

   {  

       int n = 457892;  

       getSum(n);  

   }  

}  

Similar questions