loop condition

Pages: 12
condor, i am sorry but i was in a hurry and i pasted the algorithm provided by vlad from moscow adding the interface stuff at the end
@Josue Molina

Of course I've got it...My question was about the "point" of that if: the value of the int product when int i takes all the values!..
Last edited on
@fluture

If you're displaying the odd and even numbers using loops, I see no point keeping the first of the three loops. The work it's doing can be done by the other two, lazy loops.

Andy
Last edited on
@fluture yeah this one seems the one that i need but i am wondering why is the product of odd numbers are in - ?

it shows

 
-373459037
Here's an attempt for this task:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>

using namespace std;

int main()
{
   int digit[256];               //array will have the capacity to store 256 digits.
   int x, n = 100;
	
	digit[0] = 1;              //Initializes array with only 1 digit, the digit 1.
   int counter = 1;                 //Initializes digit counter
	int carry = 0;             //Initializes carry variable to 0
	
   for(int i = 1; i <= n; i += 2) //The product starts with 1 and continues with odd numbers
   {					       //If it starts with 2 (int i = 2) will continue with even numbers...
		for(int j = 0; j < counter; j++)
      {
          x = digit[j]*i + carry; //x contains the digit by digit product
          digit[j] = x % 10;     //Contains the digit to store in position j
          carry = x / 10;     //Contains the carry value that will be stored on later indexes
      }
      while(carry > 0)        //Loop that will store the carry value on array.
      { 
         digit[counter] = carry % 10;
         carry /= 10;
         counter++;                //Increments digit counter
      }
    }
    
    for(int i = counter - 1; i >= 0; i--)//Printing answer
    cout << digit[i];
    cout << "\nThe product has " << counter << " digits...\n";
    return 0;
}


Output is

1
2
2725392139750729502980713245400918633290796330545803413734328823443106201171875
The product has 79 digits...

Last edited on
@andywestken indeed you are right but as i said i was in a hurry and i pasted the algorithm from vlad from moscow just adding theinterface stuff at the end and i didn't see the final result of the program. I am sorry about that.
@xeimaster i have just shown you how to display the operations in the output but there are still logical problems which were disccused earlier, but if you want combine condor's algorithm ( which seems to be treating the problem well) with my interface and you should get your program. My result was negative beacause the answer had too many digits so the program gave an ilogical answer. I hope you understood me. : )
Topic archived. No new replies allowed.
Pages: 12