Computer Applications

The following program segment calculates the norm of a number. The norm of a number is the square root of sum of squares of all the digits of the number.
Sample Input: 68
Sample Output: The norm of 68 is 10
[Hint: 6x6 + 8x8 = 36 + 64 = 100
The square root of 100 is 10. Hence, norm of 68 is 10]
There are some places in the program left blank marked with ?1?, ?2?, ?3? and ?4? to be filled with variable/function/expression.

Scanner in = new Scanner(System.in);
int num;
int d, s = 0;
num = ......?1?...... 	
while (......?2?......)
{
    d = num % 10;
    s = ......?3?......
    num = num / 10;
}
System.out.println("The norm of " + num + " is " + ......?4?......);

Based on the above discussion, answer the following questions:

(a) Which of the following will be filled in place of ?1?

  1. in.nextInteger;
  2. in.NextInt( );
  3. in.nextInt( );
  4. in.nextint( );

(b) What will you fill in place of ?2?

  1. num > 0
  2. num < 0
  3. num > 1
  4. num = 0

(c) What will you fill in place of ?3?

  1. s + d * d
  2. s * d + d
  3. s * s + d
  4. s + d

(d) What will you fill in place of ?4?

  1. Math.sqrt(s)
  2. Math.SQRT(s)
  3. Math.sqrt(n)
  4. Math.sqrt(num)

Java Iterative Stmts

16 Likes

Answer

(a) in.nextInt( );

(b) num > 0

(c) s + d * d

(d) Math.sqrt(s)

The completed program is given below for reference:

Scanner in = new Scanner(System.in);
int num;
int d, s = 0;
num = in.nextInt( ); 	
while (num > 0)
{
    d = num % 10;
    s = s + d * d;
    num = num / 10;
}
System.out.println("The norm of " + num + " is " + Math.sqrt(s));

Answered By

11 Likes


Related Questions