Question

In: Computer Science

(C++)Change the following loop to a while loop: int i; for (i=0; i<10; i++) {    ...

(C++)Change the following loop to a while loop:

int i;
for (i=0; i<10; i++)
{
    cout<<i<<endl;
}

Solutions

Expert Solution

Dear Student ,

As per the requirement submitted above , kindly find the below solution.

Here a new c++ program with name "main.cpp" is created, which contains following code.

main.cpp :

//header files
#include <iostream>
using namespace std;
//main method
int main()
{
int i;//declaring variable
i=0;//initilizing variable
//using while loop
while(i<10)
{
cout<<i<<endl;//print value of i
i++;//increment i
}
return 0;
}

======================================================

Output : Compile and Run main.cpp to get the screen as shown below

Screen 1 :main.cpp

NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.


Related Solutions

How many times will the following while loop iterate? int i = 1; while (i <...
How many times will the following while loop iterate? int i = 1; while (i < 5) {     i = i + 1;     System.out.println(“Hello!”); } Group of answer choices 4 0 5 It will iterate infinitely
Please code C# Convert the following for loop into a while loop: for(int count = 8;...
Please code C# Convert the following for loop into a while loop: for(int count = 8; count > 0; count--) { Console.WriteLine(count); }
Here is a traditional loop in C:   while (A{i} == 0)                A{i} = A{5} + A{i+3};...
Here is a traditional loop in C:   while (A{i} == 0)                A{i} = A{5} + A{i+3}; (NOTE: please consider braces {} here as usual square brackets for array index). Assume that i is stored in register $s1, and the base address of the array A is in $s2. Fill in the multiple blank spaces in the following MIPS program that is supposed to be compiled from the above C loop: loop: sll $t0, $s1,                add $t0, $t0,                lw $t1, 20($s2)...
int f2 (int n) j = 0; while (j <n) {for (int i = 0; i...
int f2 (int n) j = 0; while (j <n) {for (int i = 0; i <n; ++ i) {cout << "j =" + j; j = j + 5; }}
#include <iostream> using namespace std; int main() { int even=0,odd=0,sum=0,sum2=0,largest,smallest; int num,i; for ( i=1; i<=10;...
#include <iostream> using namespace std; int main() { int even=0,odd=0,sum=0,sum2=0,largest,smallest; int num,i; for ( i=1; i<=10; i++){ cout << " Enter " << i << " number: "; cin >> num; if ( num%2==0){ even++; sum+=num; } else { odd++; sum2+=num; if(num>largest){ largest = num; } if(num<largest) { smallest = num; } } cout << " The sum of even number is : " << sum << endl; cout << " The total-count of even number is : " <<...
What is the ouput of the following code? void loop(int num) { for(int i = 1;...
What is the ouput of the following code? void loop(int num) { for(int i = 1; i < num; ++i) { for(int j = 0; j < 5; ++j) { cout << j; } } } int main() { loop(3); return 0; }
REWRITE FOLLOWING CODES USING DO...WHILE LOOP. BY USING C LANGUAGE #include <stdio.h> int main(void) {     ...
REWRITE FOLLOWING CODES USING DO...WHILE LOOP. BY USING C LANGUAGE #include <stdio.h> int main(void) {      int count =2; // COUNT TAKEN 2 AS TO PRINT 2 TIMES           while(count--){                printf("\--------------------------------------------\ \n"); printf("\          BBBBB               A                   \ \n"); printf("\          B    B             A A                  \ \n"); printf("\          BBBB              A   A                 \ \n"); printf("\          B    B           AAAAAAA                \ \n"); printf("\          BBBBB           A       A               \ \n"); printf("\---------------------------------------------\ \n");             }                                 return 0; }
Show the output of the following code segment. int count=0;                         for (int i=2; i <=...
Show the output of the following code segment. int count=0;                         for (int i=2; i <= 4; i++ ) {                                     StdOut.println(i);                                     for (int j=1; j <3; j++) {                                                 count++;                                                 StdOut.println(i +" " + j +" "+ count);                                     }                         } count i j I print 0 2 1 3 1 1 1 2 3 2 2 3 3 3 3 4 3 Show the output of the function call statements shown.             double x =...
Translate the following C++ code to Pseudocode: int main() { stack<char> stk,stk2; int length =0,ele; while(cin>>ele)...
Translate the following C++ code to Pseudocode: int main() { stack<char> stk,stk2; int length =0,ele; while(cin>>ele) { stk.push(ele); length++; } if(length%2) stk.pop(); for (int i=0;i<length/2;i++) { ele=stk.top(); stk.pop(); stk2.push(ele); } int flag=1; for(int i=0;i<length/2;i++) { if(stk.top()==stk2.top()) { stk.pop();stk2.pop(); } else { flag=1; break; } } if(flag==1) cout<<"NOT palindrome"; else cout<<"palindrome"; }
14. How many times does "#" print? for(int i = 0; i < 10; ++i) {...
14. How many times does "#" print? for(int i = 0; i < 10; ++i) { if(i == 2 || i==4 || i==6) { continue; } cout << "#"; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT