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.

i wait our help

2. 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;
}```

3. 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. I want to post my answers but it not came message apear tell write(code and /code)

i dont understande it

5. 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.

6. 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. 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;
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

8. 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.

9. Originally Posted by Phenax
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... http://cboard.cprogramming.com/c-pro...endations.html ... he needs to capitalize on it.

10. 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

11. Originally Posted by rogster001
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. Thanks for every one try to help my..
But i still have adoubt..i dont understand the question very much

13. Originally Posted by Amna
Thanks for every one try to help my..
But i still have adoubt..i dont understand the question very much

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.