How do we write odd function in an if-else statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nhd
    New Member
    • Aug 2006
    • 24

    How do we write odd function in an if-else statement

    I'm facing some problems.. Its very basic but i cant seems to remember how to go abt doing it...

    Im supposed to input in a value
    if it is 1, then im supposed to stop the program,
    if n is odd, then n=n*3+1,
    else n=n/2

    What ive written is

    int n;

    cin>>n;

    if (n==1)
    {
    cout<<n<<endl;
    }

    else if (n==??) ---> I'm supposed to write this as 'if n is an odd number, answer will be as follows..
    {
    n = 3*n + 1;
    }

    else
    {
    n = n/2;
    }

    cout<<n<<endl;

    I;m not sure how do i do the ODD part.. Can anyone kindly help me in this.. Thank u..
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    If a number is odd, than it is not evenly divisible by 2 - in other words, it will have a remainder of 1. What operator can you use to check the remainder of a number divided by 2?

    Comment

    • Nhd
      New Member
      • Aug 2006
      • 24

      #3
      Originally posted by Ganon11
      If a number is odd, than it is not evenly divisible by 2 - in other words, it will have a remainder of 1. What operator can you use to check the remainder of a number divided by 2?

      is the the % operator? meaning i shud juz write it as

      if (n%2)
      {
      n=3*n+1;
      }

      ok, thk u so much, i get this already... i can go to the second part now... Thanks alot

      Comment

      • shardul316
        New Member
        • Jan 2007
        • 20

        #4
        for odd number you have to use
        if(n%2!=0)
        {


        n=3*n+1;

        }

        where n%2!=0 is the condition to check odd number.
        for even number it condition will be n%2==0.

        Comment

        • horace1
          Recognized Expert Top Contributor
          • Nov 2006
          • 1510

          #5
          Originally posted by shardul316
          for odd number you have to use
          if(n%2!=0)
          {

          n=3*n+1;
          }
          where n%2!=0 is the condition to check odd number.
          for even number it condition will be n%2==0.
          either
          Code:
          if(n%2!=0)
          which is true if the number is odd
          or
          Code:
          if(n%2)
          which is non zero if the number is odd will work because C/C++ treats a non zero value as true

          Comment

          • Nhd
            New Member
            • Aug 2006
            • 24

            #6
            Thanks shardul316 and horace1..

            Ive goten that part right already...

            However, may i noe how do we continuously continue this statement. i tried using a for loop, but it doesnt work..

            after i input in 22, 11 will be the output that i'll get.. but im expected to get this output instead -> 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1..

            which means that the output is continuously fed into the loop till i get 1, then it will terminate. is there anyone who might noe how to go abt starting this...

            for eg, 22 is even, hence 22/2 is 11.
            11 is odd, then 3*11+1 is 34.
            34 is even, 34/2 is 17 and it goes on...

            and how do we start counting cycle length?

            Comment

            • horace1
              Recognized Expert Top Contributor
              • Nov 2006
              • 1510

              #7
              try
              Code:
              			int n;
              			scanf("%d", &n);
              			while(n!=1)
              			if(n%2) 
              			   {
              				printf("odd %d ", n); 
              				n = 3*n + 1; 
              			   }
              			else  
              			   {
              			   printf("even %d ", n); 
              			   n = n/2; 
              			   }

              Comment

              Working...