0

I have to multiply digits by taking input from the user and when he enter 'n' it will produce the answer.

For example (2*3*2 = 12). But I manage to write the code for taking two inputs but can't find the way to take multiple inputs from user and produce the total answer. Here is the code;

void main (void)
            {
                float f1,f2;
                float total;
                int status1,status2;
                printf("Enter first number to multiply:'n' to quit.\n ");
                status1=scanf("%f",&f1);
                printf("Enter another number to be multiply:'n' to quit.\n ");
                status2=scanf("%f",&f2);
                while (status1==1 && status2==1)
                    {
                        total=f1*f2;
                        status1=scanf("%1.0f",&f1);
                        status2=scanf("%1.0f",&f2);
                    }
                printf("Multiplition Total = %1.0f",total);
                getch();
            }
1
  • @Macmade No, seriously I am new in programming and I am learning C. Commented Jan 25, 2012 at 11:52

4 Answers 4

1

You can use a while loop, as follows.

 float prod = 1, f;

 printf( "Enter the numbers, n to stop.\n" );
 while( scanf( "%f", &f ) )
   prod *= f;
 printf( "product = %f\n", prod );
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the answer, but can i use without bounding the user for put input not more than 100, I mean can I do it without max input limits.
Yes you may calculate the product on the fly, like shown in the edited code.
1

Tested:

#include <stdio.h>

int main()
{
    int total = 1, factor = 1, success;
    do
    {
        total *= factor;
        printf("Enter integer number to multiply or 'n' to quit: ");
        success = scanf("%d", &factor);
    }
    while (success);
    printf("Multiplication Total = %d\n", total);
    return 0;
}

And a piece of advice as you said you start your adventure with C:
Unless you have some specific reason to do otherwise, use double, not float.
However, in your question you asked for digits (integer) multiplication, so int is sufficient. If you can avoid floating point numbers, avoid them. They're much more complicated then integers and can let you in worse problems if you don't use them with caution.
You can refer to What Every Computer Scientist Should Know About Floating-Point Arithmetic.

Comments

0

This would do what you need and handles, 0, 1 or an unlimited number of inputs:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    float f1;
    float total = 0;

    printf("Enter number: ");
    if (scanf("%f",&total))
    {
        for (;;)
        {
            printf("Enter number: ");
            if (!scanf("%f", &f1))
            {
                break;
            }
            total *= f1;
        }
    }

    printf("Multiplication Total = %f\n",total);

    getch();

    return 0;
}

It keeps a running total as values are entered but it stops on first invalid input, not just when n is entered.

4 Comments

Thanks a lot, it worked. But I have a question that can I by any mean write the statement of if with the statement of while. (by using && or || operators).
I have edited the code to add the write of the question. Note, this answer has recieved a downvote so there may be a mistake (I don't what mistake I have made and downvoter has not provided a reason for the downvote).
BTW, if you just need to process integers change float to int and scanf()/printf() format specifier to %d.
That's exactly the same solution as mine, but coded a little less efficiently (and without the fflush calls which should be needed, but apparently aren't on Turbo C).
0

Untested:

float f, p = 1;

printf ("Enter a number: ");
fflush (stdout);
while (scanf("%f", &f) != EOF)
  {
    p *= f;
    printf ("Enter another number: ");
    fflush (stdout);
  }

printf ("Product: %f\n", p);

6 Comments

Just for your info. Iam using Turbo C 3.0 compiler. Thank you
The above is (a fragment of) standard C. If your compiler can't cope with that then it's not a C compiler. ;) You'll need to wrap it in a function, of course.
Sorry not worked the code for me, as my compiler does not support fflush() functtion...any other solution?
@AbdulAziz Yes, there is another solution... Install a real C compiler. ;)
@Gandaro Thanks for the kind advice. But can you advice me compiler better that Turbo C 3.0 compiler. I mean simpler and efficient that this one. please
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.