Computer Science, asked by jaiswalishita009, 22 days ago

write a program to input any multi digit number and print the sum of even digit and product of odd digit and display the sum of both the results in java​

Answers

Answered by anindyaadhikari13
6

Solution.

The given problem is solved using language - Java.

import java.util.*;

public class SumProduct{

   public static void main(String s[]){

       int num,sum=0,product=1,r;

       Scanner sc=new Scanner(System.in);

       System.out.print("Enter a number: ");

       num=sc.nextInt();

       sc.close();

       for(;num!=0;num/=10){

           r=num%10;

           if(r%2==0)

               sum+=r;

           else

               product*=r;

       }

       if(product==1)

           product=0;

       System.out.println("Sum of even digits: "+sum);

       System.out.println("Product of odd digits: "+product);

   }

}

Explanation.

  • At first, initialise sum=0, product=1.
  • Then, we will extract each digits of the number using loop.
  • If the digits are even, add them to sum or else, multiply them and store them in product.
  • At last, display the sum and product.

Note: If no odd digits are present, then the product is 0.

See attachment for output.

Attachments:
Similar questions