DIGIT-ARC read-only archive · snapshot: July 23, 2026

c++ programming in making n!

15 posts · originally from https://geek.digit.in/community/threads/c-programming-in-making-n.146623/

mayoorite
mayoorite Original post #1
Sep 19, 2011 at 6:26 PM
1+1/1!+1/2!.....n
I got really confused in this
please help today
Neo
Neo #2
Sep 19, 2011 at 7:26 PM
what to make?
clarify please
Prime_Coder
Sep 20, 2011 at 1:40 AM
Question not clearly mentioned. You should mention it and please use brackets, if possible.
S
Sep 20, 2011 at 1:28 PM
1. Create a recursive function for calculating n!
2. In a "for" loop, keep adding the result of 1/i! for i varying from 1 to n
3. Output the result.

Which portion of this has you confused?

Arun
W
Windows #5
Sep 20, 2011 at 3:04 PM
Code:
#include <iostream.h>
#include <conio.h>

void main()
{
int sum=0,i,n;
int nfact=1;
for(i=1;i<=n;i++)
{
nfact=nfact*i
sum = sum + 1/nfact
}
cout<<"Sum is : "<<sum;
getch();
}
mayoorite
Sep 20, 2011 at 9:41 PM
thanks for response well i have done it.
Code:
#include<iostream.h>
#include<conio.h>
main()
{

	clrscr();
	int l;
	float i,sum=1,j,k,x=0,n;
	cout<<"Enter last limit:";
	cin>>n;
	l=n;
	for(i=0;i<n;i++,l--)
	{
		for (k=1,sum=1; k<= l; k++ )
		sum =sum* k;

		x=x+(1/sum);
	}
	cout<<x+1;
	getch();
}
*i51.tinypic.com/30lji0z.jpg:cool:
d6bmg
d6bmg #7
Sep 20, 2011 at 10:26 PM
P.S. A good programming practice, nowadays we don't need clrscr();
So, avoid it where you can.
nims11
nims11 #9
Sep 21, 2011 at 12:44 PM
^^ you don't need to clear the screen with the newer IDEs and compilers. Still you can do so by using system("cls") function. (in Windows). also as conio.h has been deprecated, clrscr() shouldnt be used.

also for the sake of your knowledge, algo for calculating consecutive factorials can be made very fast and efficient using dynamic programming. (in easy words, storing the results of a smaller problem and not calculating it again and again). give a thought to it. :)
dashing.sujay
Sep 21, 2011 at 1:01 PM
^^ you don't need to clear the screen with the newer IDEs and compilers. Still you can do so by using system("cls") function. (in Windows). also as conio.h has been deprecated, clrscr() shouldnt be used.

also for the sake of your knowledge, algo for calculating consecutive factorials can be made very fast and efficient using dynamic programming. (in easy words, storing the results of a smaller problem and not calculating it again and again). give a thought to it. :)

*So does that mean that newer IDEs or compilers elf implement clrscr functionality?
*this system("cls") function is for c++ na? its syntax pls ( i couldnt get it actually )
*Do you mean "discontinued" by "deprecated". Plus, yeah, i have read somewhere that conio.h has been 'deprecated' Why so and any additions resulting after its deprecation?
*Do u refer to DMA by dynamic programming ?

Thnx for answering my queries, and for sure i'll give them a serious thought :)
nims11
nims11 #11
Sep 21, 2011 at 5:00 PM
*So does that mean that newer IDEs or compilers elf implement clrscr functionality?
*this system("cls") function is for c++ na? its syntax pls ( i couldnt get it actually )
*Do you mean "discontinued" by "deprecated". Plus, yeah, i have read somewhere that conio.h has been 'deprecated' Why so and any additions resulting after its deprecation?
*Do u refer to DMA by dynamic programming ?

Thnx for answering my queries, and for sure i'll give them a serious thought :)

*new compilers, atleast the standard g++ don't support conio.h which has clrscr().
* system() function executes any command the OS supports. eg. if you type cls in command line in windows, the screen will be cleared. to perform this in C++, use system("cls");. you can change the argument of system to do anything else like executing an external program.
* deprecated means it will be discontinued in near future. it has already been removed from g++ and exists only in old compilers. I think minGw gives a warning for conio.h. conio.h is basically for old DOS compilers and doesnt serve any purpose nowadays.
* no. :?
Dynamic programming - Wikipedia, the free encyclopedia
Garbage
Garbage #12
Sep 21, 2011 at 5:28 PM
Why would you need to clear the screen before running your program?

For example, if I am working on command line, executed some other commands and then invoked the program, it will clear my screen; which normally not the intended behavior.

And if in some rare cases, you still need to clear the screen, as nims11 mentioned, you can use system() call.
A
Sep 21, 2011 at 6:30 PM
once again a Turbo user!!
dashing.sujay
Sep 21, 2011 at 7:31 PM
*new compilers, atleast the standard g++ don't support conio.h which has clrscr().
* system() function executes any command the OS supports. eg. if you type cls in command line in windows, the screen will be cleared. to perform this in C++, use system("cls");. you can change the argument of system to do anything else like executing an external program.
* deprecated means it will be discontinued in near future. it has already been removed from g++ and exists only in old compilers. I think minGw gives a warning for conio.h. conio.h is basically for old DOS compilers and doesnt serve any purpose nowadays.
* no. :?
Dynamic programming - Wikipedia, the free encyclopedia

Thnx, got all points.

Queries- *Since conio.h is no longer continued, so is getch() ? If yes, then what is the replacement for it? And what about other functions needing conio.h ?
*So, turboC comes in those "old DOS compilers" ? Should i not use it ? If yes, then should i use MS VC++ ?

Why would you need to clear the screen before running your program?

Just to look neat and clean :p
Liverpool_fan
Sep 21, 2011 at 7:48 PM
Please take further discussion to one of the two C/C++ stickies as fit.