multiplication program

This is a discussion on multiplication program within the C Programming forums, part of the General Programming Boards category; Hi there, I need to write a program that multiplies two numbers and provides the output as follows: Code: 50 ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    53

    multiplication program

    Hi there, I need to write a program that multiplies two numbers and provides the output as follows:

    Code:
         50
        x43
    -------- 
        150
    +2000
    --------
       2150
    So im thinking about going about it like this:

    input first number;
    input second number;
    multiply two numbers to get the product;

    my question is, how should i go about displaying the output like that (such as the 150 and 2000)? My idea would be to use an array and move along the array and multiply each number individually and then out put it like that. Would there be any easier way of doing this?

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    I'm not entirely sure what you're asking - are you talking about the best way to do the alignment? Or are you not sure how to input and output numbers?

    It's probably best if you just make and attempt to meet the minimum requirements of the problem yourself. Post your source code, and if there's a way to improve it we can point it out to you. If you get stuck at a certain spot while you're trying, post your problem and we'll help you with that.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    Quote Originally Posted by sean View Post
    I'm not entirely sure what you're asking - are you talking about the best way to do the alignment? Or are you not sure how to input and output numbers?

    It's probably best if you just make and attempt to meet the minimum requirements of the problem yourself. Post your source code, and if there's a way to improve it we can point it out to you. If you get stuck at a certain spot while you're trying, post your problem and we'll help you with that.
    Sorry for the vaugness, heres the code I have so far:

    Code:
    #include<stdio.h>
    
    int main()
    {
    
    int num1, num2,answer;
    
    printf("Enter the number 1 : ");
    scanf(&num1);
    
    printf("Enter the number 2 : ");
    scanf(&num2);
    
    answer = num1 * num2;
    
    printf("The answer is %d", answer);
    
    //output
    printf(%d\nX,num1);
    printf(%d\n-----------,num2);
    printf(xxxxxxxxxxxxxxxx);
    printf(xxxxxxxxxxxxxxxx);
    printf(%d,answer)
    return 1;
    }
    So at the output, i was wondering how i would get the:
    --------
    150
    +2000
    --------
    included. I dont really have any idea on how to go about that. I hope this makes sense, sorry for the confusion =(

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Look up printf, and pay attention to the format width specifiers.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    Quote Originally Posted by quzah View Post
    Look up printf, and pay attention to the format width specifiers.


    Quzah.
    Gotcha. Now the last thing im trying to figure out is how do I find the numbers themselves? How would I get the 3*50, then shift to the 4 and multiply that by 50 and add a 0? Would I have to use arrays to do that?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by wankel View Post
    Sorry for the vaugness, heres the code I have so far:

    Code:
    #include<stdio.h>
    
    int main()
    {
    
    int num1, num2, answer;
    
    printf("Enter the number 1 : ");
    scanf("%d", &num1);
    
    printf("Enter the number 2 : ");
    scanf("%d", &num2);
    
    answer = num1 + num2;
    
    //printf("The answer is %d", answer);
    
    //output
    printf("\n-----------";
    printf("\n  %6d". num1);
    printf("\n+%6d", num2);
    printf("\n==========");
    printf("\n %d",answer);
    num1 = getchar();
    return 1;
    }
    So at the output, i was wondering how i would get the:
    --------
    150
    +2000
    --------
    included. I dont really have any idea on how to go about that. I hope this makes sense, sorry for the confusion =(
    When you use scanf(), remember to include what data type you will be giving to it: integer(%d), char(%c), string(%s), etc.

    When you use printf(), remember to include double quotation marks, and within those marks, include the type of data type you want it to print up: integer(%d), char(%c), string(%s), etc.

  7. #7
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    Quote Originally Posted by Adak View Post
    When you use scanf(), remember to include what data type you will be giving to it: integer(%d), char(%c), string(%s), etc.

    When you use printf(), remember to include double quotation marks, and within those marks, include the type of data type you want it to print up: integer(%d), char(%c), string(%s), etc.
    Thanks for the tip Adak

  8. #8
    Registered User
    Join Date
    Aug 2006
    Posts
    163
    Quote Originally Posted by wankel View Post
    So at the output, i was wondering how i would get the:
    --------
    150
    +2000
    --------
    included. I dont really have any idea on how to go about that. I hope this makes sense, sorry for the confusion =(

    Try using modulous division. This should allow you to seperate out the 3 from the 40. It might get a little convoluted over 99, though.

    Any other ideas?

  9. #9
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    Quote Originally Posted by System_159 View Post
    Try using modulous division. This should allow you to seperate out the 3 from the 40. It might get a little convoluted over 99, though.

    Any other ideas?
    Thanks System_159! I used your modulus division idea and im getting somewhere. However, my output values are way off, I keep getting crazy high numbers that dont make any sense. Can anyone tell me what i missed?

    Code:
    #include <stdio.h>
    
    int main ()
    {
    	int number1;
    	int number2;
    	int sum;
    	int product;
    	int removed;
    	int digit;
    
    	printf("Enter first number:");
    	scanf("%d", &number1);
    	printf("Enter second number:");
    	scanf("%d", &number2);
    
    //keep looping while the multiplier still has numbers
    while (removed != '\0')
    {
    	removed = number2/10;
    	digit = removed % 10;
    	product = digit*number1;
    	sum += product;
    }
    
    //checks the output
    printf("%d", removed);
    printf("\n");
    printf("%d", digit);
    printf("\n");
    printf("%d", product);
    printf("\n");
    printf("%d", sum);
    
    return 0;
    }
    for the removed and digit, I get a zero which I think should be right. For the product and sum, I get crazy high numbers. Did I make a silly mistake in there?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by wankel View Post
    Code:
    	product = digit*number1;
    What do you think this line does?

  11. #11
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    Quote Originally Posted by tabstop View Post
    What do you think this line does?
    Im hoping that it takes the singled out digit of the second number, multiplies it with the first number and stores it in "product". Im assuming thats not whats happening right now?

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by wankel View Post
    Im hoping that it takes the singled out digit of the second number, multiplies it with the first number and stores it in "product". Im assuming thats not whats happening right now?
    That's what happens. Maybe I misunderstood your original post, or your intent, but you're only getting *just* that number multiplied, and not the value. (I.e. if you were doing 37*24, you'd just get 3*24, not 30*24.)

    Also, sum is uninitialized, so you're starting off with some random number between -2billion and 2billion.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21