English

Write a program to accept a number and check if it is a Mark number or not. A number is said to be Mark when sum of the squares of each digit is an even number as well as the last digit - Computer Applications

Advertisements
Advertisements

Question

Write a program to accept a number and check if it is a Mark number or not. A number is said to be Mark when sum of the squares of each digit is an even number as well as the last digit of the sum and the last digit of the number given is same.

Example: n = 246

sum = 2 × 2 + 4 × 4 + 6 × 6 = 56

56 is an even number as well as last digit is 6 for both sum as well as the number.

Code Writing
Advertisements

Solution

import java.util.Scanner;

public class MarkNumber {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        // Input: Reading the integer n
        System.out.print("Enter a number: ");
        int n = sc.nextInt();
        
        // Let m = |n| to handle negative inputs
        int m = Math.abs(n);
        
        // Step 1: Store the last digit of the original number
        int last = m % 10;
        
        int sum = 0;
        int temp = m;
        
        // Step 2: Calculate the sum of squares of each digit
        while (temp > 0) {
            int digit = temp % 10;
            sum = sum + (digit * digit);
            temp = temp / 10;
        }
        
        // Step 3: Check the two conditions
        // 1. Sum must be even (sum % 2 == 0)
        // 2. Last digit of sum must equal last digit of n (sum % 10 == last)
        if (sum % 2 == 0 && (sum % 10 == last)) {
            System.out.println(n + " is a Mark number.");
        } else {
            System.out.println(n + " is NOT a Mark number.");
        }
        
        sc.close();
    }
}
shaalaa.com
  Is there an error in this question or solution?
2025-2026 (March) Official Board Paper
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×