Amazon Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: In-Person
Yes. It would. You'll just need a check for negative numbers.
public void print(int number) {
if(number < 0) {
System.out.println('-');
number *= -1;
}
if(number < 10) {
System.out.println(number);
} else {
print(number / 10);
System.out.println(number % 10);
}
}
Yes. It would. You'll just need a check for negative numbers.
public void print(int number) {
if(number < 0) {
System.out.println('-');
number *= -1;
}
if(number < 10) {
System.out.println(number);
} else {
print(number / 10);
System.out.println(number % 10);
}
}
@smffap - what abt numbers like 10 or 100?
Here's a compiled/executed (non-recursive) solution that should handle all special cases
public void PrintDigits(int num)
{
int count = 0;
int digit = 0;
if (num < 0)
{
Console.WriteLine(" - ");
num *= -1;
}
if (num < 10)
{
Console.WriteLine("Digit is {0}", num);
return;
}
while (num >= 10)
{
digit = FindDigit(num, out count);
Console.WriteLine("Digit is {0}", digit);
int temp = (int) Math.Pow(10, count);
temp = digit * temp;
num = num - temp;
//For numbers like 100, 1000
if (num == 0 && count > 1)
{
for (int i = 0; i < count; i++)
{
Console.WriteLine("Digit is {0}", num);
}
return;
}
//For last digit found
if (num < 10)
{
Console.WriteLine("Digit is {0}", num);
return;
}
count = 0;
}
return;
}
private int FindDigit(int number, out int countTens)
{
countTens = 0;
while(number >= 10)
{
number /= 10;
countTens++;
}
return number;
}
}
}
public static void printDigitsInLine(int n)
{
int power= (int)Math.log10(n);
int _base= (int)Math.pow(10, power);
if(n==0)
System.out.println(n);
while(_base >0)
{
int p =n/_base;
System.out.println(p);
n= n%_base;
_base= _base/10;
}
}
Aj,
It can easily be turned into without the _base and power variables
public static void printInLine(int n)
{
if( n <0)
while(Math.abs(n) >10)
{
System.out.println(n/((int)Math.pow(10, (int)Math.log10(Math.abs(n)))));
n= Math.abs( n%((int)Math.pow(10, (int)Math.log10(Math.abs(n)))) );
}
else
while(n >10)
{
System.out.println(n/((int)Math.pow(10, (int)Math.log10(n))));
n= n%((int)Math.pow(10, (int)Math.log10(n)));
}
System.out.println(n);
}
Don't use Math.pow. It's likely to be slow because it's intended for doubles, and might possibly have weird associated roundoff errors (not saying it necessarily will in this case). I would recommend doing a loop to see what the greatest power of 10 less than n is, assigning it to a variable m, and then doing print (n / m) % 10; m = m / 10; in a loop until m drops to 0
By the way, using an extra on-stack variable is not using "extra space" because it wouldn't be possible to solve the problem if it were. When you do something like
System.out.println(n/((int)Math.pow(10, (int)Math.log10(Math.abs(n)))));
n= Math.abs( n%((int)Math.pow(10, (int)Math.log10(Math.abs(n)))) );
some of those intermediate values could be saved to the stack anyway.
#include<stdio.h>
void print(int);
int main()
{
int i;
scanf("%d",&i);
print(i);
return (0);
}
void print(int digit)
{
if(digit/10==0)
printf("%d\n",digit);
else
{
print(digit/10);
printf("%d\n",digit%10);
}
}
HI jyoti. I do not this this will not print line by line. if we provide the input number is 1234. As per the question, it supposed to print 4 lines. Your code will print only last digit.
How is this? This is C# code
for(int i=(int)(Math.Log10(inValue)/Math.Log10(10) + 1);i>0;i--)
{
Console.WriteLine((int)(inValue/Math.Pow(10,i-1)));
inValue = inValue % (Int)Math.Pow(10, i - 1);
}
It does not work. It is returning the output as below for the input 1234
1
12
123
1234
expected output will be
1
2
3
4
#include <iostream>
#include <cmath>
using namespace std;
void Funct(int);
int main()
{
int n;
cout<<"Enter a no: ";
cin>>n;
Funct(n);
return 0;
}
void Funct(int temp)
{
int i;
cout<<"The OutPut is: "<<endl;
for(i=pow(10,int(log10(temp)));i>0;)
{
cout<<temp/i<<endl;
temp=temp%i;
i=pow(10,int(log10(temp)));
}
}
Try this one out...
It is producing the desired result...
<pre lang="" line="1" title="CodeMonkey92430" class="run-this">import java.io.*;
import java.text.*;
public class Digits
{
public void printDigit(int n)
{
if (n>=10)
printDigit(n/10);
System.out.println(n%10);
}
public static void main(String[] args)
{
int num = 0;
System.out.println("Enter any number");
try
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String s = in.readLine();
DecimalFormat df = new DecimalFormat();
Number n = df.parse(s);
num = n.intValue();
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
System.out.println("number is: " + num);
Digits d = new Digits();
d.printDigit(num);
}
}</pre><pre title="CodeMonkey92430" input="yes">
</pre>
The print statement should be before the recursive call
void printval(int n)
{
if (n==0) return;
System.out.println(n%10);
printval(n/10);
return;
}
Aj, your solution prints in reverse order. ie 1234 will print 4321.
If you put the print statement after the prinval, it will print in the right order but it fails if n=0;
This looks good, I am using only two variables i.e. x=number, n=digit. I feel use of this variable should not be considered as temp. Correct me if I am wrong.
#include <stdio.h>
#include <iostream>
#include <math.h>
using namespace std;
void print(int x, int n){
while(n>0){
cout<<x/int(pow (10,n-1))<<"\n";
x=x%int(pow (10, n-1));
n--;
}
}
int main(){
int number=1054;
int digits=4;
print(number,digits);
return 0;
}
while(n>0) {
cout<<n%10;
n=n/10;
}
return;
Either I understood the question wrongly or u guys did :D
Dude you solution will print digits in reverse.
i.e. For input 1234 your sol will print
4
3
2
1
but the required out put is
1
2
3
4
Integer temp = 532323;
String s = temp.toString();
for(int i = 0; i<s.length(); i++){
System.out.println(s.charAt(i));
}
<pre lang="" line="1" title="CodeMonkey13867" class="run-this">class Main
{
public static void main (String[] args) throws java.lang.Exception
{
PrintNum(1024);
}
public PrintNum(int n)
{
while (n != 0)
{
System.out.println(n % 10);
n = n / 10;
}
}
}
</pre><pre title="CodeMonkey13867" input="yes">1024
</pre>
I'm sorry, the above one is wrong. I did the answer in reverse order. Here's the correct one:
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
PrintNum(1024);
}
public void PrintNum(int n)
{
if (n == 0)
return;
PrintNum(n / 10);
System.out.println(n % 10);
}
}
<pre lang="" line="1" title="CodeMonkey55407" class="run-this">class PrintNumber {
public void print(int number) {
if(number < 0) {
System.out.println('-');
number *= -1;
}
if(number < 10) {
System.out.println(number);
} else {
int pow = (int) Math.pow(10, (int) Math.log10(number));
do {
System.out.println(number / pow);
number %= pow;
pow /= 10;
} while(pow != 0);
}
}
public static void main(String[] args) {
PrintNumber instance = new PrintNumber();
instance.print(0);
instance.print(1);
instance.print(10);
instance.print(12);
instance.print(122);
instance.print(-1);
instance.print(-123);
}
}
</pre><pre title="CodeMonkey55407" input="yes">
</pre>
No recursion needed. Just get the multiple of 10 to be used as divisor first and keep going.
int n = 45678;
printf("%d\n", n);
int m = 1;
int p = n;
while(n)
{
n /= 10;
if (n) m *= 10;
}
n = p;
do
{
printf("%d\n", n/m);
n -= (n/m)*m;
m /= 10;
} while(n);
So m is 10,000 to start with. int division of 45678 with 10000 gives you 4. Now subtract 40000 from 45678. It gives us 5678. Divide m by 10 to get 1,000
int division of 5678 with 1000 gives 5 and so on.
Handles -ve, 0 and +ve. It keeps printing the -ve for each number. Handling it is left as an exercise to the reader.
would this work?
- Anonymous October 28, 2011