C++ Programming: From Problem Analysis to Program Design
C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN: 9781337102087
Author: D. S. Malik
Publisher: Cengage Learning
Expert Solution & Answer
Book Icon
Chapter 5, Problem 40SA

Explanation of Solution

The output of the above segment using for loop is “value=479”.

Below is the code converted into while loop with same output “value=479”.

#include <iostream>

using namespace std;

int main()

{

   int i=0; //initialize the value of i here

   int value = 3;

   while(i<5) //while loop replacing for loop

   {

    value = value * (i + 1) + i;

    i++; // increment the value of i inside the loop

   }

   cout << “value = “ << value << endl;

   return 0;

}

Below is the code converted into do while loop with same output “value=479”...

Blurred answer
Students have asked these similar questions
2- Write a function sumsteps2 that calculates and returns the sum of 1 to n in steps of 2, where n is an argument passed to the function. For example, if 11 is passed, it will return 1 + 3 + 5 + 7+ 9 + 11. Do this using a for loop. Calling the function will look like this: >> sumsteps2 (11) ans = 36
2- How many times it's a while loop should be printed? {int i = 1; i=i - 1; while(i) { cout<<"its a while loop"; i++; }} [1
1. Rewrite the program segment that follows, using a for loop and do-while loop: = 1; product i = 0; %3D while (i < n) { scanf(“%d", &a); if (a != i) %3D product *= a; ++i; }
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning