Add Numbers - Base N ID:2672



Two numbers X and Y are provided with reference to base N. Add the numbers and print their sum with reference to base 10.

Input Format:
First line will contain the value of N.
Second line will contain X and Y separated by one or more spaces.

Output Format:
First line will contain the sum of X and Y to the base 10.

Boundary Conditions:
1 <= N <= 10

Example Input/Output 1:
Input:
2
1010 11

Output:
13

Explanation:
1010 to the base 2 is 10. 11 to the base 2 is 3.
Hence the sum is 10+3 = 13.

Example Input/Output 2:
Input:
3
11 201

Output:
23

Explanation:
11 to the base 3 is 4. 201 to the base 3 is 19.
Hence the sum is 23

#include<stdio.h>

#include <stdlib.h>

int base;

int findii(int a);

int main()

{ int a,b,n=0,m=0;

  scanf("%d %d %d",&base,&a,&b);

  n=findii(a);

  m=findii(b);

  printf("%d",n+m);

    }

  int findii(int a){

      int t,mul,n=0,i=0;

      mul=base;

  while(a){

      t=a%10;

      if(i==0)

       n=t;

       else{

           n=n+(t*mul);

           mul=mul*base;

       }

       i++;

       a=a/10;

  }

 return n;

}

Comments

Popular Posts