4 Replies - 4464 Views - Last Post: 22 February 2009 - 11:12 PM Rate Topic: -----

#1 ejiroy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 22-February 09

Multiply Using While Loop

Posted 22 February 2009 - 10:56 PM

How do I multiply two numbers without using the * operator, using a while loop?
Here is what I have so far...what am I doing wrong?
#include <iostream>
using namespace std;
main()
{
 int A1, B1, Result, i;

 cout << "Enter first number:";
 cin >> A1;
 cout << "Enter second number:";
 cin >> B1;

 while(i<B1)
 {
  Result = Result + A1;
  i++;
 }

 cout << "Your result is:" << Result;
}



** Edit ** :code:

Is This A Good Question/Topic? 0
  • +

Replies To: Multiply Using While Loop

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Multiply Using While Loop

Posted 22 February 2009 - 11:00 PM

Welcome!

Thanks for trying to use the code tags! The closing tag is "[\ code]" without the space.

When you are posting code, the edit window has a "code" button that will insert the tags at the current cursor position. First click inserts the opening tag "[ code]" and the second click will insert the closing tag "[\ code]".


The variable "i" needs to be initialized to zero before you use it. :).

int A1, B1, Result, i = 0;

This post has been edited by n8wxs: 22 February 2009 - 11:05 PM

Was This Post Helpful? 0
  • +
  • -

#3 ejiroy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 22-February 09

Re: Multiply Using While Loop

Posted 22 February 2009 - 11:04 PM

It still doesn't seem to work.
Was This Post Helpful? 0
  • +
  • -

#4 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Multiply Using While Loop

Posted 22 February 2009 - 11:10 PM

That's cause I missed the fact you're adding to "result". :)

It too needs to initialized to zero - int A1, B1, Result = 0, i = 0;

This post has been edited by n8wxs: 22 February 2009 - 11:11 PM

Was This Post Helpful? 0
  • +
  • -

#5 ejiroy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 22-February 09

Re: Multiply Using While Loop

Posted 22 February 2009 - 11:12 PM

Appreciate your help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1