Thread: I am stuck I need help...

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Germany
    Posts
    3

    Question I am stuck I need help...

    Hi everybody,

    i´m new in programming and doing a self study in C prgramming, i dont really find it easy, i came a cross a problem which i cant solve ...can anybody explain how i go abt the following programm pls;

    Read a positive integer value, and compute the following sequence: If
    the number is even, halve it; if it's odd, multiply by 3 and add 1. Repeat
    this process until the value is 1, printing out each value. Finally print
    out how many of these operations performed?
    how do i solve this problem, using the following statements;

    1- If statement
    2- While Statement
    3- For statement

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What have you tried?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    Hi!!
    That is The maximum cycle length problem, it is a classic!!
    look it up in the net, find algorithms and try to follow using C, it is not that hard..

  4. #4
    Registered User
    Join Date
    May 2008
    Location
    Germany
    Posts
    3

    Question

    This is what i have tried so far,

    # include <stdio.h>
    # include <stdlib.h>

    int main ()

    {

    int i = 0

    for(i=0;(Xmod2==0);X/2);
    for(X=0;(Xmod2!=0);x*3+1);

    {
    scanf ("%d",&number);
    printf ("insert number %d,number\n");

    At this point i cant continue....i don know if wat i am doing is right. i used mod because of the conditions that were given ....

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Okay. Before you continue, write a program that reads in an integer and prints out whether that integer is even or odd.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2008
    Location
    Germany
    Posts
    3

    Exclamation C programming

    Hi guys,
    i need help. can anyone tell me how to write a program that differentiates an even number from an odd number?

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Either you don't have a book on C programming or you haven't looked at one. I suggest you read one. Your code above shows you have almost no idea what you're doing. Any decent C programming book, website, etc. would help you, but only if you read it.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    The modulo operator &#37; gives you the remainder from a division operation. Use it with the number two and you get 0 if the number is divisible by two, and 1 if it is not.

    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-23-2006, 07:22 PM
  2. string array stuck:(
    By mass in forum C Programming
    Replies: 18
    Last Post: 05-22-2006, 04:44 PM
  3. Program stuck in infinite loop-->PLEASE HELP
    By Jedijacob in forum C Programming
    Replies: 5
    Last Post: 03-26-2005, 12:40 PM
  4. Stuck on random generating
    By Vegtro in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2003, 07:37 PM
  5. stuck ky
    By JaWiB in forum Tech Board
    Replies: 2
    Last Post: 06-15-2003, 08:28 PM