Interview Question


Country: United States
Interview Type: Written Test




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

#include<stdio.h>
int main(void){

int a ,b,s=0,m,i=1;
printf(" \n Enter number 1 ");
scanf("%d",&a);
printf("Enter number 2 ");
scanf("%d",&b);
while(b>10)
{
m=b%10;
m=m*a*i; // 12x11 => 12 + 120
s=s+m;
b=b/10;
i=i*10;
scanf("%d",&s);
}
scanf("%d",&s);
return 0;
}

- Amogh Singhal December 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

and

//multiplication as repeated addition
#include <stdio.h>
#include <stdlib.h>

int main()
{
int multiplicant,multiplier,i,result=0;
scanf("%d",&multiplicant);
scanf("%d",&multiplier);
for(i=0;i<multiplicant;i++)
{
result+=multiplier;

}
printf("result =%d",result);
return 0;
}

and

- sri December 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//multiplication as repeated addition
#include <stdio.h>
#include <stdlib.h>

int main()
{
int multiplicant,multiplier,i,result=0;
scanf("%d",&multiplicant);
scanf("%d",&multiplier);
for(i=0;i<multiplicant;i++)
{
result+=multiplier;

}
printf("result =%d",result);
return 0;
}

- sree December 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//multiplication as repeated addition
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int multiplicant,multiplier,i,result=0;
    scanf("%d",&multiplicant);
    scanf("%d",&multiplier);
    for(i=0;i<multiplicant;i++)
    {
        result+=multiplier;

    }
    printf("result =%d",result);
    return 0;
}

- sree December 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

are numbers integers or could they be floats or doubles?

- zr.roman December 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

for integers the solution is simple and obvious,
what if the input numbers are doubles or floats?

- zr.roman December 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

With less complexity
#include <stdio.h>
#include <stdlib.h>

int main()
{
int multiplicant,multiplier,i,result=0;
scanf("%d",&multiplicant);
scanf("%d",&multiplier);
for(i=0;i<multiplicant/2;i++)
{
result+=multiplier;

}
result+=result;
if(multiplicant%2 !=0){
result+=multiplier;
}
printf("result =%d",result);
return 0;
}

- jagan December 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

its not working

- dammi baba December 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For integers

#include <stdio.h>
#include <stdlib.h>

int multiply(int multiplier, int multiplicant);

int main()
{
    int multiplicant,multiplier,i,result=0;
  scanf("%d",&multiplicant);
  scanf("%d",&multiplier);
    printf(" result =%d", multiply(multiplier,multiplicant));
    return 0;
}

int multiply(int multiplier, int multiplicant){
    if(multiplicant == 0 || multiplicant ==1){
        return multiplier;
    }
    int result = multiply(multiplier, multiplicant/2);
    result +=result;
    if( multiplicant %2 ==0){
        return result;
    } else{
        return result+multiplier;
    }    
}

- jagan December 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def mult(a, b):
	sum = 0
	while b != 0:
		if b & 1 != 0:
			sum = sum + a
		b >>= 1
		a <<= 1
	return sum
print(mult(44, 88))

- aka December 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It is same as saying ::

sum=sum+(b&1)a+(b>>1&1)a*2+(b>>1&1)a*4+(b>>1&1)a*8 ......

For example ::
344*244

is put it in form (344/2)*(244*2)
(172/2)*(244*4)
(86/2)*(244*8)=43*244*8
sum=sum+244*8;
sum=sum+244*16;
sum=sum+244*64;
sum=sum+244*256;

This can be simply put as
while(b!=0)
{
if(b is odd)
sum=sum+a;
b=b/2;
a=a*2;
}

- raghu.aok December 09, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think the tricky part is that you need to iterate for the small number, also take care of the sign. C# code

public int Multiply(int a, int b)
{
	int sign = a > 0 ^ b > 0 ? -1 : 1;

	a = Math.Abs(a);
	b = Math.Abs(b);
	int n = Math.Min(a, b);
	int number = Math.Max(a, b);
	int total = 0;

	for (int i = 0; i < n; i++)
		total += number;

	return sign * total;
}

- hnatsu December 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void mulAsAddition(int n,int m)
{
	int iProduct =0;
	for(int i=0;i<m;i++)
		iProduct=iProduct+n;
	cout<<endl<< "product of" << n << "and" << m<<"is"<<iProduct;
}

- sv December 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
int main()
{
long m1,m2,res=0;
printf("enter the first number\n");
scanf("%d",&m1);
printf("enter the second number\n");
scanf("%d",&m2);int i;
for(i=0;i<m1;i++)
res=res+m2;
printf("%d",res);
return 0;
}

- Anuj July 11, 2016 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More