Assignment 4
Assignment 4
1) Write a program, which finds the sum of last digits. (In above case: 3 + 0 + 7 + 2 + 2 = 14).?
Answer:-
#include <iostream>
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>
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>
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>
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>
int main() {
int n, sum = 0;
while(cin >> n) {
n /= 10;
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>
int main() {
int n, sum = 0;
while(cin >> n) {
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>
int main() {
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>
int main() {
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>
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>
int main() {
int n, count = 0;
while(cin >> n) {
if (n % 2 != 0) {
count++;
cout << "Number of odd numbers: " << count << endl;
return 0;