Freshman_in_C_Programming said:
I tried it many times...but it seems not working.
My program goes like this:
No it does not!
Don't re-type your code here. Copy and paste _exact_ code you used.
This one won't compile and is broken in other ways too. See below.
BTW, since you re-typed it, you could have fixed that horrible
indentation.
Avoid implicit `int` and do a proper
int main(void)
or
int main(int argc, char *argv[])
An `int` is not guaranteed to be able to hold the product of two
`int`s. You'd have been better off declaring `product` as `long`, but
that would not have been guaranteed to hold your product either (
sizeof(int) <= sizeof(long) ). You'd really want to check `x` and `y`
before you multiply them.
I prefer declarations on separate lines, at least the ones that are
logically separate:
int x, y;
long product;
Would have been much clearer.
First initialisation of `product` (c.f. your own PS).
This is not a standard function. You also don't need it anyway.
printf("Enter first number: ");
You really want to terminate `printf`s with '\n', otherwise there's no
obligation on the part of the system to output anything.
Don't use scanf(). Use fgets()/sscanf() combination instead. Search
this group for detailed discussion as to why (also why fgets() and
_not_ gets()).
Also, you never declared `a` (or `b` for that matter).
printf("Enter second number: ");
You don't seem to...
Here, I'd first check whether a*b can fit into product, and output a
diagnostic message if it won't.
product=a*b;
printf("The product of two numbers is %d", product);
} while(a==0&&b=0);
The comparison is broken. You probably meant `b==0`.
For that matter, relying on specific input parameter values as an exit
condition is generally not a good idea, and especially when what you do
with them is actually _defined_.
getch();
}
_____________________________________________
That was my original program and it ran correctly.
No it did not. What does it do if `a` and `b` are both == 0? Or was
hammering the fact that 0*0 = 0 into the poor user part of the
assignment? ;-)
Then our teacher told us not to use multiplication operator ( * )....
and I thought for a moment..."God! can I do that?"
Oh dear, here we go again... :'-(
Go back to where they taught you what multiplication is...
It actually does!
My problem is...how will I initialize the variable product.
No, that's /not/ what your problem is. The problems are listed above.
P.S. My first initialization is = product=a*b;
No it is not. See above.
Cheers
Vladimir