Write a Java program to find Sum of Squares of Even Digits

Write a program to read a number, calculate the addition of squares of even digits (values) present in the given number. Also, we will see How to find the sum of even digits along with the sum of the square of the first ‘N’ even numbers.

Sample Input and Output Format

  • Input consists of a positive integer n.
  • Output is a single integer.

Refer sample output for formatting specifications

Sample Input 1:

56895

Sample Output 1:

100

Sum of Squares of Even Digits in java.

 

Write a Java Program to calculate the sum of squares of even digits

Following are the steps to calculate the sum of squares of even digits in Java

  • Input number as an integer.
  • Declare and initialize variables n1, n2 with 0.
  • Iterate over the number and check if the number is even then multiply the number by itself and add it to the variable n2 then we will get the sum of the squares of even digits.
  • Repeat this process till the number is not equal to 0.
  • At last, print the number.
package com.demo;

import java.util.Scanner;

public class Main3 {
    public static void main(String[] args) {
        System.out.println("Enter number :");
        Scanner input = new Scanner(System.in);
        int number = input.nextInt();
        int n1 = 0, n2 = 0;
        while (number != 0) {
            n1 = number % 10;
            if ((n1 % 2) == 0)
              n2 += n1 * n1;
            number /= 10;
        }
        System.out.println(n2);
    }
}

Output

Print the Sum of Even digits

Create a Program to find the sum of the even digits in a given number in Java. Here, accept a number n, and find its sum of digits of even numbers.

Program to find the sum of even digits in Java

Following are the steps to find the addition of even digits in a number

  • Declare and initialize variable rem, sum, and temp.
  • Take a number as input in the num variable.
  • Store the number in a temp variable.
  • Find each digit from the number and check if it is even and store it to the sum variable. 
  • Repeat this process till the number is greater than 0.
  • At last, just print the sum value.
package com.date;

import java.util.Scanner;

public class SumOfEvenDigits {
    public static void main(String[] args) {
        int rem,sum = 0, temp;
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        temp = num;
        while (num > 0){
            rem = num % 10;
            if(rem % 2 == 0){
                sum = sum + rem;
            }
            num = num / 10;
        }
        System.out.println("The sum of even digits in "+temp+"is :"+sum);

    }
}

Output

Find the sum of squares of first ‘N’ Even Numbers

Create a Program in Java to find the sum of squares of first ‘N’ even numbers. Here, just iterate over the number and print the sum of the square of the first n numbers

Program to Print Sum of squares of first ‘N’ even numbers

Following are the steps to find the sum of the square of the first ‘N’ numbers:

  • Input a number and store it in variable n.
  • Declare and initialize the sum variable.
  • Traverse over the number and find the sum of the squares and store it in the sum variable.
  • At last, just print the value of the sum.
package com.date;

import java.util.Scanner;

public class Main4 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int temp = n;
        int sum = 0;
        for(int i =0 ;i <=n; i++){
            sum = sum + (2 * i) * (2 * i);
        }
        System.out.println("The sum of squares of even numbers till "+temp+" is "+sum);
    }
}

Output

Thus, in this way, we learned how to calculate the sum of squares of even digits in a number along with printing the sum of the first ‘N’ even numbers.