DAY 10: coding interview questions
Question 1:
Sum of Powers of Digits: Alex has been asked by his teacher to do an assignment on powers of numbers. The assignment requires Alex to find the sum of powers of each digit of a given number, as per the method mentioned below.
If the given number is 582109, the Sum of Powers of Digits will be calculated as =
= (5 raised to the power of 8) + (8 raised to the power of 2) + (2 raised to the power of 1) + (1 raised to the power of 0) + (0 raised to the power of 9) + (9 raised to the power of 0)
i.e. each digit of the number is raised to the power of the next digit on its right-side. Note that the right-most digit has to be raised to the power of 0. The sum of all of these powers is the expected result to be calculated.
Example - If the given number is 582109, the Sum of Powers of Digits =
= (5 raised to the power of 8) + (8 raised to the power of 2) + (2 raised to the power of 1) + (1 raised to the power of 0) + (0 raised to the power of 9) + (9 raised to the power of 0)
= 390625 + 64 + 2 + 1 + 0 + 1 = 390693
Alex contacts you to help him write a program for finding the Sum of Powers of Digits for any given number, using the above method.
Write the logic in the given function sumOfPowerOfDigits where,
input1 represents the given number.
The function is expected to return the "Sum of Powers of Digits" of input1.
Assumptions: For this assignment, let us assume that the given number will always contain more than 1 digit, i.e. the given number will always be >9.
//Code:
import java.io.*;
import java.util.*;
class UserMainCode{
public int sumOfPowerOfDigits(int input1){
double sum=0.0;
String str=Integer.toString(input1);
for(int i=0;i<str.length()-1;i++) {
int a=Character.getNumericValue(str.charAt(i));
int b=Character.getNumericValue(str.charAt(i+1));
sum=sum + Math.pow(a, b);
}
return (int)sum+1;
}}
Sum of Sums of Digits in Cyclic order: Alex has been asked by his teacher to do an assignment on sums of digits of a number. The assignment requires Alex to find the sum of sums of digits of a given number, as per the method mentioned below.
If the given number is 582109, the Sum of Sums of Digits will be calculated as =
= (5 + 8 + 2 + 1 + 0 + 9) + (8 + 2 + 1 + 0 + 9) + (2 + 1 + 0 + 9) + (1 + 0 + 9) + (0 + 9) + (9)
= 25 + 20 + 12 + 10 + 9 + 9 = 85
Alex contacts you to help him write a program for finding the Sum of Sums of Digits for any given number, using the above method.
Help Alex by completing the login in the given function sumOfSumsOfDigits which takes as input an integer input1 representing the given number.
The function is expected to return the "Sum of Sums of Digits" of input1.
Assumptions: For this assignment, let us assume that the given number will always contain more than 1 digit, i.e. the given number will always be >9.
//Code:
import java.io.*;
import java.util.*;
class UserMainCode{
public int sumOfSumsOfDigits(int input1){
String str=Integer.toString(input1);
int sum=0;
import java.util.*;
class UserMainCode{
public int sumOfSumsOfDigits(int input1){
String str=Integer.toString(input1);
int sum=0;
for(int j=i;j<str.length();j++){
int num=Character.getNumericValue(str.charAt(j));
sum+=num;
}}
return sum;
}}
0 Comments
Please do not Enter any spam link in the comment box