+ 2
I have written program but while entering a number I'm not getting the answer. I have written factorial program.i need urgent.
# include<iostream.h> class power { int f,fact,i,n; public: void factorial() { cout<<"enter a no"; cin>>i; fact=1; for(i=1;i<=f;i++) fact=fact*i; cout<<"factorial of"<<f; } }; void main() { power p; p.factorial (); getch(); }
20 Respostas
+ 4
In c++,
Header file form is #include<iostream>
Some old compilers need to include. dot.h but not in new compilers.
You missed to add
include namespace std;
after header file..
And remove dot in power. p;
power p;
And there is display function defined. You may meaning that p.factorial();
getch() will not work here. Remove that also...
And the actual logic for factorial is what you implemented.
It should be like for ex 4:
4*3*2*1=24
Use a for loop to find factorial there...
+ 4
https://code.sololearn.com/c3eg8MvlATAf/?ref=app
https://code.sololearn.com/cOInw4yJq7wi/?ref=app
https://code.sololearn.com/cE7lXk0wwKMN/?ref=app
https://code.sololearn.com/c4phTP2wUQ3u/?ref=app
https://code.sololearn.com/cOfMIY803RdZ/?ref=app
https://code.sololearn.com/c4Q0rFUFxZIP/?ref=app
https://code.sololearn.com/cj7pAcAEmqTm/?ref=app
+ 3
Why you taking input to i?
What is value for f in loop?
Do you know how loop works?
Disha Dey
If no, I given explanation in this read comments..
https://code.sololearn.com/cgsEElzOH17d/?ref=app
+ 2
Disha Dey Am telling about here in Sololearn play ground.
If you are executing in PC with gcc or any other compilers, and if not getting any wrong or no warnings with those then it's fine. No need include std, removal of getch(). But those not from standard documentation. will not work in all compilers..
And for other statements take into considation.. If any wrong, reply again....
For the factorial, you need to include this lines
for(int i=1;i<=f;i++)
fact=fact*i;
+ 2
Why you are getting input of 'i'
You are not assign the value of f
0
I have iostream.h so i don't need namespace std; and there is need of getch()
0
But when i write for loop statement it shows 1 warning
functions containing for are not expanded inline. how can i remove it .
0
From these statements :
cin>>f;
for(i=0;i<n;i++)
fact=1;
fact=fact*f;
Going wrong is like:
You input for f for ex :4
The loop executes i=0 to n-1 times assigning fact=1, only. And here n is still not assigned any value... It may take garbage value or don't run at all.
Next fact=fact*f=1*4=4 output you get...
So change loop like
for(i=1;i<=f;i++)
fact=fact*i;
It works like this from i=1 to f
Executing fact=fact*i; hoping you know how this loop works...
0
after changing i'm still getting the warning
0
Remove fact=1 after loop.
Assign fact=1 before loop or at initializing in
int fact=1;.
You need to take input for f;
cin>>f;
0
cin >>f ;
fact=1;
for(i=1; i<=f; i++)
Still there is the same warning
0
Which one? Copy paste exact code here...
And in c++, main() return type is int according to standards. I can't guess about the compiler you are using..
int main()
0
Now can u tell me why i'm still getting the warning and is my program is correct or not.
0
https://code.sololearn.com/chUz2JHQyvBg/?ref=app can u see it please.
0
OK. Now it's fine..
Do you need or want any changes?
0
No but i'm using turbo c which is in the play store and in that the warning is still showing
0
I think, because turbo c is not updated since long ago. That does not support latest changes in c++.
For ex: All cin, cout functions some others are trasffered from <iostream.h> to <iostream>. Namespaces are added. In all books, lessons are still according to turbo c.
So we supposed to follow latest changes.
If you want remove there errors also, include
Old dot.h file only..
main() change to void main()..
Edit:
If you still confused why, and want to know more read these.
https://www.quora.com/Why-is-Turbo-C++-still-being-used-in-Indian-schools-and-colleges
https://www.google.com/amp/s/galdin.dev/blog/why-you-shouldnt-be-using-turbo-c/amp/
0
Ok thank you for the help
0
If you want know further some more clarifications read above link...
And
You are Wel come...
0
so you made amistake in private member the int you taken is not applicable so just remove fact,i,and n only take one int i and then assign in public member i=x and in factorial(int i) made another fuction for logic only and call that fuction your output will be display.