Display all numbers that are multiples of 5

The problem =

Write a C++ program that asks the user to enter an integer (n). Check to see if the integer is a positive number. If not, give an error. Display all numbers that are multiples of 5 (evenly divisible by 5) between 1 and n and display the values to the user. Make sure the output is neatly indented and clear to read.

i have my book with me currently but since our teacher decides not to use the book i am not sure where to start looking.

now i know i have to use a loop , i have chosen to use while since the others are not as straight forward to me yet.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>

using namespace std;

int main()
{
    double n, i;
     i-0;
   
    cout << " please enter a positive number" << endl;
    cin >> n;

    if(n>0)
    {

    while(i<=n,)   //this is where i get stuck i am not sure how to proceed


    }

    else
    {

    cout << " Error, The Number You Have Entered " << n << " Is Not A Positive" << endl;


    }


    return 0;
}






any tips on how to proceed would be appreciated i dont even mind not getting the answer right off the bat i am here to learn and practice until i get it right

//cheers!
Last edited on
correction



while( n>=i,) 

It should be something like this (the loop, that is)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

int i = 0;
int mod;

for(i; i<=n;i++){

mod = n%i;

if(mod = 0){

cout<<mod;
}



}

1
2
3
4
5
6
7
8
9
#include <iostream>
int main(void) {
     const int n = 30;
     for(int i = 0; i <= n; ++i) {
          if(i % 5 == 0)
               std::cout << i << ' ';
     }
     return 0;
}

Use the modulus operator.
Last edited on
not sure what modulus operator is... but but...i think i am close give me a few. more hints would be awesome.
The code I posted does exactly what you want. Am I missing something?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>

using namespace std;

int main()
{
    double n, i,x;
    i=0;
    cout << " please enter a positive number" << endl;
    cin >> n;

    if(n>=0)
    {

    for(i; i<=n; ++i)
        {
        x=(i%n);
            
            if(x =0);
            {
            cout << x << " ";
            }

        }


    }

    else
    {
    cout << " Error, The Number You Have Entered " << n << " Is Not A Positive" << endl;
    }


    return 0;
}




ok i know i am close....
probably is the knowledge to put it into effect
ive been at this for awhile can anyone correct me? im getting decimals


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <math.h>


using namespace std;

int main()
{
    double   n, result;

    cout << " please enter a positive number" << endl;
    cin >> n;

    if(n>=0)
    {


    for( int   i=0; i<=n; ++i)

        {
            result=   n/i;

          if   (result<n)

            {
           cout << result << " ";
            }

        }

    }

    else
    {
    cout << " Error, The Number You Have Entered " << n << " Is Not A Positive" << endl;
    }

    return 0;
}

> Display all numbers that are multiples of 5 (evenly divisible by 5) between 1 and n

for( int i = 5 ; i < n ; i += 5 ) std::cout << i << '\n' ;
The modulus operator(%) returns the remainder of a number divided by another.
e.g 2%2 = 0
2%3 = 1
I'd agree that i += 5 makes the code both simpler and more efficient.

You could use that inside a while loop or as part of a for loop as already presented above.
Last edited on
2%3 = 1

i guess it should be 2

@xavega:
1
2
3
result=   n/i;

if   (result<n)

do you think that there's a chance result is greater than n?


im getting decimals

of course, since you've been doin this:

 
result=   n/i;
I'm a newb to C++ and I was just curious if I could knock this out. Here's what I threw together. Tell me what you guys think. It seems to work, but there may or may not be a better way to do this. I don't know how to use the for (int i = 5; i < n; i+=5) , so I did what I could without it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <math.h>

using namespace std;

int main()
{
        int n				        = 0;
	double hold			= 0.0;

    cout << "Please enter a positive number: ";
    cin >> n;

	if (n >= 0)
	{
		hold = n % 5;
	}
	else
	{
		cout << "The number you have entered is not valid" << endl;
		return 0;
	}
	// end if

	if (hold != 0)
	{
		cout << "The number you entered is not divisable by 5" << endl;
		return 0;
	}
	else
	{
		n += 5;
		while (n > 0)
		{
			n -= 5;
			cout << n << " ";
		}	// end while
	}

	cout << endl;
    return 0;
}
Last edited on
Topic archived. No new replies allowed.