AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



programming-basics: Calculate the sum of digits in a^b

Input specification:


The first line contains the number of inputs t. After that t lines follow each containing a pair of space separated integers i.e. a and b.


Output specification:


For each input, you need to print the sum of digits in a^b.


Sample Input :


2


2 10


3 3


Sample Output :


7


9


Explanation:


Consider the first input. 2^10 = 1024. Now, 1 + 0 + 2 + 4 = 7 So, output for this input is 7.

programming x 169
basics x 171
Posted On : 2017-07-10 11:50:21.0
profile Divesh - anyforum.in Divesh
101180
up-rate
3
down-rate

Answers


import java.util.Scanner;

public class PowerDigitsSum {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int size=input.nextInt();
int[] a=new int[size];
int[] b=new int[size];
for(int i=0;i<size;i++){
a[i]=input.nextInt();
b[i]=input.nextInt();
}

for(int i=0;i<size;i++){
System.out.println(getPowerDigitsSum(a[i],b[i]));
}
}

public static int getPowerDigitsSum(int a, int b){
return getSumOfDigits((int)Math.pow(a, b));
}

public static int getSumOfDigits(Integer number){
int sum=0;
String num=number.toString();
for(int i=0;i<num.length();i++){
sum=sum+Integer.valueOf(Character.toString(num.charAt(i)));
}
return sum;
}
}

Posted On : 2017-07-10 23:13:12
Satisfied : 1 Yes  1 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523188233879
Reply This Thread
up-rate
4
down-rate

import java.util.*;
import java.lang.Math;
class SumofPow
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int len=sc.nextInt();
int sum,sum1;
int a[]=new int[len];
int b[]=new int[len];
for(int i=0;i<len;i++)
{
a[i]=sc.nextInt();
b[i]=sc.nextInt();
}
for(int i=0;i<len;i++)
{
sum=(int)Math.pow(a[i],b[i]);
while (sum > 9 )
{
sum1=0;
while (sum > 0)
{
int rem;
rem = sum % 10;
sum1 = sum1 + rem;
sum = sum / 10;
}
sum = sum1;
}
System.out.println(sum);
}

}
}

Posted On : 2017-07-11 15:49:10
Satisfied : 1 Yes  1 No
profile Sudheer Kumar Javvadi - anyforum.in Sudheer Kumar Javvadi
0960
Reply This Thread
up-rate
3
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in