Simple C++ assistance needed!!!

I need to write a program that uses WHILE loops to preform said tasks:

1. Prompt user to input two integers, second has to be bigger then first
Did that!

2. Output all the numbers that are multiple of 3 and then their sum, in the set range.
I can find the multiples but I don't know how to get the sum of them all.

3. Output the sum of the square of the odd numbers between the set range
No idea how to get them to add.

4. Output the prime numbers within the set range.
I have an idea of what to do but I'd love ideas.

#include <iostream>
using namespace std;

void main()
{

int counter1 = 0;
int counter2 = 0;
int counter3 = 0;
int num1 = 0;
int num2 = 0;

start:

cout << "Please enter 2 integers, the first bigger then the second!" << endl;
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;

if(num1 > num2)
{
cout << "Your second number is bigger then your first!!!" << endl;
cout << endl;
goto start;

}else if(num1 < num2)
{
counter1 = num1;

while(counter1 != (num2 + 1)) //Find multiples of 3
{
if(counter1 % 3 == 0)
{
cout << counter1 << ", ";
counter1++;
}else if(counter1 % 3 != 0)
{
counter1++;
}

} //end of first while

counter2 = num1;

while(counter2 != (num2 + 1)) //sum of squares, of odd numbers
{

if(counter2 % 2 != 0)
{
sqrt(counter2);

}else if(counter2 % 2 == 0)
{
counter2++
}



}





}


system("pause");
}
closed account (o3hC5Di1)
Hi there,

Welcome to the forums. As for calculating the sum of the multiples of 3:

1
2
3
4
5
6
7
8
9
10
11
12
13
int multiple_three_sum = 0; //declare a variable to hold the sum

while(counter1 != (num2 + 1)) //Find multiples of 3
{
if(counter1 % 3 == 0)
{
cout << counter1 << ", ";
multiple_three_sum += counter1; //increase the sum with current the value of counter1
counter1++;
}else if(counter1 % 3 != 0)
{
counter1++;
}


Point 3 can be solved in a similar way.
As for prime numbers, the question has been asked quite a few times before: https://www.google.com/search?q=c%2B%2B+find+prime+numbers+site:www.cplusplus.com

On a sidenote, please wrap your code in [code][/code]-tags, it makes it much more readable.
Please do let us know if you require any further help.

All the best,
NwN
Thank you sir!
You just saved me so much time, I was over thinking it so much.
Again thank you :)

Edit: Yes I will put my code in the header and footer provided :)
Last edited on
Topic archived. No new replies allowed.