Is it possible to solve this problem in c++?
Write a program that counts the numbers from 3 to 117. But for multiples of three add 3 instead of 1 and for the multiples of five add 5 instead of 1. For numbers which are multiples of both three and five add 15 instead of 1. Ex: If we are looking at numbers 5 to 15 (inclusive), the program would output 39.

Recommended Answers

All 23 Replies

Yes, it is perfectly possible to solve that problem in C++.

You will need a for loop and an understanding of modulus, and a little bit of good old fashioned logic.

Good luck!

ok. I know how to do a for loop. I had to look up modulus but from what I read it's used for the remainder and I'm not sure how that would help.

Well if X % 3 is zero then you know X is divisible by 3.

when something is evenly divisible by a number the modulo is 0.

15 / 3 = 5
15 % 3 = 0

Knowing this you can write the following code to see if something is divisble by a number:

if(someNUmber % 3 == 0)
    the number is divisble by 3
if(someNumber % 5 == 0)
    the number is divisble by 3
if(someNumber % 3 == 0 && someNumber % 5 == 0)
    the number is divisble by 3 and 5

Thanks guys. I think I got it from here :)

I wrote a code but it's been a while since I've done any of this. I'm working in xcode and for some reason the "build and run" button is grayed out and I can't try the code to see what happens (google was not very helful in this area. Anyone know what I'm doing wrong or how to fix this (I tried restarting xcode already)?

how to count the given numbers in c++ ?

Ok, I have this code and I got it to run but it doesn't continue after asking for the second number.

//Write a program that counts the numbers from 3 to 117. 
//But for multiples of three add 3 instead of 1 
//and for the multiples of five add 5 instead of 1. 
//For numbers which are multiples of both three and five add 15 
//instead of 1. 
//Ex: If we are looking at numbers 5 to 15 (inclusive), 
//the program would output 39

#include<iostream>

using namespace std;

int main()
{
    int x,y;
    int sum=0;

    cout << "Choose starting number (from 3-117) " << endl;
    cin >> x;
    cout << "Choose ending number (from 3-117) " << endl;
    cin >> y;

    for (int i=0; x<=i<=y; i++) 
    {
        if (i%3==0) 
        {
            i==3;
        }
        if (i%5==0)
        {
            i==5;
        }
        if (i%3==0 && i%5==0)
        {
            i==15;
        }
        else 
        {
            i==1;
        }

        sum+=i;

    }

    cout << "The total output between " <<x<< " and " <<y<< " is " << sum;

    return 0;
}
x<=i<=y

Is not valid c++. If you need to loop between all number in x and y then you can use:

for (int i = x; i <= y; i++)

Wich is translated to: start with i equall to x and loop while i is less than or equall to y increment i by one with every iteration.

that helped the program finish but I'm not getting the right output :(
when I enter 5-15 it's giving me 110

I have the sum formula wrong. It's counting each number for it's value instead of the values I inputted for if/else

Is it working now? Also you shouldn't be changing the value of i. i should only be used for controlling the loop. You are adding 1,3,5 or 15 depending on what i is divisible by. The code should look like this:

if (i%3==0 && i%5==0)
    sum += ;
else if (i%3==0) 
    sum += ;
else if (i%5==0)
    sum += ;
else 
    sum += ;

I understand what you're saying about not changing y, but why are you using sum+=? Before when I tried getting the sum from 5-15 I was getting 110 which meant it was summing all those values, but now I'm getting 162-or 137 if I change the code to say 'else if' instead of just 'if'-which I assume means it's calculating something I'm just not sure what.

What did you plug in for the missing values and what does your for loop look like now? Remember when you are doing the summation that if the number is divisible by 15 you add 15, if it is divisible by 5 you add 5, if it is divisible by 3 then you add 3, otherwise you add only 1. when I run my version of the program for 5 to 15 I get 39.

@ivel
I looked at the code you have written and at NathanOliver's helpful suggestions. I came to the conclusion that you need beginner's help.

I took your code and corrected it, please study it:

/* modulus_exercise101.cpp
Write a program that counts the numbers from 3 to 117.
For multiples of three add 3 instead of 1
For the multiples of five add 5 instead of 1.
For numbers which are multiples of both three and five add 15
instead of 1.

Ex: If we are looking at numbers 5 to 15 (inclusive),
the program would output 39

compiled with mingw32-g++.exe on CodeBlocks IDE
*/

#include<iostream>

using namespace std;

int main()
{
    int x, y;
    int sum = 0;

    /*
    cout << "Choose starting number (from 3-117) " << endl;
    cin >> x;
    cout << "Choose ending number (from 3-117) " << endl;
    cin >> y;
    */
    // for testing only
    x = 5; y = 15;

    for (int i = x; i <= y; i++)
    {
        // do this conditional statement first
        if (i%3 == 0 && i%5 == 0)
        {
            sum += 15;
        }
        else if (i%3 == 0)
        {
            sum += 3;
        }
        else if (i%5 == 0)
        {
            sum += 5;
        }
        else
        {
            sum += 1;
        }
    }

    cout << "The total output between " <<x<< " and " <<y<< " is " << sum;

    return 0;
}

Your conditional if statements have to be in a certain order and you also have to use else if otherwise you get double evaluations.

Note that for instance sum += 3 behaves like sum = sum + 3. Also {} are optional if you only have a single statement. Hope this helps.

commented: Nice +15

Interesting.
@nathanoliver, I had 3, 5, and 15 entered as my values.
@sneekula, I had no idea that the if statements had to be in a certain order. What would the order be in general? I tried it the way you had it and my output was 149 so I thought maybe the order was supposed to be largest to smallest. I switched it and still got 149. So I compared my code to yours, line by line. Turns out I had an extra sum+=i at the end of my loop which is why I was getting the wrong answer. So that's all fixed now :)
But then I went back and switched the 3 and 5 again and it seems like they are interchangeable. So I'm just wondering what the general rule is about the order of if statments is.
Thanks so much for all your help guys.

Checking if the number is divisible by 3 or 5 can be switched around. The main thing is that you need to have the check for if it is divisible by 3 and 5 first otherwise the checks for 3 or 5 will trigger.

Yes, and why not just check for divisible by 15?

You have this execution/evaluation order:
1) if (i%3 == 0 && i%5 == 0)
2) else if (i%3 == 0)
3) else if (i%5 == 0)
4) else

Here 2) and 3) could be swapped

If 1) is true then the else conditions 2) 3) 4) will not be evaluated.
If 1) is false then 2) will be evaluated
If 2) is true 3) and 4) will be skipped
If 2) is false then 3) will be evaluated and so on

Let's say you had this evaluation order and i = 15:
1) if (i%3 == 0)
2) else if (i%3 == 0 && i%5 == 0)
3) else if (i%5 == 0)
4) else
then 1) would be true and you would never reach 2)

Doing pseudo code on a piece of paper really helps.

Now I understand it. Thank you so much.

write a proram to accept Employee Number, Employee Name, and his Salary in a structure?

@ Nimra_1

1) Dont post your own question in a different thread.
2) We dont do homework for others
3) Read the Rules

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.