1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
wolverine [178]
3 years ago
10

Write program fragments (i.e., you do not need to write complete programs) with loops that compute: A)The sum of all even number

s 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.)
Computers and Technology
1 answer:
miss Akunina [59]3 years ago
4 0

// 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.

You might be interested in
Please help! first one to answer correctly gets brainliest and thanked
jonny [76]
It’s either presentation Because the software is used to create a sequence of texting, graphics and often audio and video to accompany a speech or a public presentation, or letter
6 0
3 years ago
5 efectos del desarrollo tecnológico en el medio ambiente
sineoko [7]

Answer:

english plzzz

Explanation:

5 0
3 years ago
What does anyone who chooses to use speech recognition software need to do?
skelet666 [1.2K]
D I believe. I’m not %100 percent sure.
4 0
3 years ago
Explain in brief terms some of the technology and developments that were important in the history and development of the Interne
igomit [66]

Answer: The Internet started in the 1960s as a way for government researchers to share information. ... This eventually led to the formation of the ARPANET (Advanced Research Projects Agency Network), the network that ultimately evolved into what we now know as the Internet.

Have a nice day ahead :)

7 0
3 years ago
How to add a while loop? here's my code
Jet001 [13]

To add while loop in python language, the use of syntax is to be brought into the effect.

<h3>What is a while loop?</h3>

A while loop can be referred to as an expression of code-blocks being true to their cores. Until the blocks of the code are not proved to be true, the loop continues.

In order to add such while loop to your code, one needs to use the syntax function in python to prove the true and correctness of the code blocks.

Hence, the procedure to add while loop has been aforementioned.

Learn more about a while loop here:

brainly.com/question/15690925

#SPJ1

3 0
3 years ago
Other questions:
  • 10 10 105 Each process is assigned a numerical priority, with a higher number indicating a higher relative priority. In addition
    8·1 answer
  • A private local network, accessible only to an organization's staff, is known as an:
    12·2 answers
  • Within the hardware of the personal computer temporary memory is known as
    10·2 answers
  • How do I attach a file on the computer?
    13·1 answer
  • 5. The stage of engine operation when both the intake and exhaust valves are closed is the _______ stage.
    5·1 answer
  • How can you tell that a spreadsheet was saved as a 2007 Excel file?
    7·1 answer
  • Help me with this, please. Are vacuum cleaners, Cd players, and telephones considered computers? Do they store any data or proce
    8·2 answers
  • Write a function definition for a function which takes one parameter and returns twice that parameter
    12·1 answer
  • Primary functions of lighting are sufficient light to...
    13·1 answer
  • Which device allows you to convert sounds into digital format to be used on the computer​
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!