Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
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,357 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