Click here to Skip to main content
15,889,498 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
We have an activity to program, but I am not good in programming so I would like to ask for your help. We are tasked to code a c program but my program does not run. Below is the instructions of our activity.


Design a flowchart and make a C program that will perform multiplication from user-given multiplicand (positive integer) and multiplier (positive integer) using repeated addition.

Example:
Enter Multiplicand: 4 Enter Multiplier: 3
The product is: 12

Enter Multiplicand: 4 Enter Multiplier: -3
INVALID INPUT

What I have tried:

C++
#include <stdio.h>
const int;
int main()
for (int i = 0; i <= max_values; i++) {
    int a,b;
    int mul,loop;
     
    printf("Enter Multiplicand: ");
    scanf("%d",&a);
    printf("Enter Multiplier: ");
    scanf("%d",&b);
     
    mul=0;

    for(loop=1;loop<=b;loop++){
        mul += a,b;
        
    printf("The product is: %d\n",mul);
    }
else {
        fprintf(stderr, "INVALID INPUT\n");
    }
    

    return 0;
}
Posted
Updated 16-Dec-20 0:31am
v2

Try
C
#include <stdio.h>
int main()
{
  int a, b;
  int c;
  do
  {
    printf("Enter Multiplicand: ");
    scanf("%d",&a);
    printf("Enter Multiplier: ");
    scanf("%d",&b);

    if ( a < 0 || b < 0 )
    {
      printf("Invalid input\n");
    }
    else
    {
      int mul = 0;
      for(int i=0; i<b; ++i)
        mul += a;

      printf("The product is: %d\n",mul);
    }

    printf("enter '1' to continue\n");

    scanf("%d", &c);
  }  while  (c == 1);

    return 0;
}



[Update]
Fixed the missing check on input a, thanks to KarstenK.
[/Update]
 
Share this answer
 
v2
Comments
KarstenK 16-Dec-20 6:57am    
you need to check both input values.
CPallini 16-Dec-20 7:00am    
You're right. Thanks.
There are a number of simple errors in your code, which could be resolved by reading your notes or checking any of the study guides available online.
C++
#include <stdio.h>
// this line does not mean anything. When you declare a variable you must give it a name.
const int;
int main()
// you have not declared max_values anywhere. Presumably it should be on the line above.
for (int i = 0; i <= max_values; i++) {
    int a,b;
    int mul,loop;
     
    printf("Enter Multiplicand: ");
    scanf("%d",&a);
    printf("Enter Multiplier: ");
    scanf("%d",&b);
     
    mul=0;

    for(loop=1;loop<=b;loop++){
// this line will add the value of a to mul. the reference to b is ignored*.    
        mul += a,b;
        
    printf("The product is: %d\n",mul);
    }
// this else has no corresponding if clause.
else {
        fprintf(stderr, "INVALID INPUT\n");
    }
    

    return 0;
}


* thanks to Karsten for pointing out my mistake.
 
Share this answer
 
v2
Comments
KarstenK 16-Dec-20 6:56am    
I think it will add only a :-O
Richard MacCutchan 16-Dec-20 7:08am    
Correct, answer updated.
CPallini 16-Dec-20 7:25am    
So, writing
  mul+=a,b;

is a rather obscure way for writing the correct statement. :-D
Richard MacCutchan 16-Dec-20 7:29am    
Yes, and it's so annoying.
To add to what CPallini says, why is a negative number an invalid value?
3 * -1 == -3
3 * -2 == -6
...
-3 * 1 == -3
-3 * 2 == -6
...
-3 * -1 == 3
-3 * -2 == 6
...
Instead of rejecting negative inputs, you should be working out the sign of the result, using the absolute values of the inputs, and applying the sign to the product you generate.

It would also be an idea to ensure that the lowest value is used as the loop guard, or 2 * 100000000 will take a lot longer to evaluate than 100000000 * 2.

It might also be worth looking at implementing a decimal or binary version of "long multiplication" if you want efficiency with larger numbers:
69 * 72 == 690 * 7 + 69 * 2
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900