Hello guys i need help on my program factoring.... i have the progam here but the bad news is it is been giving me -3242 or infinite number as an output... I already been so busy changing any thing but i cant make the output correct the way it should be... please help me where have i gone wrong :?:? Thank you very much....

#include<iostream.h>
#include<conio.h>
main()
{
int num;
cout<<"Enter a Number:"<<endl;
cin>>num;
while (num>=1)
{
num*=num;
num--;
}
cout<<"The Factor is:"<<num<<endl;
getch();
return 0;
}

what must be your ouput for an example...??

what must be your ouput for an example...??

please try this.....

#include<iostream>
#include <conio.h> 
using namespace std;
int main()
{

int i, n;
int fact=1;
cout<<"\nEnter a number: ";
cin>>n;
for(i=n ; i>=1 ; i--)
fact = fact*i;
cout<<"The factoral value is: "<<fact;
getch();
return 0;
}

i used for loop...just compare...

while (num>=1)
{
num*=num;
num--;
}
}

Hi Demonisya,

The problem I guess is here in while loop.
What exactly do you want your program to do? to find the factors of the entered number or the Factorial of the number??

Hi Demonisya,

The problem I guess is here in while loop.
What exactly do you want your program to do? to find the factors of the entered number or the Factorial of the number??

well the output woud be like this for example....

output
Enter a Number: 3

6....

3*2*1 it multiplies each number that it subtracts from the number you gave....

Ok that's fine..
Its factorial of the number.
Use Kiryoku's example.
That might help.

What you have done is :
num *= num;
num--;

which will do the following :
1. if suppose you entered 3.
num *= num will giv num = 3*3 = 9
then num-- i.e, 2
then it will proceed like:
9*2

Ok lets do a dry run to see where you went wrong in the program.

Say num = 5

while num >= 1 ( 5 >=1 , hence true )
num = num * num , num becmes 25.
num = num -1 , num becomes 24

again while num > = 1 ( 24 > =1 , hence true )
num = num * num , num becmes 24 squared = 576
num = num - 1 , num = 575 now

and so now , so basically your variable num keeps increasing and the condition while num >= 1 is always true , and thus ur program takes num to infinity or maximum 16bit integer value.

I guess u are looking for something like 5! = 120 right ?

thats 5 x 4 x 3 x 2 x 1 . Hint : thats 5 multiplication , with each number 1 less. try some more, then we are here.

can't you use for loop??

Ok lets do a dry run to see where you went wrong in the program.

Say num = 5

while num >= 1 ( 5 >=1 , hence true )
num = num * num , num becmes 25.
num = num -1 , num becomes 24

again while num > = 1 ( 24 > =1 , hence true )
num = num * num , num becmes 24 squared = 576
num = num - 1 , num = 575 now

and so now , so basically your variable num keeps increasing and the condition while num >= 1 is always true , and thus ur program takes num to infinity or maximum 16bit integer value.

I guess u are looking for something like 5! = 120 right ?

thats 5 x 4 x 3 x 2 x 1 . Hint : thats 5 multiplication , with each number 1 less. try some more, then we are here.

Thanks for teaching me why i got always an infinity as an output.... Let me see... all i have to do is just subtract one from the number you inputed.... and not the solution.... but what id i swap the num-- to --num what will that do??

>>what id i swap the num-- to --num what will that do??

-- is the decrement operator.
The Basic function of the Decrement operator is to decrease the value of the variable by 1.Thats pretty simple right.

However Decrement/Increment can be used in two methods they are Prefix and Postfix
The Main Difference between Pre-fix and Post-Fix is the returning value,
The Prefix operator used as --operand(var_name) first Subtracts the value and then returns it.The Postfix operator used as operand-- first returns the value and later subtracts it.

If that was a little unclear.

Consider the following Example :

#include <iostream>
using namespace std;

int main()
{
   int a = 10; //Value of a= 10
   int b= a--;// Value of a = 9
   int c= --a;// Value of a = 8
   cout<< "A = " << a <<endl   
           << "B =  a-- ="  <<b <<endl //First Assigning of Value is done then Change in value.
           << "C =  --a ="  << c <<endl//here value of 'a' is changed and then assigned. 
            << "A = " << a ;
}

The Output of the program would be .

A = 10
B = a-- = 10
C = --a = 8
A = 8

This Is About the Difference in Prefix and Postfix.

However You Swapping Would not make much difference in the program because, there is no variable/ function call taking in the value. And there-fore Not Much Change.

What you have done is :
num *= num;
num--;

which will do the following :
1. if suppose you entered 3.
num *= num will giv num = 3*3 = 9
then num-- i.e, 2
then it will proceed like:
9*2

Wrong. It will never be 9 * 2 , it will be 8 * 8 , as num -- takes it to 8 and then in the next while loop its num = 8 * 8 again.

its always num * num in the code , hence a squaring will take place. 9 * 2 doesn't come into the picture.

Thanks for teaching me why i got always an infinity as an output.... Let me see... all i have to do is just subtract one from the number you inputed.... and not the solution.... but what id i swap the num-- to --num what will that do??

My pleasure. Ok , yes you will have to substract 1 from the number you input. But not before the multiplication. Note how it works :-
if Num = 5 , you are looking for a product of
5 x 4 x 3 x 2 x 1 right ? So first the product then the decrement.

changing num-- to -- num will not help.

Lets try to see how this works :-

5! = 5 x 4 x 3 x 2 x 1

Which is 1 more deducted from 5 with each step.

Hint : You are not going to be able to accomplish this in a do-while loop ( or for loop ) using only 1 variable. You will need to use the num to count the number of products taking place and another variable to store the product.

For example, have you done the summation program ?

I want Σ5 = 5 + 4 + 3 +2 + 1 = 15 ?

Remember how it goes ? Concept is very similar with a tiny little difference.

My Suggestion before trying factorial , try working out summation of n numbers :)

Σ5 = 5 + 4 + 3 + 2 + 1.

Ok lets do a dry run to see where you went wrong in the program.

Say num = 5

while num >= 1 ( 5 >=1 , hence true )
num = num * num , num becmes 25.
num = num -1 , num becomes 24

again while num > = 1 ( 24 > =1 , hence true )
num = num * num , num becmes 24 squared = 576
num = num - 1 , num = 575 now

and so now , so basically your variable num keeps increasing and the condition while num >= 1 is always true , and thus ur program takes num to infinity or maximum 16bit integer value.

I guess u are looking for something like 5! = 120 right ?

thats 5 x 4 x 3 x 2 x 1 . Hint : thats 5 multiplication , with each number 1 less. try some more, then we are here.

I admit my mistake at 9*2... :(
I really apologise for giving wrong suggestion.. Dunno how I went wrong..
yes it will be sqauring every tym num decreases by 1 :(

YES!!! ThankS guys I got it.... Took me 1 day to figure it out now here it goes in the using while loop i need 2 variables... Like you said...

{
long double i,product=1;
cout<<"Enter a Number:"<<endl;
cin>>i;
while(i>=1)
{
  product=product*i;
  i--;
}
cout<<"The Factor is:"<<product;
getch();
return 0;
}

Now that i got it... Another thing i should ask.... How can i change the output to a whole number instead of giving an exponent?
Ex.
Output:
4.7x10e8....

Thats wonderful!! See how pleasurable it is to find out a solution on your own!! :D

Ok , now for another self discovery. Check the difference between long int and long double.

Thats wonderful!! See how pleasurable it is to find out a solution on your own!! :D

Ok , now for another self discovery. Check the difference between long int and long double.

ok... I know long double but what is long int?? :?:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.