Hello everyone,i am a beginner in c++ and i am trying to write a program that outputs the factorial of a number in my own way,however i don't know why it's not working.Thanks for any help.This is the code: - #include<iostream.h>
-
#include<string>
-
using namespace std;
-
int main(void){
-
int fac;
-
int n;
-
string b;
-
cout<<"this program produces the factorial of a number"<<"/n";
-
cout<<"type an integer"<<"/n";
-
cin>>n;
-
if(n>0){
-
int c=1;
-
for(int a;n>=0;n=n-1){
-
a=n-1;
-
b=a*n;
-
c=b*c;
-
}
-
cout<<c;}
-
cin>>b;
-
}
12 4043
There are a couple things wrong with your program, as well as with your algorithm:
First, you have declared an integer named fac in the first line of main() - when do you use it? Were you planning to use it?
Second, you declared b as a string, but then try to use it like an integer within your loop - why is it a string? Should it be an int?
I'm not sure how your loop is supposed to be finding the factorial of n - could you try and explain what you're trying to do?
There are a couple things wrong with your program, as well as with your algorithm:
First, you have declared an integer named fac in the first line of main() - when do you use it? Were you planning to use it?
Second, you declared b as a string, but then try to use it like an integer within your loop - why is it a string? Should it be an int?
I'm not sure how your loop is supposed to be finding the factorial of n - could you try and explain what you're trying to do?
Thanks for taking the time.Firstly c was supposed to be fac.So try to ignore fac.[mistakes I made after the frustration of not running the program successfully]b is supposed to be an integer and there is supposed to be another variable at”cin>>b” which should make the program wait for user input”I am trying to decrement n continuously while it it is greater than zero.
Since fac of n=n*n-1*n-2
So what I am trying to do is to reduce n continuously and multiply it by a[which is n-1]
And I try to store the value of n*n-1 in b then store b*c in c.Where c at first is one,but afterwards will take the previous value of c and multiply it by the new value of b which is updated for each iteration due to the fact that n is being decreased. Therefore when the loop stops [when n ceases to be greater than 0]the value of c should be the factorial of n
Since fac of n=n*n-1*n-2
So what I am trying to do is to reduce n continuously and multiply it by a[which is n-1]
And I try to store the value of n*n-1 in b then store b*c in c.Where c at first is one,but afterwards will take the previous value of c and multiply it by the new value of b which is updated for each iteration due to the fact that n is being decreased. Therefore when the loop stops [when n ceases to be greater than 0]the value of c should be the factorial of n
Now you've made me dizzy ;-) This is how I would do it by hand. 'n' is the
number for which the factorial should be calculated: -
fac= 1;
-
if (n < 1) stop;
-
fac= fac*n;
-
n= n-1;
-
goto step 2;
This does all the multiplications starting at the highest numbers; if you want to
start at the lowest numbers something like this will do: -
fac= 1;
-
i= 1;
-
if (i >= n) stop;
-
fac= fac*i;
-
i= i+1;
-
goto step 3;
All you have to do now is translate that pseudo-code to C/C++ and remove the
unused variables.
kind regards,
Jos
Hello everyone,i am a beginner in c++ and i am trying to write a program that outputs the factorial of a number in my own way,however i don't know why it's not working.Thanks for any help.This is the code: - #include<iostream.h>
-
#include<string>
-
using namespace std;
-
int main(void){
-
int fac;
-
int n;
-
string b;
-
cout<<"this program produces the factorial of a number"<<"/n";
-
cout<<"type an integer"<<"/n";
-
cin>>n;
-
if(n>0){
-
int c=1;
-
for(int a;n>=0;n=n-1){
-
a=n-1;
-
b=a*n;
-
c=b*c;
-
}
-
cout<<c;}
-
cin>>b;
-
}
Hi dynamo,
Sorry I could not understand what you were trying to do inside the "for" loop.
Thought the following would be helpful: -
#include<iostream.h>
-
-
int main()
-
{
-
int n,c=1;
-
cout<<"Enter a positive integer:";
-
cin>>n;
-
if(n>0)
-
{
-
for(c=1;n>1;n--)
-
{
-
c=c*n;
-
}
-
}
-
cout<<"Factorial:"<<c;
-
}
-
You can also use recursive functions to do this job. The body of the function goes as follows: -
int fact(int n)
-
{
-
return (n==0?1:n*fact(n-1));
-
}
-
(Assuming n is not negative)
Hope this was helpful.
Regards,
Pradeep.
Hi dynamo,
Sorry I could not understand what you were trying to do inside the "for" loop.
Thought the following would be helpful: -
#include<iostream.h>
-
-
int main()
-
{
-
int n,c=1;
-
cout<<"Enter a positive integer:";
-
cin>>n;
-
if(n>0)
-
{
-
for(c=1;n>1;n--)
-
{
-
c=c*n;
-
}
-
}
-
cout<<"Factorial:"<<c;
-
}
-
You can also use recursive functions to do this job. The body of the function goes as follows: -
int fact(int n)
-
{
-
return (n==0?1:n*fact(n-1));
-
}
-
(Assuming n is not negative)
Hope this was helpful.
Regards,
Pradeep.
This is the best way.
However I would rather make n to be of unsigned long int type, because for a little bigger number n u allready pass int limit of 32,767, and function to return unsigned long int too!
:)
Savage
This is the best way.
However I would rather make n to be of unsigned long int type, because for a little bigger number n u allready pass int limit of 32,767, and function to return unsigned long int too!
:)
Savage
According to the C standard sizeof(int) <= sizeof(long) and on a lot of nowaday
systems both ints and longs are at least four bytes in size. The unsigned
qualifier doesn't buy you much because the unsigned version max number is
just twice the signed number max number, e.g. for both signed and unsigned
four byte values the maximum factorial number they can hold is 13! and 13 > 2.
kind regards,
Jos
According to the C standard sizeof(int) <= sizeof(long) and on a lot of nowaday
systems both ints and longs are at least four bytes in size. The unsigned
qualifier doesn't buy you much because the unsigned version max number is
just twice the signed number max number, e.g. for both signed and unsigned
four byte values the maximum factorial number they can hold is 13! and 13 > 2.
kind regards,
Jos
Better then short int which can hold up to 8!
I guess that long double is than a best solution.
It can go up to 1755!
Savage
Thanks for all the help.I have not been on the internet of recent or I would have been
active in the discussion.Thanks again,you have covered every aspect but i would like to ask what makes c++ so special.
Thanks for all the help.I have not been on the internet of recent or I would have been
active in the discussion.Thanks again,you have covered every aspect but i would like to ask what makes c++ so special.
We are more then happy to help u!
What makes C++ special?
Take a look here
Savage
#include<iostream.h>
int main(void)
{
int number;
int sum=1;
cout<<"Enter number for factorial :";
cin>>number;
for(int i=number;i>=1;i--)
{
sum*=number;
number--;
}
cout<<sum;
}
We are more then happy to help u!
What makes C++ special?
Take a look here
Savage
Interesting discussion.Does not explain why C++ is better than python though.
Interesting discussion.Does not explain why C++ is better than python though.
:P
Savage
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Martin Jørgensen |
last post by:
Hi,
Consider (factorial.cpp):
#include <iostream>
using namespace std;
double R=3.2; /* not used, but R is static because it is a global
variable (file scope) */
|
by: aNt17017 |
last post by:
This is my code:
long fact(int n)
{
if (n == 0)
return(1);
if(n > 100)
{
printf("\t\tERROR: %d is too large for factorial.\n", n);
return 1;
|
by: salman |
last post by:
this program is giving compile time error. so plse ge me the logic of
factorial
# include <iostream.h>
# include <math.h>
void main()
{
int f,sum=0,i,j,n;
cout<<"\nEnter Number: ";
|
by: Tin |
last post by:
Dear Sir/Madam,
I have one assignment , which need me to
write a program to calculate the factorial of an integer,
then press the button that would display the result in a label.3
& the...
|
by: yusuke |
last post by:
can you please answer this:thanks!!!
write a factorial function that returns a factorial of a long int number. Enter the number at the main funtion and call factorial() to carry on calculations...
|
by: Umesh |
last post by:
i wrote the following program to calculate factorial:
#include<stdio.h>
#include<iostream.h>
void main()
{
int i,n;
long int p=1; // or long double p=1; for exponential result which I
don't...
|
by: Sugandh Jain |
last post by:
Hi.
How to write a function that will return me the factorial (say in a string)
for the any positive integer it takes?
When we find a factorial of even say 2000 or a higher number, it will be...
|
by: becky808 |
last post by:
Hi,
I'm trying to write a program to calculate n factorial but it won't compile. Can anyone tell me what I'm doing wrong?
#include <iostream>
#include <cmath>
using namespace std;
int...
|
by: Blue sky |
last post by:
Hi,I am a new C++ learner.The follow prgram produces a wrong
result,but I can't find the wrong.Can you help me?Thank you!
#include<stdio.h>
long factorial( long number);
int main()
{
int...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |