WEEK:7 PROGRAMMING ASSIGNMENT 1 SOLUTION (Jan 2020-Apr2020) An Introduction to Programming through C++ NPTEL 2020

  

               An Introduction To Programming Through C++


IIT Bombay.



Week 7- Programming Assignment 1:


The input for this program consists of numbers n, a0, a1, ..., a{n-1}, b0, b1, b{n-1}.  In this, ai and bi are supposed to be the ith least significant digits of numbers A, B.  You are supposed to print the digits of the sum of A, B, again from least significant to the most significant - one per line. Note that the sum can have n+1 digits, and so you should print n+1 digits, with the most significant digit being possibly 0.You may assume that the size of an array can be a value that is known only during execution.  For example, it is OK to write 
"int n; cin >>n; int A[n];"

Sample Test Cases
Input Output
Test Case 1
3 2 1
6 5 4
9
7
5
0
Test Case 2
20
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
8
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
1


WRITE/COPY THESE CODE DIRECTLY IN NPTEL TERMINAL AND COMPILE AND RUN THEN SUBMIT.

PROGRAM:
#include<iostream>
using namespace std;
int main(){
   int n;
   cin>>n;
   int A[n], B[n];

   for (int i=0; i < n; ++i){
      cin>> A[i];
   } 
   
   for (int i=0; i < n; ++i){
      cin>> B[i];
   }
   
   int sum[n+1];
   sum[n] = 0; 
   
   int carry = 0; 
   int i=0;
   for (; i < n; ++i){
      int digitSum = A[i] + B[i] + carry;
          
      sum[i] = digitSum % 10;
      carry = digitSum / 10; 
   }
   
   if(carry != 0){
      sum[i] = carry;
   }

   
   for (int i=0; i < n+1; ++i){
      cout << sum[i]<<"\n";
      
         
   }
   
   return 0;

}

WEEK-7 :PROGRAMMING ASSIGNMENT 2 AND 3 
CLICK ON BELOW LINK.
<<CLICK HERE FOR  WEEK 7:PROGRAMMING -02>>

<<CLICK HERE FOR  WEEK 7: PROGRAMMING-03>> FOLLOW OUR WEBSITE FROM THE BUTTON PROVIDED TO THE BOTTOM OF PAGE TO GET SOON ANSWERS OF PROGRAMS.
Disclaimer: Here you can find all nptel assignment solutions related to CS stream.These may help you for your assignment.The answers are only for verification.You cannot copy these directly from the post(code of conduct of NPTEL).These is not 100%  correct solutions.

2 comments:

  1. Plz provide me problem solving through programming answerplz

    ReplyDelete
    Replies
    1. Problem solving through programming answer has been uploaded

      Delete

If you have any problems related to solutions or any concept please let me know.

Copyright (c) 2020 Custom Programs All Right Reserved

Pages