81.4k views
4 votes
Write program fragments (i.e., you do not need to write complete programs) with loops that compute: A)The sum of all even numbers between 2 and 100 (inclusive). B)The sum of all squares between 1 and 100 (inclusive). C)All powers of 2 from 2^0 up to 2^20 (inclusive). D)The sum of all odd numbers between a and b (inclusive), where a and b are integer variables with a ≤ b. E)The sum of all digits at odd positions (right-to-left starting at 1 as the right-most digit) of a numeric input. (For example, if the input is 432677, the sum would be 7 + 6 + 3 = 16.) F)The sum of all digits at odd positions (left-to-right starting at 1 as the left-most digit) of a numeric input. (For example, if the input is 432677, the sum would be 4 + 2 + 7 = 13.)

1 Answer

7 votes

// Programs in C++

A)

Answer:

int i = 2;

int n = 100;

int sum = 0;

while (i <= n)

{

if (i % 2 == 0)

{

sum += i;

}

i++;

}

cout << sum;

Explanation:

First of all, run a while loop until i is less than or equal to 100. Then inside the while loop, check if i variable has an even value and then add it the sum variable and keep on increasing the i variable which is acting as a counter. At last, print the sum.

B)

Answer:

int i = 2;

int n = 100;

int square;

int sum = 0;

while (i <sqrt(n))

{

square = i*i;

sum += square;

i++;

}

cout << sum;

Explanation:

Run a while loop until i is less than or equal to square root of 100. Then inside the while loop, square the i variable, and then add it the sum variable and keep on increasing the i variable which is acting as a counter. At last, print the sum.

C)

Answer:

int i = 0;

int n = 20;

int square;

int sum = 0;

while (i <=n)

{

sum += pow(2, i);

i++;

}

cout << sum;

Explanation:

Run a while loop until i is less than or equal to 20. Then inside the while loop, take the power of 2 up to the i variable, and then add it the sum variable and keep on increasing the i variable which is acting as a counter. At last, print the sum.

D)

Answer:

int a;

int b;

int sum = 0;

cin >> a;

cin >> b;

while (a <= b)

{

if (a % 2 != 0)

{

sum += a;

}

a++;

}

cout << sum;

Explanation:

Take inputs from user in variables a and b. Run a while loop until a is less than or equal to b. Then inside the while loop, check if a is not an even number and then add it the sum variable and keep on increasing the a variable which is acting as a counter. At last, print the sum.

E)

Answer:

int number;

cout << "Enter a number: ";

cin >> number;

int odd_digits_sum = 0;

while (number > 0)

{

int digit = number % 10;

odd_digits_sum += digit;

number /= 100;

}

cout << "Sum of all digits at odd digits: " << odd_digits_sum;

Explanation:

Run a while loop until the number entered by user is greater than 0. Inside the while loop, calculate the value of digit by taking a mod of the number variable with 10.

After that sum those digits and then divide the number by 100 and assign this new value to number variable. Print the sum of digits stored in the sum variable at the end.

F)

Answer:

int number;

cout << "Enter a number: ";

cin >> number;

int length = to_string(number).length();

int sum = 0;

for (int i=1; i <= length; i++)

{

//Add odd place digit starting from left.

if (i % 2 != 0)

{

//Read left most digit

int digit = floor(number / pow(10, length - i));

sum += digit;

int l = pow(10, (length - i));

number = number % l;

}

//Remove even place digit

else

{

int digit = floor(number / pow(10, length - i));

int l = pow(10, (length - i));

number = number % l;

}

}

cout << "Sum: " << sum << endl;

Explanation:

Run a for loop up to the length variable. Add odd place digit starting from left by checking if i variable does not have an even value.

If the condition is true, read the left most digit and add it the sum variable. Otherwise remove even place digit. Print the sum of digits stored in the sum variable at the end.

User Peter Pohlmann
by
3.5k points