Thread: C Programming help needed

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    2

    Question C Programming help needed

    I'm stumped on this assignment. C hasn't really been coming easy for me so I would appreciate some help!

    Problem 1: Is It Prime?

    Write a C program that takes a positive integer on the standard input (aka, user input) and prints ”prime” or ”composite” or ”unit” (A number is a unit if it is 1, prime if it is divisible only by 1 and itself (and is not 1), and composite otherwise). I recommend using the % modulus operator, as it returns the remainder of dividing two numbers. n % x == 0 only if x evenly divides n. Using loops, particular a for loop, would be quite advantageous. Example input and output:
    Enter a number: 6 composite

    Problem 2: Guess My Number

    You are going to generate a random number between 1 and 10. Remember to include time.h. At the beginning of the main function, use the following:
    //This seeds the random generator using the current time . srand (time(NULL));
    Then you can get a random number between 1 and 10 using:
    int r = rand () % 10 + 1;
    Let the user keep guessing until they get it correct. Then output how many guesses it took. OPTIONAL: Tell them whether they are too high or too low.
    I am thinking of a number between 1 and 10.
    Your guess : 5 Incorrect . Guess again : 7 Incorrect . Guess again : 2 Correct ! Number of guesses : 3.

    thank you very much for your help

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    132
    The chances of your post being welcome here would probably be greater if you wrote here what you've tried so far. Then, we can point suggestions on the code you've written.

  3. #3
    Registered User
    Join Date
    Apr 2015
    Posts
    2
    I'm sorry, I honestly do not know C. I am trying to learn some languages so I can go back to school. I know HTML and python, C is just coming on difficult to me. I like to learn it by looking at code and breaking it down.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    132
    I suggest you try following some book or tutorial for beginners in C. If you already know Python, some concepts in C might be more easily learned, like conditionals and repetions.
    Last edited by stdq; 04-20-2015 at 09:17 PM.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by jluzi
    ...C is just coming on difficult to me. I like to learn it by looking at code and breaking it down.
    There's your problem. You won't learn C by simply studying code. As stdq said, you should get yourself a good book. Open it to the first chapter and start learning from the beginning. Read about how C works, type out the examples for yourself, modify the examples to see if the changes do as you expect, and do all practice problems at the end of the chapter. Do a few simple "test" programs along the way to see how well you understand the concepts you've learned.

    If you already have some experience with programming languages, then learning C should come easier than if it were your first language ever.

    Honestly, I don't know how much you can learn by "breaking down" the code, if you don't know the basic rules of the language in the first place.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. some help needed for ADC programming
    By O.lewis in forum C Programming
    Replies: 32
    Last Post: 05-14-2011, 09:38 AM
  2. Help needed c programming
    By d.bissell in forum C Programming
    Replies: 10
    Last Post: 11-28-2010, 05:44 PM
  3. Help needed for c programming only
    By zhengkoon8 in forum C Programming
    Replies: 1
    Last Post: 12-27-2009, 06:57 AM
  4. C++ Programming Help Needed Please!!!
    By a2008 in forum C++ Programming
    Replies: 6
    Last Post: 03-26-2008, 01:10 AM
  5. c programming help needed!
    By yankees8223 in forum C Programming
    Replies: 7
    Last Post: 05-01-2003, 01:57 PM

Tags for this Thread