Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

Divisible or not, nested if statements


  • Please log in to reply

#1
willmon2000

willmon2000

    Member

  • Member
  • PipPipPip
  • 215 posts
hello every one im trying to write a c++ program where it can check if a number is divisible by 5 and 10. if so it needs to print out number is divisible by 10; same case if it is not divisible by it. Here is part of what i have so far but it is still fruit less.
#include <iostream>



using namespace std;



int divisibleBy  (int x) {

if (x == 1) {

cout << x << "is divisible by five!" << endl;

} else {

divisibleBy  (x-5/5);

    if (x!=1){

cout<<"x is not diviible by 5"<< endl;

}



if (x==1){

cout <<"x is divisible by 10"<< endl;

}

 else if (x!=1){

cout<<"x is not divisible by 10" << endl;

}

return 0;

}

}





int main()

{

    int x;

    cout << "Please enter a number: " << endl;

    cin >> x;

    divisibleBy(x);

    return 0;

}


  • 0

Advertisements


#2
Spike

Spike

    nOoB

  • Member
  • PipPipPipPip
  • 1,355 posts
Hey there willmon2000,

I don't seem to understand your logic behind your "divisibleBy" function where you are comparing your "x" variable to "1" and your "x-5/5". I think what you are trying to do there is look for a remainder of a certain number when divided by another number. The best way to achieve this is by using the modulus operator to return back a remainder rather than just dividing the number. I have also neatened up your function and made it a void function since you don't seem to be returning anything of importance, here is the revised code:

#include <iostream>

using namespace std;

void divisibleBy(int x);

int main()
{
    int x;
    cout << "Please enter a number:" << endl;
    cin >> x;
    divisibleBy(x);
    return 0;
}

void divisibleBy(int x)
{
    if (x % 5 > 0)
        cout << x << " is not divisible by five!" << endl;
    else
        cout << x << " is divisible by five!" << endl;

    if (x % 10 > 0)
        cout << x << " is not divisible by ten!" << endl;
    else
        cout << x << " is divisible by ten!" << endl;
}
Please let me know if this was what you were looking for and if there is anything else you need ;)

Peace Out :cool:
  • 0

#3
willmon2000

willmon2000

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 215 posts
.

Edited by willmon2000, 24 February 2012 - 04:25 PM.

  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP