tags:

views:

207

answers:

6

I can't figure out the problem in this:

#include<stdio.h>
int main()
{
    int a,b,count ;
    count =0;
    printf("enter the value for a ");
    scanf("%d ",&a);
    while(a>0)
    {
        b=a%10;
        count=b+count;
        a=a/10; 

        printf ("hence the simplified result is %d",count);
    }
    return 0;
}
+1  A: 

I think the printf statement should be outside the loop.

ammoQ
+2  A: 

One issue is you print the count with every loop, rather than than after the loop.

Not an issue, but C has arithmetic assignment (aka compound assignment) operators that can be more readable. For example, a /= 10 is equivalent to a = a/10.

outis
+2  A: 

You do not terminate your printf() with a "\n". The output stream (stdout) is, usually, line buffered. That means that incomplete lines need not be printed unless you force them with fflush(). But there's no need for that.

Simply add a "\n" to your printf()

        printf("hence the simplified result is %d\n", count);
pmg
That won't solve his problem.
Amit
I couldn't determine his problem from his post. Anyhow, this will make his program "better".
pmg
It will solve one problem. Hence a useful answer since the question was a bit vague.
Peter Lindqvist
+1  A: 

Move the printf out side the loop. That will fix it.

mkamthan
A: 

Try the following:

#include<stdio.h>
int main()
{
    int a,b,count ;
    count =0;
    printf("enter the value for a ");
    scanf("%d",&a);
    while(a>0)
    {
        b=a%10;
        count=b+count;
        a=a/10; 
    } 
    printf ("hence the simplified result is %d",count);
    return 0;
}
Himadri
Try loosing the " " in the scanf as well.. :)
Peter Lindqvist
Yes @PeterLindqvist. u r right
Himadri
+2  A: 

There's a silent killer in your code:

scanf("%d ",&a);

The extra space in your scanf will make entering numbers harder: this will match 12<space>, but not 12. Replace the "%d " with "%d".

Andomar
My previous comment was incorrect when I stated that there's no problem with `"%d "` format. There's a problem, although it is different from what the above answer discribes. The `"%d "` will match `12` input perfectly fine, meaning that it will successfuly read the 12, since the space after the `%d` has no effect on the way `%d` is interpreted. However, the trailing `" "` in the format string will direct `scanf` to continue to eat-up whitespace after reading the number until a non-white space or EOF is encountered. This makes no sensea and will be confusing when enering numbers manually.
AndreyT
@AndreyT: On my system at least, `scanf("%d ")` does not return if you input `1<return>`
Andomar
@Andomar: That's what I'm talking about. As I said, it doesn't return because `scanf` continues to consume whitespace and `<return>` is whitespace. If you input, say. `1a<return>` it will return immediately and the value of a will be successfully read as `1`.
AndreyT
@Andomar: You said above that it will not match `12`. This is incorrect. It will match `12` perfectly well, if it is really just `12`, i.e. there's an EOF or a non-whitespace after that `12`. The problem is that in addition to `12` it can match `12<a-lot-of-whitespace>`, so `scanf` will no return until it finds the end of that whitespace.
AndreyT
@AndreyT: Interesting comments, that certainly explains it in detail
Andomar