Thread: Simple C program..please help my

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    4

    Simple C program..please help my

    GoodMorning every body.. I hope all are will

    i want help from any one how know the answer to the following Q:

    write c program that read an integer value (4-digits only) then display the sum of its digits.

    Note_ Display an appropiate message if the user entered any value consists of more than or less than 4 digits.

    Example:
    123 Display the message(not allowed...try again)
    1234 calucate the sum of 1+2+3+4=10
    12345 Display the message(not allowed...try again)

    And so on.


    please help i want the answer .. i try to solve it but it no came correct

    i wait our help
    Last edited by Salem; 03-03-2011 at 10:28 AM. Reason: Snip font size abuse

  2. #2
    THANK YOU KINDLY SIR Phenax's Avatar
    Join Date
    Mar 2011
    Posts
    74
    1234 % 10 = 4
    Modulo by 10 will return the ones place. This is because 10 goes into 1230 so many times, and the remainder of said division is 4.

    1234 / 10 = 123 (Integer division!)

    Now keep repeating these operations, and you should be on the right tract.

    To make sure it's four digits, you can just do something like
    Code:
    if(num > 9999 || num < 1000)
    {
        printf("Number must be four digits");
        return 1;
    }
    Last edited by Phenax; 03-02-2011 at 10:57 PM.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    We have a strict homework policy on this board. You should take time to read it as well as the forum guidelines. Please post the code you have and ask specific questions or tell us exactly what isn't working. Phenax gave you some good hints, so you should be able to get something together.

    Also, please stop screaming (using a giant font). The regular font is much nicer to read.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    4
    I want to post my answers but it not came message apear tell write(code and /code)

    i dont understande it

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    You will not see a message telling you to use the code tags, it's just something that if you read first the advice in the guidelines then you would know. You will see the '#' symbol on the toolbar of the messagebox, you can use this for the code tags, or just simply type the actual HTML tags opening with 'code' insert the content and then close the tag by typing '/code' replace the ' characters with [ and ] or it won't work.
    Last edited by rogster001; 03-03-2011 at 02:48 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    No need to complicate things... I don't see any '#' in a toolbar unless I press "Go Advanced" button at the bottom. Who would ever do that. I just type [ code ] before the C code and [ / code ] after. Simple.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    4
    Iam not very good in programming and my specilization is business not IT for that i find difficulty in writng the code_But i try to write and the code which i write for the Q is:

    [ code ]
    #include<stdio.h>
    main()
    {
    int n,sum;
    printf("please entere the value n:\n");
    scanf("%d",&n);
    if (n<=4)
    sum=not allowed..try again
    if (n>=4)
    sum=not allowed..try again
    if(n==4)
    sum=sum of n
    [ / code ]

    I think it,s not correct
    Last edited by Amna; 03-03-2011 at 10:22 PM.

  8. #8
    THANK YOU KINDLY SIR Phenax's Avatar
    Join Date
    Mar 2011
    Posts
    74
    Code brackets do not have spaces in them (anywhere), which is why your code isn't in code blocks.

    You are correct, it's wrong in both logic and syntax.

    First of all
    n >= 4 means exactly that. That the number is >= 4. It does not mean it has greater than or equal to 4 digits. Even if it did, your code still wouldn't work, because >= means "greater than or equal to." You'd want to use > and <, not >= and <=. Look at my code above for an example on how to make sure your number is four digits. the || in the if statement is a logical or, if you didn't know that.

    sum cannot equal "sum is not correct." sum is an integer therefore it can only be set to numbers. So, if your number is not four digits, you do
    Code:
    printf("Sum is not four digits long");
    , and then you can
    Code:
    return 1;
    which will stop the program if placed in main, and return 1 (which most people will interpret that the program ended incorrectly).

    If you need a hint on how to sum the digits of a number, please refer to my post above. % = modulo operator which returns the remainder of division. So 10 % 3 = 1. Because 3 goes into 10 3 times, with a remainder of 1. Thus if you do n % 10 it will get you the ones digit (think - 17 % 10 = 7. 7 would be the ones digit). And if you divide an integer by 10, it will "downshift" each digit to the right, or rather, 1234 would turn into 123. So then you can repeat these operations, and have another integer variable summing the values you get from the modulo operator.

    I doubt anyone here is going to code it for you. It's a pretty simple problem but you seem to lack a basic understanding of programming or C. A lot of people are probably weary about your objectives as this is a trivial (and seemingly academic) problem being approached by someone with no experience in programming.

    Also, you don't have to "think" your code is incorrect, you compile it and see if it compiles & works appropriately. If you don't already have a compiler, there are tons of free ones out there. Try Dev-Cpp. It comes with a nice editor and a compiler for C/C++ programming.
    Last edited by Phenax; 03-03-2011 at 10:37 PM.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Phenax View Post
    I doubt anyone here is going to code it for you. It's a pretty simple problem but you seem to lack a basic understanding of programming or C. A lot of people are probably weary about your objectives as this is a trivial (and seemingly academic) problem being approached by someone with no experience in programming.
    It's the homework policy thing... Helping someone debug a program where they've made a reasonable effort is good... writing their homework for them is not... Nobody learns from being handed the answers. People learn best by doing...

    Our friend needs to grab a book on C... start reading, doing the exercises as he goes. Then it will sink in.

    There's a reading list in this forum... C Book Recommendations ... he needs to capitalize on it.

  10. #10
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    no need to complicate things? what are you on about mate? not everybody intuitively knows how to write html tags, so if there is a simple button available in thd forum then i will happily direct people towards it
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by rogster001 View Post
    no need to complicate things? what are you on about mate? not everybody intuitively knows how to write html tags, so if there is a simple button available in thd forum then i will happily direct people towards it
    When making a reply... in the toolbar at the top you will find an octothorpe... (# sign)... click it and it will insert the open and close CODE tags for you... past your code between the tags.

    When doing quick reply... click Go Advanced and there it is again.

    FWIW... I agree it should be on the quick reply toolbar as well...

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    4
    Thanks for every one try to help my..
    But i still have adoubt..i dont understand the question very much

    please help my more i want to solve it
    Last edited by Amna; 03-05-2011 at 10:02 AM.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Amna View Post
    Thanks for every one try to help my..
    But i still have adoubt..i dont understand the question very much

    please help my more i want to solve it

    Things get a lot simpler when you spend a few minutes and break things down to smaller steps (blobs) and work on each step separately....

    The overall goal is to take a 4 digit number such as 6498 and add up the individual digits...

    Your first step is to get that number from someplace (probably the keyboard)
    Once you have the number you need to be sure it is 4 digits (1000 -> 9999)
    Next you need to break out each digit into it's own variable (hint: % and / )
    Then you need to add up the 4 resulting variables (simple addition)
    Finally you need to display the result.

    So... work on the first "blob" ... once you have that working move on to the second... and so on until you complete the program.

    If you get really stuck, post your code and we'll see what we can do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a Battleship Program
    By HBlakeH in forum C Programming
    Replies: 1
    Last Post: 12-05-2010, 11:13 PM
  2. Help with a very simple program
    By htdefiant in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2007, 01:27 PM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM