Adobe Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: Written Test
@jaks Yes, just edit the main loop a little:
int q = 0;
printf("Quotient = ");
while(rem)
{
q = ( rem*10 + 1 ) / num;
printf("%d", q);
rem = ( rem*10 + 1 ) % num;
count++;
}
printf(" Number = ");
#include<stdio.h>
#include<conio.h>
void main()
{ clrscr();
int n, num=1, r, x=1;
scanf("%d",&n);
while(x)
{ r=num%n;
if(r==0)
x=0;
else
{
num=r*10 +1;
}
printf("1");
}
getch();
}
At some point the value of r will be overflowed, which is clearly mentioned in question. E.g. after 11111 (limit of int is 32,767).
@mads overflow condition is for 11111..... not for 'n',,, i hv used n as the input value and and num & r as jus a temporary variable...111...are displayed by using printf
@shagun:u using value of mod r=num%n to calculate the next num i thnk num will never be grter than 11...how it will work den
@shagun: apologize for pointing wrong variable. I was talking about num. num will grow like 1...11...111...1111 but after 11111 num will be overflowed.
num will never exceed 91 since remainder will b 9 at max so 9*10+1....max 91...dis way no prob of overflow watever b the value of n(bt value of the number input shud b in d range of integer)
@shagun: i didn't get how the remainder will never exceeds 9
take example of n = 13
in first iteration: r = 1 and num = 11
in next iteration: r = 11
answer in Java:
public static void printAllOnes(int n)
{
System.out.println("Input: "+n);
if(n%10!=3) {
System.out.println("Output: Error !");
return;
}
else {
boolean cont = true;
int num = 0;
int length = 0;
// get integer consisting only 1's
while(cont){
if(num>0 && num%n==0)
{
cont = false;
System.out.println("Output:"+num);
}
else{
num = (int) (num + Math.pow(10, length));
length ++;
}
}
}
}
#include<stdio.h>
int main(){
int count=0;
int number,k;
scanf("%d",&number);
int temp = number;
printf("number = %d \n",number);
while(temp != 0){
temp = temp/10;
count++;
}
int start = 0;
for(k=0;k<count;k++){
start = start*10+1;
}
start = start*10+1;
printf("the start value is %d \n",start);
while(start != 0){
start = start*10+1;
count++;
start = start%number;
}
for(k=0;k<=count;k++){
printf("1");
}
printf("\n");
return 0;
}
Hi,
As per my understanding of the problem we need to print multiples of the number ending in 3 which consists of all 1's.But how is 111(7) a multiple of 3 and 111111(63) a multiple of 13.Can somebody elaborate the question please.
Thanks in advance.
public void test() {
System.out.println(bar(3));
System.out.println(bar(13));
System.out.println(bar(23));
}
private String bar(int x){
int i = 0;
int rem = 0;
StringBuilder sb = new StringBuilder();
do{
rem += foo(i, x);
i++;
sb.append("1");
} while (rem % x !=0);
return sb.toString();
}
private int foo(int power, int mod){
int retVal = 1;
for(int i =0; i<power; i++){
retVal = (retVal*10) % mod;
}
return retVal;
}
/*This code properly maintains overflow by returning the number of 1's in the answer */
int f(int num)
{
int a = 111;
int length=3;
while(1)
{
if(a%num == 0 ) return length;
a = a*10 + 1;
if(a>=num) a%=num;
length++;
}
}
int main()
{
int length = f(23);
int i;
for(i=1; i<=length; i++)
printf("1");
printf("\n");
return 0;
}
Understandable version of code.
- rakesh June 25, 2012