06/04/2017

Geeksforgeeks Solution For " Sum of two numbers represented as arrays "

GeeksforGeeks Solution For Hard Domain .Below You Can Find The Solution Of  School Basic ,Easy ,Medium . Or Hackerrank Solution You Can Also Direct Submit Your Solution to Geeksforgeeks Same Problem .You Need to login then you can submit you answers 

Problem :- Sum of two numbers represented as arrays

Submit Your Solution :- Click Here 

Solution :- 

#include<iostream>
using namespace std;
int Sum_of_two_numbers(int [],int [],int [],int ,int ) ;
void reverse(int [],int ) ;
int main()
 {
 int t,arr1[1000],arr2[1000],n1,n2,i ;
 cin>>t ;
 while(t--)
 {
    cin>>n1>>n2 ;
  
    for(i=0;i<n1;i++)
        cin>>arr1[i] ;
    for(i=0;i<n2;i++)
        cin>>arr2[i]  ;
      
    int arr3[1001]={0} ;
  
    int no=Sum_of_two_numbers(arr1,arr2,arr3,n1,n2) ;
  
    reverse(arr3,no) ;
  
    for(i=0;i<=no;i++)
        cout<<arr3[i]<<" " ;
    cout<<endl ;
 }
 return 0;
}

int Sum_of_two_numbers(int a[],int b[],int c[],int n1,int n2)
{
    int k,carry ;
    k=0 ;
    carry=0 ;
   
    while(n1!=0&&n2!=0)
    {
        c[k]=a[--n1]+b[--n2]+carry ;
        carry=c[k]/10 ;
        c[k]=c[k]%10 ;
        k++ ;
    }
   
    if(n1!=0)
        while(n1!=0)
        {
            c[k]=a[--n1]+carry ;
            carry=c[k]/10 ;
            c[k]=c[k]%10 ;
            k++ ;
        }
       
    if(n2!=0)
        while(n2!=0)
        {
            c[k]=a[--n2]+carry ;
            carry=c[k]/10 ;
            c[k]=c[k]%10 ;
            k++ ;
        }
       
    if(carry!=0)
        c[k++]=carry ;
   
    return k-1 ;
}

void reverse(int arr[],int no)
{
    int i,j,temp ;
    i=0;
    j=no ;
   
    while(i<j)
    {
        temp=arr[i] ;
        arr[i]=arr[j] ;
        arr[j]=temp ;
        i++ ;
        j-- ;
    }
}

Output:-



Geeksforgeeks Solution For " Sum of two numbers represented as arrays "

Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: