0% found this document useful (0 votes)
13 views

Assignment 4

The document provides solutions to 10 programming problems involving loops and conditionals. The problems involve tasks like finding sums of digits, weighted sums, last even numbers, and counting odd numbers from user input. C++ code snippets are given as answers to each problem.

Uploaded by

yash.verma.21cse
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Assignment 4

The document provides solutions to 10 programming problems involving loops and conditionals. The problems involve tasks like finding sums of digits, weighted sums, last even numbers, and counting odd numbers from user input. C++ code snippets are given as answers to each problem.

Uploaded by

yash.verma.21cse
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignment 4 (Simple Loop)

1) Write a program, which finds the sum of last digits. (In above case: 3 + 0 + 7 + 2 + 2 = 14).?

Answer:-

#include <iostream>

using namespace std;

int main() {

int n, sum = 0;

while(cin >> n) {

sum += n % 10;

cout << "Sum of last digits: " << sum << endl;

return 0;

2) Write a program, which finds the sum of second last digits. (In above case: 3 + 0 + 7 + 4 +1 = 15)?

Answer:-

#include <iostream>

using namespace std;

int main() {

int n, sum = 0;

while(cin >> n) {

n /= 10;

sum += n % 10;

cout << "Sum of second last digits: " << sum << endl;
return 0;

3) Write a program to find the sum of numbers after deleting last digit. (In above case: 3 + 10 +7 + 4 +
1 = 25)?

Answer:-

#include <iostream>

using namespace std;

int main() {

int n, sum = 0;

while(cin >> n) {

sum += n / 10;

cout << "Sum after deleting last digit: " << sum << endl;

return 0;

4) Write a program to find the sum of numbers after deleting second last digit. (In above case: 3 + 10
+ 7 + 2 + 2 = 24)?

Answer:-

#include <iostream>

using namespace std;

int main() {

int n, sum = 0;

while(cin >> n) {

n /= 100;

sum += n * 10;

cout << "Sum after deleting second last digit: " << sum << endl;
return 0;

5) Write a program, which finds the sum of the product of last two digits (In above case:3 × 3 + 0 × 0 +
7 × 7 + 4 × 2 + 1 × 2 = 68). If n = 3 and numbers are 137, 41593, and 89 then output is (3 × 7 + 9 × 3 + 8 ×
9 = 120).

Answer:-

#include <iostream>

using namespace std;

int main() {

int n, sum = 0;

while(cin >> n) {

int last_digit = n % 10;

n /= 10;

int second_last_digit = n % 10;

sum += last_digit * second_last_digit;

cout << "Sum of product of last two digits: " << sum << endl;

return 0;

6) Write a program, which finds the sum of numbers after exchanging last two digits. (In above case:
33 + 100 + 77 + 24 + 21 = 255).

Answer:-

#include <iostream>

using namespace std;

int main() {

int n, sum = 0;
while(cin >> n) {

int last_digit = n % 10;

n /= 10;

int second_last_digit = n % 10;

n = (n * 10) + last_digit;

n = (n * 10) + second_last_digit;

sum += n;

cout << "Sum after exchanging last two digits: " << sum << endl;

return 0;

7) Write a program, which finds the last even number (In above case: 12). If n = 6 and numbers are 14,
17, 16, 19, 21, and 33 then output is 16.

Answer:-

#include <iostream>

using namespace std;

int main() {

int n, last_even = -1;

while(cin >> n) {

if (n % 2 == 0) {

last_even = n;

cout << "Last even number: " << last_even << endl;

return 0;

}
8) Write a program, which finds the weighted sum of these numbers. The weight of ith number is i.
(In above case: 1 × 33 + 2 × 100 + 3 × 77 + 4 × 42 + 5 × 12 = 692).

Answer:-

#include <iostream>

using namespace std;

int main() {

int n, sum = 0, weight = 1;

while(cin >> n) {

sum += n * weight;

weight++;

cout << "Weighted sum of numbers: " << sum << endl;

return 0;

9) Write a program, which finds the sum of odd numbers. (In above case: 33 + 77 = 110).

Answer:-

#include <iostream>

using namespace std;

int main() {

int n, sum = 0;

while(cin >> n) {

if (n % 2 != 0) {

sum += n;

cout << "Sum of odd numbers: " << sum << endl;
return 0;

10) Write a program, which finds how many of these numbers are odd. (In above case: 2).

Answer:-

#include <iostream>

using namespace std;

int main() {

int n, count = 0;

while(cin >> n) {

if (n % 2 != 0) {

count++;

cout << "Number of odd numbers: " << count << endl;

return 0;

You might also like