using c++ Write a code fragment that is the same as #include using namespace std; int main() { int n, sum = 0; cout << ("please enter a positive integer") << endl; cin >> n; for (int j = 0; j <= n; j++) { for (int k = 0; k <= n; k++) { sum += j + k; } } cout << sum << endl; } but uses “while” loops, rather than “for” loops.
using c++ Write a code fragment that is the same as
#include <iostream>
using namespace std;
int main() {
int n, sum = 0;
cout << ("please enter a positive integer") << endl;
cin >> n;
for (int j = 0; j <= n; j++) {
for (int k = 0; k <= n; k++) {
sum += j + k;
}
}
cout << sum << endl;
}
but uses “while” loops, rather than “for” loops.

code-
#include <iostream>
using namespace std;
int main() {
int n, sum = 0;
cout << ("please enter a positive integer") << endl;
cin >> n;
int j=0;
while(j<=n){
int k=0;
while( k <= n){
sum += j + k;
k++;
}
j++;
}
cout << sum << endl;
}
Step by step
Solved in 2 steps with 1 images









