Write a java program to calculates the sum of squares in given digit

Write a java program to calculate the sum of squares in a given digit. Create a program that accepts a positive number as input and calculates the sum of squares of individual digits of the given number.

Input and Output Format

  • Input consists of an integer.
  • Output consists of an integer.

Sample Input:

321

Sample Output:

14

Java program to calculate the sum of squares in a given digit

Following are the steps to find the sum of squares in a given digit:

  • Input a number from the user.
  • Pass the number to getValues() method.
  • Inside the method, Declare a variable a and initialize it with the input number. Also, declare rem, and sum variable with 0.
  • Iterate over the number till it is not equal to 0. Retrieve each digit from the number and find its square and add them to store it into the sum variable.
  • At last, just print the value of the sum variable.
package com.demo;
 
import java.util.*;
 
public class Main {
 
private static Scanner sc;
 
public static void main(String[] args) {
sc = new Scanner(System.in);
int n = sc.nextInt();
getvalues(n);
}
 
public static void getvalues(int n) {
int a = n;
int rem = 0;
int sum = 0;
while (a != 0) {
rem = a % 10;
sum = sum + (rem * rem);
a = a / 10;
}
System.out.println(sum);
}
}

Output

Sum of the Square from 1 to N in Java

Write a program to accept positive integer ‘N’ from the user and find the sum of the square of all the numbers from 1 to N.

Input and Output Format

  • Input consists of an integer.
  • Output consists of the sum of squares from 1 to N integer.

Sample Input:

5

Sample Output:

55 (1+4+9+25+36)

Program to find the sum of the square from 1 to N

Following are the steps to calculate the sum of the square of numbers from 1 to N:

  • Input a number from the user. (i.e N- limit)
  • Pass the number to the getValues() method.
  • Inside the method, declare and initialize value sum and count.
  • Iterate till the count is less than the number keep doing the square of the number and store it in the sum variable. (Increment count value for each iteration)
  • At last, just print the sum value.
package com.demo;

import java.util.*;

public class SumOfSquare {
    private static Scanner sc;

    public static void main(String[] args) {
        sc = new Scanner(System.in);
        int n = sc.nextInt();
        getvalues(n);
    }

    public static void getvalues(int n) {
        int sum = 0, count = 1;
        while (count <= n) {
            sum = sum + (count * count);
            count++;
        }
        System.out.println(sum);
    }
}

Output

Sum of Square of Odd Numbers

Write a program to accept the positive integer from the user and print the sum of the squares of odd numbers to the limit specified.

Input and Output Format

  • Input consists of an integer.
  • Output consists of an integer.

Sample Input:

5

Sample Output:

35 (1+9+25)

Program to Calculate Sum of Square of Odd Numbers

Following are the steps to find the sum of the square of odd numbers:

  • Input a number from the user.
  • Pass the number to getValues() method.
  • Inside the method, declare and initialize the variable sum.
  • Now, iterate to the limit and keep checking if the number is odd and do the square, add them and store it in the sum variable.
  • At last, just print the value of the sum.
package com.demo;

import java.util.*;

public class SumOfSquare {
    private static Scanner sc;

    public static void main(String[] args) {
        sc = new Scanner(System.in);
        int n = sc.nextInt();
        getvalues(n);
    }

    public static void getvalues(int n) {
        int sum = 0;
        for (int i = 1; i <=n; i++){
            if(i % 2 !=0){
                sum = sum + (i*i);
            }
        }
        System.out.println(sum);
    }
}

Output

Thus, in this way, we learned how the sum of squares in a digit. Along with this, how to find the sum of the digit from 1 to N. At last, just check the sum of squares of odd numbers.