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
ADVERTISEMENT
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
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
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
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
View Related
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Feb 6, 2014
I define "Comwords" as a string, but apparently it takes the members as chars, then I can't set strings in a structure equal to the chars.
I see to also be having unknown problems with the ComMAL array and loading it values into another element of the same structure.
How to correct this? I was thinking of casting char elements as strings, but could find no reference in my library book regarding how to do that (lots on casting int's a doubles...)
Code:
int _tmain(int argc, _TCHAR* argv[]) {
int comm = 10;
int targ = 5;
int death;
struct AI_WORDS
[Code]....
View 2 Replies
View Related
Jul 1, 2014
Using a for loop, construct two 100 element arrays, x and y, such that element i of x stores the value sin(2*pi*i/100)) and the corresponding element of y stores cos((2*pi*i/100)). Print the values stored in the elements of x and y as you calculate them.
I have attempted to solve it but I'm not sure why the value 0 is only being printed, maybe I haven't assigned sin(2i/100)) and cos((2i/100)) to the arrays properly?
Code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main () {
[Code] .....
View 3 Replies
View Related
Aug 9, 2012
It is given an integer "p". I have to find a number "n" for which "n factorial" has "p" numbers of zero at the end. Here is the solution i thought, but i am not sure if it is a solution for this problem. Do i have to make a function to calculate the factorial and another function to get the zeros?
int p;
int count5=0;
int i;
int copy_i;
printf("Enter p: ");
scanf("%d",&p);
for(i=1; ;i++) {
[Code] .....
View 1 Replies
View Related
Dec 17, 2014
int arr[4][5];
for (int(&row)[5] : arr){
for (int &column : row){
} }
Why do we name it row instead column?
int arr[row][column]
Wouldn't this be like
int arr[4][5];
for (int(&column)[5] : arr){
for (int &row: column){
}
It just seems funny to me because we use row[5] which should be 4 if we meant row? Or did I read it wrong in C++ prime !?
View 2 Replies
View Related
Mar 6, 2015
Whenever I put my dll files in the same directory as my main.cpp and use this code, Code: g++ main.cpp -L. -lMyDll it works.
but if my dll files are in the other directory, Code: g++ main.cpp -L/myDlls -lMyDll it won't work. Why is this not working?
View 6 Replies
View Related
Jul 5, 2014
i am currently trying to get SDL working with C language.I am using Cygwin and Sublime text 2.I know and understand general programming, however i am trying to understand how to get SDL working with a file i create i.e test.c. i know to include which i have downloaded
#include <SDL.h>
where do i add the libraries so that i can use them in my program?
View 9 Replies
View Related
Apr 16, 2014
I am writing an irc bot in c++ on linux mint, and its not connecting right for some reason. anyways, heres the link: [URL].... . it was connecting to quakenet before I added the functions, but now it just says connecting... and then quits.
View 12 Replies
View Related