Norm of a Number in Java
Norm of a Number in Java
Question : Write a program to calculate Norm of a Number.
Norm of a number is square root of sum of squares of all digits of the number.
Example :-
Input : 68
6 X 6 + 8 X 8 = 100 ; Square root of 100 = 10
Output : Norm of 68 is 10.
Logic :-
- First input number.
- Copy the input number in another variable
- extract each digit of the number
- Store the sum of squares of the digit of the number in a variable say 's'
- Print the square root of the result. (i.e. square root of 's')
- Compile and run the program
Note : To understand the program clearly watch this video : https://youtu.be/tXDl56akraE
Program to calculate Norm of a Number:-
Program :-
import java.io.*;
import java.util.*;
class Norm
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int n,p,dig,s=0;
System.out.println("Enter Number");
n= in.nextInt();//input number
p=n;//copying input number
while(p>0)
{
dig= p%10;
s= s+(dig*dig);//storing square of each digit
p=p/10;
}
System.out.println("Norm of "+n+" is : "+Math.sqrt(s));//print norm of a number
}//end of main method
}//end of class
For Proper Understanding Watch the Video :-
All the best :)
Keep Learning :)
Comments
Post a Comment