C++ :: For Loop Multiplication - X Factorial

Oct 18, 2013

int main() {
int x;
int y=1;
for(x=1 ; x<=12 ; x++ , y=y*x ) {
cout<<y<<'
';
}
}

This code give x! ( x factorial).

whenever ,in x<=12 , we change the 12 to any number n where n>=1 and n<=12

the code works and give the (x factorial)

for example 12!=479001600

the problem begins with the number 13.

when we put 13 like this x<=13 or for(x=1 ; x<=13 ; x++ , y=y*x )

the compiler gives a wrong result : it gives 1932053504 which is wrong because the true result is 6227020800

View 1 Replies


ADVERTISEMENT

C++ :: Using While Loop For Factorial

Mar 2, 2014

I need to write a complete program using "While Loop" to calculate 1! to 12! using just "int" variables. Only from 1 to 12 and there are no other inputs.. This is my first time using While loop.

View 2 Replies View Related

C :: Program To Calculate Factorial Of Numbers - For Loop

Feb 5, 2013

I have been working on a program to calculate the factorial of numbers. Part of my code is copied and modified from the FAQ about validating numbers in user input.

I have encountered a problem with the for loop that I am using near the end of my code. No matter what I do, it seems that my loop only does the multiplication of b = a*(a-1) and then prints. For example, inputting 5 will result in a print of 20, but the factorial is 120.

