Notification texts go here Contact Us Buy Now!

Write a program that given the digits of two numbers A and B prints out the digits of the sum. The first input is N, the number of digits in A as well as B. After this, the digits of A and B will be given one by one from least significant to the most significant. Your program should print the ith digit of the sum before reading the (i+1)th digit of A and B. You should print N+1 digits for the sum, where the most significant digit can be 0. The length N of the numbers can be large, so in general it won’t be possible to store A or B in the “int” or “long long” data type. Your program should mimic the manual addition process you learned in primary school, i.e. add digits consecutively together with the carry if any, with initially there being no carry. Note that if a computer has to do arithmetic on numbers with hundreds of digits, it will be done in the manner of your program.

Write a program that given the digits of two numbers A and B prints out the digits of the sum. The first input is N, the number of digits in A as w

 Write a program that given the digits of two numbers A and B prints out the digits of the sum.    The first input is N, the number of digits in A as well as B.  After this,

the digits of A and B will be given one by one from least significant to the most significant.  Your program should print the ith digit of the sum before reading the (i+1)th digit of A and B. You should print N+1 digits for the sum, where the most significant digit can be 0. The length N of the numbers can be large, so in general it won’t be possible to store A or B in the “int” or “long long” data type.  Your program should mimic the manual addition process you learned in primary school, i.e. add digits consecutively together with the carry if any, with initially there being no carry.  Note that if a computer has to do arithmetic on numbers with hundreds of digits, it will be done in the manner of your program.


For example, the sum of the numbers 12945 and 89597 is 102542.  Then the first input will be 5, the number of digits in A,B.  Then the least significant digits will be given, 5 and 7.   Then next least significant, 4, 9 and so on.  This example is shown in full in the third Sample Test Case below.


Note: Please do not include the header files. Start writing your code from main_program.

Input


### First line contains an integer ###

N- the number of digits in the numbers

0<N<100


### Followed by N lines ###

A1 B1

A2 B2

.

.

AN BN


Ai is the ith least significant digit of the number A. 

Bi is the ith least significant digit of the number B.

0<= Ai , Bi <=9


Output

The output should be N+1 digits of the sum A+B starting from the least significant. The (N+1)th digit can be 0 in case there is no carry. Note that each digit must appear in a new line. 


If S=A+B, the output should be

S1

S2

.

.

SN

SN+1

Where Si is the ith least significant digit of the sum S.

*****************************************************************************


#include<iostream>
using namespace std;

int main()
{
  long A =12945 , B = 89597 ; /* Do not assign values leave it empty in nptel compiler*/
  long sum=0,sum2=0; /* Do not assign values leave it empty in nptel compiler*/
  int N = 5; /* Do not assign values leave it empty in nptel compiler*/
  int i =0;
  int j=0;
  int r=0;
  sum=A+B;
  sum2 = sum;

while(sum2!=0)
{
  sum2=sum2/10;
  j++;
}
   
  while(i!=j)
  {
    r=sum%10;
    cout<<r<<endl;
    sum=sum/10;
   i++;
  }

}


OR Else

main_program
{
  long A =12945 , B = 89597 ; /* Do not assign values leave it empty in nptel compiler*/
  long sum=0,sum2=0; /* Do not assign values leave it empty in nptel compiler*/
  int N = 5; /* Do not assign values leave it empty in nptel compiler*/
  int i =0;
  int j=0;
  int r=0;
  sum=A+B;
  sum2 = sum;

repeat(sum2!=0)
{
  sum2=sum2/10;
  j++;
}
   
  repeat(i!=j)
  {
    r=sum%10;
    cout<<r<<endl;
    sum=sum/10;
   i++;
  }

}

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.