# An Introduction to Programming Through C++ | NPTEL 2022 | Week 3 Answers

This set of MCQ(multiple choice questions) focuses on the An Introduction to Programming Through C++ NPTEL 2022 Week 3 Answers.

### Course layout

NOTE: You can check your answer immediately by clicking show answer button. An Introduction to Programming Through C++ NPTEL 2022 Week 3 Assignment Answers” contains 10 questions.

Now, start attempting the quiz.

### An Introduction to Programming Through C++ NPTEL 2022 Week 3 Assignment Answers

``````int count=0;

for(int i=1; i<=5; i++){

for(int j=1; j<=5; j+=i){

for(int k=1; k<=5; k+=j){

count++;

}

}

}``````

Q1. What is the value of variable count at the end of execution?

A certain government levies taxes on a person’s total annual income. The taxes are calculated in the given manner.
If a person earns an amount of less than or equal to Rs. 1,00,000 annually, the person doesn’t pay any tax.
If a person earns an amount of more than Rs. 1,00,000 and less than or equal to Rs. 5,00,000, then the person is charged 5% on the amount.
If a person earns an amount of more than Rs. 5,00,000, the person is charged with 5% of Rs. 5,00,000 and 10% of the remaining amount.

The following code snippet computes the total tax to be paid by a person given their annual income. You are to fill in the blanks so that the code computes the tax correctly.

``````int income;

cin>>income;

double tax;

if(income<=blank1){

tax=0;

}

else if(income<blank2){

tax=blank3*income

}

else{

tax=0.05*500000+0.10*(blank4)

}

cout<<tax<<endl;``````

Q2. What should be filled in blank1?

a) 0
b) 100000
c) 500000
d) 1000000

Q3. What should be filled in blank2?

a) 0
b) 100000
c) 500000
d) 1000000

Q4. What should be filled in blank3?

a) 0
b) 0.05
c) 0.1
d) 0.5

Q5. What should be filled in blank4?

a) income
b) income-100000
c) income-500000
d) Income+100000

An Introduction to Programming Through C++ NPTEL Week 3 Assignment Answers

Given below is a program fragment that is meant to calculate the average of all odd numbers given in the input. You are to fill in the blanks so that the code computes the average correctly.

``````int sum=0;

int count=0;

for(int i=0; i<n; i++){

int num;

cin>>num;

if(num%2==0){

blank5;

}

blank6;

blank7;

}

cout<<sum/count<<endl;``````

Q6. What should be filled in blank5?

a) break
b) continue
c) sum+=num
d) return 0

Q7. What should be filled in blank6?

a) continue
b) break
c) sum+=num
d) sum*=num

An Introduction to Programming Through C++ NPTEL Week 3 Assignment Answers

Q8. What should be filled in blank7?

a) count+=1
b) count-=1
c) count+=num
d) count+=2

The program given below is meant to read an unending sequence of non-negative numbers.  This sequence may increase and decrease several times.  We are interested in the last increasing portion, particularly its length.  So having read a number, the program prints out the length of the last increasing portion.  Suppose the sequence is 1, 4, 6, 2, 5, …  Then having read 1, 4, 6 the program will print 3, because at this point the entire sequence forms the increasing portion.  On the other hand, having read 1, 4, 6, 2, 5 the program should print 2 because the last increasing portion is 2, 5, of length 2.  You can see that the program will have to print out 1, 2, 3, 1, 2 and so on if the input was 1, 4, 6, 2, 5, ….  You are to answer the questions below in order to fill in the blanks so that the program does its job.

``````int lastSeqLength=0; // stores the current length of sequence which is continuously increasing

int last=-1;

while(true){

int num;

cin>>num;

if(num>last){

blank8;

}

else{

blank9;

}

last=num;

cout<<lastSeqLength<<endl;

}``````

Q9. What should be filled in blank8?

a) lastSeqLength=0
b) lastSeqLength=1
c) lastSeqLength+=1
d) lastSeqLength-=1

Q10. What should be filled in blank9?

a) lastSeqLength=1
b) lastSeqLength=0
c)lastSeqLength+=1
d) lastSeqLength-=1

An Introduction to Programming Through C++ NPTEL week 3 Assignment Solutions

### Programming Assignment 3.1

You are currently at the origin (0, 0) and will be given commands to either go Right (R), Left (L), Up (U) or Down (D) by a certain number of steps. At the end of all these commands, you will be signaled to stop by reading the character ‘E’, after which you need to output your position in the x-y plane. The four kinds of movements are the following (direction followed by number of steps in that direction):

R number_of_steps : You need to increase your x-coordinate by “number_of_steps”.
L number_of_steps : You need to decrease your x-coordinate by “number_of_steps”.
U number_of_steps : You need to increase your y-coordinate by “number_of_steps”.
D number_of_steps : You need to decrease your y-coordinate by “number_of_steps”.

INPUT:
Direction number_of_steps (a character and integer separated by a space)
.
.

E (command to stop)

OUTPUT:
x y

CODE:

``````int main() {
int x = 0, y = 0, steps;
char ch;
cin >> ch >> steps;
while(ch != 'E') {
switch(ch) {
case 'R': x = x + steps;
break;
case 'L': x = x - steps;
break;
case 'U': y = y + steps;
break;
case 'D': y = y - steps;
break;
default: break;
}
cin >> ch >> steps;
}
cout << x << " " << y << endl;
}``````

### Programming Assignment 3.2

Write a program to find the sum of the digits of a given integer N.

Solving this problem is easy manually if the number is given to you on paper: you simply see the digits written down and you can add them up.  A computer program does not “see” the digits.  However, you can get the least significant digit by taking the remainder modulo 10, using the % operator.  You can determine the number resulting from erasing the least significant digit of an integer x by dividing x by 10.  As you know x/10 will equal the quotient, which is exactly what remains if you erase the last digit of x.  If you now take the remainder of this modulo 10 you will get the second least significant digit.  Thus by alternately taking the remainder modulo 10 and dividing by 10 you will be able to obtain the different digits.  So then you can add them up.  All that remains is to put this into a nice loop.

INPUT:
N (1<=N<=1000000000).  The upper bound for N is set at 109 so that the numbers will fit in standard int.

OUTPUT:
Sum of digits of N

CODE:

``````int main() {
int n, sum = 0;
cin >> n;
while(n > 0) {
sum = sum + n % 10;
n = n / 10;
}
cout << sum << endl;
}``````

<< Prev- An Introduction to Programming Through C++ Week 2 Solutions

>> Next- An Introduction to Programming Through C++ Week 4 Solutions

`DISCLAIMER: Use these answers only for the reference purpose. Quizermania doesn't claim these answers to be 100% correct. So, make sure you submit your assignments on the basis of your knowledge.`

Programming in Java NPTEL week 1 quiz answers

Nptel – Deep Learning assignment solutions