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


ADVERTISEMENT

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++ :: 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/C++ :: Find Sum Of Prime Factors Of User Input Integer

Sep 16, 2014

As an assignment for school , I've to write a program to find the sum prime factors of a user input integer.

E.g. 20 = 2 x 2 x 5 , Sum = 2 + 2 + 5 = 9
E.g. 10 = 2 x 5 , Sum = 2 + 5 = 7

My method for finding the result is as follows :

- Divide the number by increasing values if int i , starting from i=2.

- Once I get a value of i that can divide the number without giving me a remainder , I add this value of i to int sum and divide the number by i.

- I will repeat this process until the user input value is equal to 1.

My code is as shown:

#include<stdio.h>
int primecheck(int n); // Function to check if i is prime
int primesum(int n); // Function to sum the values of i that are prime
int main(void) {
int n;
int sum;
printf("Enter a number (> 1): "); //Prompting and scanning user input ,n
scanf("%d",&n);
sum = primesum(n);

[Code] .....

But for some reason I keep getting an incorrect result, it's as if it is missing out the last factor for each case.

Eg. 20 = 2 x 2 x 5 , the result I get is 4 , which is 2+2
Eg. 40 = 2 x 2 x 2 x 5 , the result I get is 6 , which is 2+2+2;

I've looked through my code numerous times.

View 7 Replies View Related

C++ :: Prime Number Program Not Working With Odd Non-prime Numbers

Dec 2, 2014

I have a program that gets prime numbers that doesn't work. It looks like it works for all prime numbers except odd non-prime ones. Here is the code:

Code:
#include <iostream>
using namespace std;
bool isPrime(int prime) {
while(true) {
int track = 0;

[Code] ...

View 3 Replies View Related

C/C++ :: Finding All Prime Numbers To Nth Prime Number

Aug 24, 2014

I'm a new coder for C++, and I've recently learned java before starting this programming language. I'm attempting to find all prime numbers up to the number a user enters (i.e. User enters 10, System shows "1,2,3,5,7"),

#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char** argv) {
int num;
cout << "Enter a positive number." << endl;

[Code] ....

I've been looking at my forloop, and I can't seem to pinpoint the problem with it.

View 1 Replies View Related

C :: Calculate Prime Numbers In Range And Return Count Of Prime Numbers

Apr 28, 2013

I wrote a program which sends a starting and ending range to other processes and the processes calculate the prime numbers in that range and return the count of prime numbers to the head process, process 0. But this is not working properly at the moment. I realize I still have to split up the range based on how many processes I have...I still have not figured out how I want to set that up. I

Code:

#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
int isPrime(int num);
int main(int argc, char **argv){
}

[code]....

View 7 Replies View Related

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 :: 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++ :: 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 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++ :: 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 :: 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 :: 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++ :: 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 :: Assign Integer Value To Unsigned Char Array But It Is Not Storing Integer Values

Oct 25, 2013

I am trying to assign the integer value to unsigned char array. But it is not storing the integer values. It prints the ascii values. Here the code snippet

Code: uchar uc[100];
for(i=0;i<100;i++)
{
uc[i] = i;
}

The values which are stored in uc[] is ascii values.I need the integer values to be stored in uc[]. I tried to do it with sprintf. but the output is not as expected. if I print the uc[i] it should diplay the value as 0,1,2....99.

View 9 Replies View Related

C++ :: Changing Integer Into New Integer With Simple Mathematical Operations?

Jun 15, 2014

changing a 9 digit integer into a new 9 digit integer through simple mathematical operations. For example, I need to change 123456789 into the new digit 456123789. Sometimes I need to change a 9 digit integer into an 8 digit integer. An example is 789062456 into 62789456. I can ONLY use simple mathematical operations (addition, subtraction, multiplication, division and modulo).

View 4 Replies View Related

C/C++ :: Converting 8-byte Integer To String Based Integer

Oct 15, 2014

Complete the function myitohex so that its converts 8-byte integer to string based hexadecimals.

Ex: printf("0x%s",myitohex(64,hex)); should printout "0x40" on the screen
char *myitohex(uint8_t i, char *result){
???
return result;
}

I wrote:

#include <stdio.h>
#include <stdint.h>
char *myitohex(uint8_t i, char *result){
*result = i + '0';

[Code] ....

0xp gets printed out which is wrong. I think *result = i + '0'; is wrong. What changes should i do on this row ?

View 4 Replies View Related







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