Convert this Java code to C++: public class Problem5 { public static void main(String[] args) { //loop variables int i = 0; int num = 0; int num_count = 0; //Empty String to store prime numbers String primeNumbers = ""; //First for loop which runs from 1 to 10000 for (i = 1; i <= 10000; i++) { int counter = 0; //second for loop which runs from selected number to 1 for (num = i; num >= 1; num--) { //using modulus operator to check if selected number is divisible by any number from num(i) to 1 if (i % num == 0) { counter = counter + 1; //count the divisible } } //if counter is 2 then it is prime number(divisible by themselves and one (1)) if (counter == 2) { //Appended the Prime number to the String primeNumbers = primeNumbers + i + " "; num_count++; //arrange numbers, 50 numbers per line if (num_count == 30) { primeNumbers += "\n"; num_count = 0; } } } //display prime numbers System.out.println("Prime numbers from 1 to 10000: "); System.out.println(primeNumbers); }}
Convert this Java code to C++:
public class Problem5 {
public static void main(String[] args) {
//loop variables
int i = 0;
int num = 0;
int num_count = 0;
//Empty String to store prime numbers
String primeNumbers = "";
//First for loop which runs from 1 to 10000
for (i = 1; i <= 10000; i++) {
int counter = 0;
//second for loop which runs from selected number to 1
for (num = i; num >= 1; num--) {
//using modulus operator to check if selected number is divisible by any number from num(i) to 1
if (i % num == 0) {
counter = counter + 1; //count the divisible
}
}
//if counter is 2 then it is prime number(divisible by themselves and one (1))
if (counter == 2) {
//Appended the Prime number to the String
primeNumbers = primeNumbers + i + " ";
num_count++;
//arrange numbers, 50 numbers per line
if (num_count == 30) {
primeNumbers += "\n";
num_count = 0;
}
}
}
//display prime numbers
System.out.println("Prime numbers from 1 to 10000: ");
System.out.println(primeNumbers);
}
}
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 2 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)