Computer Science, asked by sakshamsrivastava653, 5 months ago

Write a program to define a method void sumEvenDigits( int n)- to find and print the sum
of only even digits of the number in the argument "num and also print the average of all even
digits.
For example:
10
If num=634, then output is:
The sum of even digits are =
The average of even digits are = 5
(int num ) - to print the number in the​

Answers

Answered by shraddha974096
3

Answer:

Java Program to Find Sum of Even Digits in a Given Number

Java / Leave a Comment

Previously we have written a Java program to find the sum of digits of a given number. Now in this post, we will write a Java program to find the sum of even digits in a given number.

Procedure to find the sum of even digits in a given number,

Take a number

Declare a variable evenDigitSum to store the sum value and initialize it with 0

Find the last digit of the number

Check that the last digit is even or not

If it even then adds it to evenDigitSum variable, else go to next step

Remove the last digit of the number

Repeat 3 to 6 steps until the number becomes 0

import java.util.Scanner;

public class SumOfEvenDigitsProgram {

 public static int findEvenDigitSum(int number) {

   // declare variables

   int lastDigit = 0;

   int evenDigitSum = 0;

   // loop to repeat the process

   while(number!=0) {

      // find last digit

      lastDigit = number%10;

      // check last digit even?

      if(lastDigit % 2 == 0)  

          // add it to sum

          evenDigitSum += lastDigit;

      }

      // remove last digit of number

      number = number / 10;

   }

   // return sum value

   return evenDigitSum;

 }

 public static void main(String[] args) {

    // declare variables

    int number = 0;

    int sumOfEvenDigits = 0;

    // create Scanner class object  

    // for reading the values

    Scanner scan =  new Scanner(System.in);

    // read inputs

    System.out.print("Enter an integer number:: ");

    number = scan.nextInt();

    // find sum of digits of number

    sumOfEvenDigits = findEvenDigitSum(number);

    // display result

    System.out.println("The sum of even digits of"+

      " the number "+number+" = "+ sumOfEvenDigits);

    // close Scanner class object

    scan.close();

 }

}

The output for the different test-cases are:-

Enter an integer number:: 1245

The sum of even digits of the number 1245 = 6

Enter an integer number:: 123456789

The sum of even digits of the number 123456789 = 20

In this program, we used a while loop. While loop is a pre-test loop where the expression is evaluated then only statements are executed. It uses a test expression to control the loop. Before every iteration of the loop, the test expression is evaluated.

Also See:-

Sum of digits of a number

The sum of N Natural numbers

Sum of odd digits in a number

Sum of first & last digit of a number

The Sum of Digits Until Single Digit

Instead of using a while loop, we can also use for loop to develop java program to find the sum of even digits in a given number. The for loop is also a pre-test loop, where first of all initialization expression is evaluated then the condition is checked and if the condition is true then only the statements of the for loop are executed.

public static int findEvenDigitSum(int number) {

    int sum, lastDigit;

    for(sum=0; number!=0; number/=10){

        lastDigit = number%10;

        if(lastDigit % 2 == 0)

            sum += lastDigit;

    }

    return sum;

}

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Explanation:

Similar questions