Code:
int main(void) {
char buf[BUFSIZ];
char *p;
long int a;
long int b;
long int i;

printf ("Enter a number to be factorialized: ");

[Code] ....

View 5 Replies View Related

C :: Factorial Using Prime Numbers?

Feb 3, 2014

Code:

#include <stdio.h>
int find_next_prime(int num);
int is_prime(int num);
int main()

[Code]......

How would I go about counting the number of times a factorial has a specific prime number?

For example, 5! = (2^3)*(3^1)*(5^1), 6! = (2^4)*(3^2)*(5^1).

How would I begin to design my find_prime_count function in order to count how many times each occurs? My program is to read in a number between (2<=N<=100) from a text file and output the results exactly like above which I still have to figure out after, I'll assume I have to use fscanf.

View 8 Replies View Related

C :: Working Out Factorial Using Arrays

Nov 26, 2013

The code works and comes out with a correct factorial up to 69! (But this is fine I only need it to work up to 60). I was wondering if this could be simplified or optimized in any way.

Code:

#include <stdio.h>
int main(){
int number[100]; /*Created array of size 100 */
int n=34; /*19931120 summed to make 34 */
int i;

[Code].....

View 4 Replies View Related

C++ :: Calculate The Factorial With Function?

Jan 3, 2013

calculate the factorial with function

The program should then calculate the factorial of the number n, where n!= n×(n −1)...× 2×1

the output like this:

Enter number: 4
Factorial of 4! = 24
Enter number: 6
Factorial of 6! = 720
Enter number: 3
Factorial of 3! = 6
Enter number: 0
Factorial of 0! = 1
Enter number: -5
Factorial of -5! = -1
Press any key to continue . ....

View 7 Replies View Related

C/C++ :: Factorial Program With Recursion

Nov 9, 2014

I put some checks in factorial program, I am confused how 27, 40 50 is coming in output and ans is 10.

#include<stdio.h>
int fact(int n) {
if(n==1) {
printf("hello1 %d
",n);
return 1;

[code].....

View 2 Replies View Related

C/C++ :: Factorial Of Large Numbers

Jul 29, 2014

I have designed a code and still i dont get the desired output ....

#include <iostream>
using namespace std;
int fact(int,int);
int arr[200]={1};
int main() {
int n,t,len=0,i=0,j,a;
cin>>t;

[Code] ....

View 3 Replies View Related

C/C++ :: Getting Prime Factorial Of Integer?

Sep 23, 2013

code of prime factorization. Example input:135 / The problem is I want to be an output of (3^3) (5^1) instead of 3,3,3,5.

here's the code:

#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;  
void get_divisors(int n);  

[code]....

View 3 Replies View Related

C++ :: Extra Parameter In Call To Factorial

Mar 2, 2013

While running it gives the runtime error: "Extra parameter in call to factorial."

#include<iostream.h>
#include<conio.h>
int factorial(long int);
void main() {
clrscr();
long int a;
cout<<"This program displays the factorial of the number you enter."<<endl;

[Code] .....

View 15 Replies View Related

C++ :: Identifying Error In Factorial Function

Apr 8, 2014

double expression(int n, double x) {
double num=(pow((x),2*a));
double den=fact(n,a);
double sum=0;
for(int a=0; a>=0; a++)
sum=pow(x,n)*(num/den);
return sum;
}

I don't know why this isn't working. fact is a factorial function which I have tested and it works fine. But when I input, for example:

int main() {
cout<<expression(1,3)<<endl;
getch();
return 0;
}

it comes up with an error.

View 5 Replies View Related

C/C++ :: Factorial Program - Input Number

Mar 26, 2014

I'm trying to run a factorial program but I'm getting the error statement has not effect.

here's the code

#include <iostream>
using namespace std;
int main() {
int fac = 0;
int sum = 0;

[Code] ....

the error happened in the int main section

View 3 Replies View Related

C/C++ :: Classes - Find Factorial Of Any Given Function

Aug 2, 2014

I wrote this code to find the factorial of any given function ., works fine but when i put it in a class... it gives me an error ::assignment of read only variable fact;

#include <iostream>
using namespace std;
const static int fact=1;
class My_Factorial {
public:
int x;
void Get_Number(){
cout<<"enter a number to find its factorial";

[Code] ....

View 3 Replies View Related

C :: Function That Computes Factorial Result Of Certain Number

Aug 28, 2013

I just want to practice in the language so i wrote this simple function that computes the factorial result of a certain number. The problem is that for all numbers > 20, the results are wrong ( < 20 all good).

I already learned that normal "long" type in c is more like 32 bit int and not 64 bit like a long type in java. so I used here a "long long" type.

why am I getting strange results above the number 20? isn't 64 bit enough to hold those numbers?

Code:
long long factorial(int n);
int main() {
long long result = factorial(20);
printf("%lld",result);

[code] ...

for 21 i get: -4249290049419214848

where the right result shoud be: 51,090,942,171,709,440,000

View 8 Replies View Related

C++ :: Input From User A Positive Number N And Find Its Factorial

Oct 8, 2014

Write a C++ program that will input from the user a positive number n and find its factorial. Don’t forget to validate the input. The factorial of a positive integer n (denoted by n!) is defines as the product of the integers from 1 to n.

n! = 1* 2 * 3 * ... * (n - 1) * n

You should allow the user to continue working with your program for additional data sets.

Sample output:

Please enter a number: 5
5! = 120
Would you like to continue (Y/N)?Y
Please enter a number: 3
3! = 6

Would you like to continue (Y/N)?N
Good Bye!!

My code for what i think I'm doing is as follows:

#include <iostream>
using namespace std;
int main(){
int i=1;
int n;

[Code] ....

View 1 Replies View Related

C++ :: Program Only Accept Integer From 0-10 But Doesn't Show The Factorial

Feb 28, 2013

#include<iostream>
using namespace std;
int main() {
int a[9],x,f=1;
cout<<"Please enter an integer [0-10 only] : ";

[Code] ....

View 3 Replies View Related

C++ :: Writing Program That Computes Factorial Of Number And Displays It?

Oct 24, 2014

write a program that computes the factorial of a number and displays it. A factorial of a number (written N!) is defined as N * (N-1) * (N-2) * (N-3) ... *1 In other words, 5! = 5 * 4 * 3 * 2 * 1 = 120 and 3! = 3 * 2 * 1 + 6.

Example of output 15 is 1.30767e+012

Can't get it to display error when user enters a number lower than 2 or higher 60.

// Program to calculate factorial of a number
#include <iostream>
#include <iomanip>

[Code].....

View 13 Replies View Related

C :: Find Factorial Of Any Number Greater Than Zero And Use Gosper Formula To Approximate It

Feb 24, 2013

I was given a question in my programming class to create a program to find the factorial of any number greater than zero and to use Gosper's formula to approximate it.

Code:
#include <stdio.h>
#include <math.h>
#define PI 3.14159265
double equation(int n);
int

[Code] ....

View 2 Replies View Related

C :: Variable Won't Change When Calculating Factorial In Euler Number Homework

Sep 27, 2014

I started to learn programming through this site two weeks or so ago. I've got a book with exercices and so on, and one of them involves calculating e within a tolerance given by the user.

The formula for calculating e is the summation of 1+(1/i!), where i -> n.

So, here's the code and I'll explain the problem below:

Code:

#include <stdio.h>
int main()
{
float error;
float terme;
float sumatori = 0;
int cicle_euler = 1;
int factorial;

[Code]...

For some reason, when I set factorial to cicle_factorial, factorial remains 0, which I find puzzling; the program always halts when 1 + sumatori is 2.0 no matter what error is.

This must be a common problem and I suspect it has to do with some distinction between variables inside a loop and variables outside it, but as I lack technical vocabulary I can't seem to find anything on Google.

View 10 Replies View Related

C++ :: If Statement Multiplication By Zero

Mar 28, 2013

I want to set an integer to zero when it easy equal to another integer, but it seems that the program for some reason won't set the integer to zero. Here is the example of that code:

#include <iostream>
using namespace std;
int main () {
int n = 2;
int r = 2;
if(n==r)
n++;
r*0;
cout << " n is " << n << endl;
cout << " r is " << r << endl;
}

What am I doing wrong, it should say that "n is 3" and "r is 0".

View 2 Replies View Related

C++ :: Matrix Multiplication Using Pointers

Feb 15, 2014

Code:
#include<stdio.h>
#include<conio.h>
void main()

[Code] .....

This program on running returns an error of "ILLEGAL USE OF POINTER".

View 4 Replies View Related

C :: Multiplication Table In 2D Array

Dec 30, 2014

I want to declare a 2D 4*4 array, and fills the array with the multiplication table of an integer x from the user, for example: x*1,x*2,x*3, ....., x*16 and how to pass that array to a function to print the array and to another function that swaps a column with another column chosen by the user.

View 3 Replies View Related

C :: Matrix Multiplication Using Pointers

Feb 17, 2014

Code:
#include<stdio.h> Code: #include<conio.h>
void main() {
int *a[2][2],*b[2][2],*c[2][2],i,j,k;
Printf("
ENTER a MATRIX ELEMENTS

[Code] .....

View 4 Replies View Related

C :: Multiplication With Sort Function

Oct 30, 2014

I need to include validation to only accept numbers and to only the last four odd numbers in the table.

Code:

#include <stdio.h>void main() {
int upper, range, i;
printf("Enter an integer to find multiplication table: ");
scanf("%d",&upper);

[Code].....

View 3 Replies View Related

C++ :: How To Able To Overload A Multiplication Operator

Apr 24, 2014

How would i be able to overload a multiplication operator that if, in the main example(0, 5, 0) * example (0, 5, 0) is given, it gives me 25?

View 1 Replies View Related

C++ :: Multiplication Table Using Functions?

May 25, 2013

how to print a multiplication table of 2,3 ,4 or any table using functions in c++.. ?

Remember using "Functions" e.g Multiplication(int x)
{ }
int main{
then call the function}

View 3 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved