## Amazon Interview Question for Software Engineer / Developers

• 0

Comment hidden because of low score. Click to expand.
0
of 0 vote

int Sum(int n){

int count = 0;
int r1 = n%10;
n = n/10;
while (n>9){
int r = n%10;
n = n/10;
r1 = r+r1;

if( r1 >10 ){
count++;

r1 = r1-10;
}

if(count >9){
count =1;
}
}
cout<<count<<endl;
r1 = r1+n;
if( r1 >10 ){
count++;
r1 = r1 - 10;
}
if(count >9){
count =1;
}

return (count+r1);
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````int FindSum(int num){
//If num is single digit
int sum = 0;
if(num / 10 == 0)
return num;
else
{
while(num > 1){
sum = sum + num % 10;
num = num / 10;
}
//If sum is not single digit
if( sum / 10 != 0)
sum = FindSum(sum);
}

return sum;
}
int main( )
{
cout << FindSum(9999) << endl;
return 0;
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

Assuming non-negative numbers --
we can take advantage of the fact that

singleSum(994) = singleSum(9 + singleSum(94))

therefore,

``````int singleSum(int num) {
int n = num;
int sum = 0;
int mod = 0;
while(n) {
mod = n % 10;
n = n / 10;
sum += mod;
if(sum > 9)
sum -= 9;
}
return sum;
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

Modifying Sachinsaner's code to make it work (Just added a >= for num>1)

int FindSum(int num){
//If num is single digit
int sum = 0;
if(num / 10 == 0)
return num;
else
{
while(num > 1){
sum = sum + num % 10;
num = num / 10;
}
//If sum is not single digit
if( sum / 10 != 0)
sum = FindSum(sum);
}

return sum;
}
int main( )
{
cout << FindSum(9999) << endl;
return 0;
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````public class SumofDigits {

public static int sumofdigits(int inputnum)
{
int quot = inputnum;
int rem = 0;
while(quot > 0)
{
rem = rem + (quot % 10);
quot = quot/10;
}
if(rem/10 > 0)
{
rem = sumofdigits(rem);
}
return rem;
}

public static void main(String[] args)
{
int result = 698239623; //input number
result = SumofDigits.sumofdigits(result);
System.out.println(result);
}
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

int Sum(int n)
{
while (n > 9)
{
int sum = 0;
do
{
sum += n % 10;
n /= 10;
} while (n > 0);
n = sum;
}

return n;
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

public static Integer findNumber(Integer sum,Integer i){

if(i == 0 && sum > 10)
return findNumber(0,sum);
else if(sum<10 && i == 0)
return sum;
else
return findNumber(sum+(i%10),(i/10));
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

public int calculate(int num) {
while (num > 10) {
int curr_num = num;
num=0;
while (curr_num > 0) {
num += curr_num % 10;
curr_num = curr_num /10;
}
}
return num;
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

int out(int in){
int val;
if(int(in/10)==0)
return(in);
val=in%10+out(int(in/10));
return(int(val/10)==0?val:out(val));
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````int FindSum(int num)
{
int sum = 0
while(num > 9)
{
sum = sum + n%10;
num = num/10;
if(num < 10)
{
num += sum; sum = 0;
}
}
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

forgot to return number...

``````int FindSum(int num)
{
int sum = 0
while(num > 9)
{
sum = sum + n%10;
num = num/10;
if(num < 10)
{
num += sum;
sum = 0;
}
}
return num;
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

You must be kidding around here.

number % 9

987 % 9 = 6 = 9 + 8 + 7 = 2 + 4 = 6

Comment hidden because of low score. Click to expand.
0

can you explain why this works?

Comment hidden because of low score. Click to expand.
0

won't work for multiples of 9 (9, 18, 27, ....)

Comment hidden because of low score. Click to expand.
0

digit 9 has a secial property that when added to another digit, single digit some would be the other digit. When multiplied by other digit the resulting single sum woulb be 9.

using above observations, solution can be

if(num%9 == 0)
return 9;
else
return (num%9);

Comment hidden because of low score. Click to expand.
0

good thing to know. great!

Comment hidden because of low score. Click to expand.
0

- hello2008

Comment hidden because of low score. Click to expand.
0
of 0 vote

9 has a property, when added to a digit ...becomes the digit e.g. 9+7=16 = 1+6=7
when multiplied wid digit, resulting single digit sum is 9 e.g. 9*7 =63 =6+3=9

Moreover if number>9 sum cant be 0

if(num>9)
{
if(num%9==0)
return 9;
else
return (num%9);
}
else
return num;

Comment hidden because of low score. Click to expand.
0
of 0 vote

{{
#include<stdio.h>
int main()
{
int r=0,n=0,sum=0;
scanf("%d",&n);
while(n)
{
r=n%10;
sum=sum+r;
if(sum>9)
sum=sum-9;
n=n/10;
}
printf("%d",sum);
return 0;
}
}}

Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="c" line="1" title="CodeMonkey59881" class="run-this">#include<stdio.h>
int main()
{
int r=0,n=0,sum=0;
scanf("%d",&n);
while(n)
{
r=n%10;
sum=sum+r;
if(sum>9)
sum=sum-9;
n=n/10;
}
printf("%d",sum);
return 0;
}
</pre><pre title="CodeMonkey59881" input="yes">123
</pre>

Comment hidden because of low score. Click to expand.
0
of 0 vote

int findsum(int n)
{
int sum=0;
while(n>9)
{
while(n)
{
sum=sum+n%10;
n=n/10;
}
n=sum;
sum=0;

}
return n;
}

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.