Numbers Addition - Left to Right - c - Spicy Coders

Recent

Tuesday, July 11, 2017

Numbers Addition - Left to Right - c

Given two numbers N1 and N2, find the sum by adding them from left to right.

Input Format:
 The first line contains N1.
The second line contains N2.

Output Format:
The first line contains the sum of N1 and N2 added from left to right.

Boundary Conditions: 100 <= N1 <= 999999 100 <= N2 <= 999999

Example Input/Output 1:
Input:
6321
5235

Output:
1656

Example Input/Output 2:
Input:
16282
5964

Output:
11257

Solution:
#include<stdio.h>
#include <stdlib.h>
int main()
{
int a,b;
scanf("%d%d",&a,&b);
int p=rev(a);
int c=rev(b);
counts(&p,&c);
int r=p+c;
printf("%d",rev(r));
return 0;
}
int rev(int a){
    int t=0;
    while(a>0){
        t=t*10+a%10;
        a=a/10;
    }
    return t;
}
int counts(int *a,int *b){
          if(count(*a)==count(*b)){
                   return 1;
          }
          else if(count(*a)>count(*b)){
                   int v=count(*a)-count(*b);
                   while(v--){
                   *b=*b*10;
          }
          }
          else{
          int d=count(*b)-count(*a);
          while(d--){
          *a=*a*10;
}
}
}
int count(int a){
          int c=0;
          while(a>0){
          a=a/10;
          c++;
}
return c;

}

No comments:

Post a Comment