hi! to everyone
i have to make a program in c++ about factorial but the main problem is that the working of factorial must me shown for eg.
4!=4*3*2*1
like this on the output screen i had done upto here....

#include <stdio.h>
#include <conio.h>
int main()

{
long float i,j,n;
clrscr();

gotoxy(33,7);
printf("enetr any value=");
scanf("%lf",&n);

gotoxy(32,4);
printf("-----------WORKING--------------");
gotoxy(32,5);
printf(" =======");
j=1;


for ( i=1; i<=n; i++)
{

j=j*i ;
}

gotoxy(33,8);
printf("The factorial is %lf\n", j);

getch();
}

if any one help me in easier way because iam beginner user of c++
thank you!

Recommended Answers

All 4 Replies

My clue for you

Our Software Development forum category encompasses topics related to application programming and software design. When posting programming code, encase it in [code], [code=syntax], where 'syntax' is any language found within our Code Snippets section, or [icode], for inline code, bbcode tags. Also, to keep DaniWeb a student-friendly place to learn, don't expect quick solutions to your homework. We'll help you get started and exchange algorithm ideas, but only if you show that you're willing to put in effort as well.
Our C++ forum is the place for Q&A-style discussions related to this popular language. Note we have a separate C forum for non-OOP straight C. Please direct C++ questions directly related to game development to our Game Development forum.

Hallo
first of all, I think the way u code IS NOT STANDARD C++ !!!! Instead it looks more like C to me.
Maybe it is not important for u , maybe it is for your professor :d

Here is what u wanted:

#include <iostream>
#include <iomanip>

int main () { 

	long number; // the number to use to calculate its factorial
	long factor; // the factorial of the number
	long term; // next term to use in the calculation


	// prompt and get the first number
	std::cout << "Enter a number or -1 to quit: ";
	std::cin >> number;

	while (number >= 0 && std::cin) 
	{
		// calculate number factorial 
		factor = 1; 
		for (term=2; term <= number; term++) 
		{ 
			factor *= term; 
		} 
		
		// output number and its factorial
		if(number == 1)
		{
			std::cout << number << "! = 1*1=1" <<std::endl;
		}
		else
		{
			std::cout << number << "! = " ;
			for (int k=2; k<number+1; ++k)
			{
			     std::cout<<number+2-k<<"*";
			}
			std::cout <<"1 ="<< factor <<std::endl; 
		}

		// get another number to do 
		std::cout << "Enter a number or -1 to quit: "; 
		std::cin >> number; 
	} 
	
	return 0; 
}

I leave it to u to do some improvements :)

Remove the long before the float and change %lf to %f . Then it'll work.
Also, it'd be clearer if you'd intialize j when declaring it, and not some lines after it.

And here the solution in C like u asked:

//#include <iostream>	//c++

#include <stdio.h>		//the same in C

int main () { 

	long number; // the number to use to calculate its factorial
	long factor; // the factorial of the number
	long term; // next term to use in the calculation


	// prompt and get the first number
	//std::cout << "Enter a number or -1 to quit: ";	//C++
	printf("Enter a number or -1 to quit: ");		//the same in C
	//std::cin >> number;					//C++
	scanf("%ld",&number);					//the same in C

	//while (number >= 0 && std::cin)
	while (number >= 0) 
	{
		// calculate number factorial 
		factor = 1; 
		for (term=2; term <= number; term++) 
		{ 
			factor *= term; 
		} 
		
		// output number and its factorial
		if(number == 1)
		{
			//std::cout << number << "! = 1*1=1" <<std::endl;//C++
			printf("%d!=1*1= 1\n",number);			//the same in C
		}
		else
		{
			//std::cout << number << "! = " ;		//C++
			printf("%d!=",number);				//the same in C
			for (int k=2; k<number+1; ++k)
			{
			//std::cout<<number+2-k<<"*";			//C++
			printf("%d*",number+2-k);				//the same in C
			}
			//std::cout <<"1 ="<< factor <<std::endl; 	//C++
			printf("1= %d\n",factor);				//the same in C
		}

		// get another number to do 
		//std::cout << "Enter a number or -1 to quit: ";	//C++ 
		printf("Enter a number or -1 to quit: ");		//the same in C
		//std::cin >> number; 					//C++ 
		scanf("%ld",&number);					//the same in C

	} 
	
	return 0; 
}
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.