top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C: Given two numbers a=20 b =30 write a method that return an int c=3020 without using arithmetic and string operations?

+3 votes
582 views

It was asked today in interview but could not crack it, any help.

posted Aug 13, 2015 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

3 Answers

+1 vote

Its concat i.e. b followed by a. Just need to do some trick

#include <stdio.h>

#define myfunc(a,c) b##a

int main()
{
    int c;
    c = myfunc(20,30);
    printf("%d\n", c);
    return 0;
}
answer Aug 13, 2015 by Upma
0 votes
#include <stdio.h>

int main(void) {
    int a, b;
    scanf("%d%d",&a,&b);
    printf("%d",b);
    printf("%d",a);
    return 0;
}

output
3020

answer Aug 14, 2015 by Vikram Singh
0 votes

I think there is no other way except string concatenation and arithmetic operation for your problem.
I would give you simple arithmetic logic

int a=20;
int b=30;
int c=(b*100)+a;

if you tell your purpose i would give a better solution
thank you.

answer Aug 16, 2015 by Vivek B
Question was saying no arithmetic operation :) so no + - * /
...