4/06/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 "

Ghanendra Yadav

Ghanendra Yadav

Hello, I Am Ghanendra Yadav Owner of This Blog, I am professional Blogger and Programmer. I Love Programming, Logo Making, And Banner Designing. My Highest Qualification is MCA From NIT Warangal. You Can Find Me On Social Media Through Below Link And If You Have Any Query Related To Programming And Other Subject Comment Below or You Can Mail Me I Will Try To Answer Within 24 Hours Email:- yghanendra@student.nitw.ac.in

Find me on Social Media

Facebook | Twitter | Google+ | RSS Feed

No comments:

Post a Comment