+ 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(); }

23rd Jan 2020, 3:06 PM
Disha Dey
Disha Dey - avatar
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...
23rd Jan 2020, 3:24 PM
Jayakrishna 🇮🇳
+ 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
23rd Jan 2020, 4:50 PM
Jayakrishna 🇮🇳
+ 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;
23rd Jan 2020, 3:34 PM
Jayakrishna 🇮🇳
+ 2
Why you are getting input of 'i' You are not assign the value of f
24th Jan 2020, 1:16 PM
Gopal Agarwal
Gopal Agarwal - avatar
0
I have iostream.h so i don't need namespace std; and there is need of getch()
23rd Jan 2020, 3:29 PM
Disha Dey
Disha Dey - avatar
0
But when i write for loop statement it shows 1 warning functions containing for are not expanded inline. how can i remove it .
23rd Jan 2020, 3:47 PM
Disha Dey
Disha Dey - avatar
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...
23rd Jan 2020, 3:57 PM
Jayakrishna 🇮🇳
0
after changing i'm still getting the warning
23rd Jan 2020, 4:11 PM
Disha Dey
Disha Dey - avatar
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;
23rd Jan 2020, 4:24 PM
Jayakrishna 🇮🇳
0
cin >>f ; fact=1; for(i=1; i<=f; i++) Still there is the same warning
23rd Jan 2020, 4:32 PM
Disha Dey
Disha Dey - avatar
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()
23rd Jan 2020, 4:36 PM
Jayakrishna 🇮🇳
0
Now can u tell me why i'm still getting the warning and is my program is correct or not.
23rd Jan 2020, 4:43 PM
Disha Dey
Disha Dey - avatar
23rd Jan 2020, 5:15 PM
Disha Dey
Disha Dey - avatar
0
OK. Now it's fine.. Do you need or want any changes?
23rd Jan 2020, 5:31 PM
Jayakrishna 🇮🇳
0
No but i'm using turbo c which is in the play store and in that the warning is still showing
23rd Jan 2020, 5:59 PM
Disha Dey
Disha Dey - avatar
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/
23rd Jan 2020, 6:22 PM
Jayakrishna 🇮🇳
0
Ok thank you for the help
23rd Jan 2020, 6:28 PM
Disha Dey
Disha Dey - avatar
0
If you want know further some more clarifications read above link... And You are Wel come...
23rd Jan 2020, 6:33 PM
Jayakrishna 🇮🇳
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.
24th Jan 2020, 4:15 AM
Nikunj Bhavsar
Nikunj Bhavsar - avatar