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
ADVERTISEMENT
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 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
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
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
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
Mar 23, 2013
If yes then how?
View 6 Replies
View Related
Sep 13, 2013
Let say i have a following scenario:
a function like this.
Code:
template <typename T1>
print (T1 x){
cout << x << "
";
}
How do I prevent user passing a class or a structure or aanoter function to my function print. I mean i know if a wrong thing is passed that i'll get an error eventually but is there a way to explicitly check what has been passed. How is this done usually ?
View 6 Replies
View Related
Nov 30, 2012
For this problem, you will design and implement 2 classes and then write a driver function to test these classes. The first will be a C++ class for an abstract data type color with a public enumeration type colorType that has the color values shown in Listing 10.8. Your abstract data type should have an attribute for storing a single value of type colortype and member functions for reading (readColor) and writing (writeColor) a color value as well as setting and accessing it. The function readColor should read a color as a string and store the corresponding color value in the value attribute of a type color object. The function writeColor should display as a string the value stored in the value attribute of a type color object (see Figure 7.5). Modify class circle and the driver function in Listing 10.9 to include and use this class. You'll need to remove the declaration for color in class circle. Test your modified driver function with the new color and circle classes.
The second class will be to design and implement a rectangle class similar to class circle. Be sure to incorporate the new color class you have written and tested in the first part of the programming exercise. Write a client program that asks the user to enter a shape name (circle or rectangle) and then asks the user for the necessary data for an object of that class. The program should create the object and display all its attributes.
The circle class .h and .cpp files as well as the original driver function will be supplied. You are to provide the .h and .cpp files for the new color class and the modified driver function as well as the .h and .cpp files for the rectangle class and the client program that uses all three classes.
What I have so far, and I'm pretty lost;
color.h
Code:
Code:
//color.h
//Color class definition
#include "stdafx.h"
#ifndef COLOR_H
#define COLOR_H
class color {
public:
enum colorType {black, blue, green, cyan, red, magenta, brown, lightgray, nocolor};
[code].....
View 4 Replies
View Related
May 18, 2013
This is a program I developed in which we had to define a class named BOOK with the data members and member functions as shown in the program..We have to:
(i) Make the user enter the values in the array BOOK.
(ii) Display the details that the user entered.
(iii) Search for a book from the array upon its Bno and display its details.
(iv) Search for a book from the array upon its Bname and display its details.
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class BOOK {
private:
int Bno;
char Bname[20];
[Code] .....
But while running it the compiler gives the errors as:
Line 43 to 48: Illegal character '' (0x5c)
Line 69: Undefined symbol 'Display'
Line 88: 'BOOK::Bno' is not accessible.
Line 89:'BOOK::Bname' is not accessible.
Line 90:'BOOK::Author' is not accesible.
Line 91:'BOOK::Price' is not accesible.
Line 108:'BOOK::Bno' is not accessible.
Line 109:'BOOK::Bname' is not accessible.
Line 110:'BOOK::Author' is not accesible.
Line 111:'BOOK::Price' is not accesible.
from 43 to 48..the line feed was also used at many other places but there it was not given as an error so why here?
Line 69: I defined the Display() function outside the class since it contained control structures, so what's the error then?
About the lines the rest of the error( the "not accessible" ones) I know these data members are not accessible because they are in private visibility mode. But then how to make them accessible? (Without putting them in public because it was a part of the question to create the data members in private).
View 1 Replies
View Related
Nov 30, 2012
I'm am having a few issues with this program, they stem from passing the color.h and .cpp into another .h and .cpp, and frankly getting how those two things really fit together.
For this problem, you will design and implement 2 classes and then write a driver function to test these classes. The first will be a C++ class for an abstract data type color with a public enumeration type colorType that has the color values shown in Listing 10.8. Your abstract data type should have an attribute for storing a single value of type colortype and member functions for reading (readColor) and writing (writeColor) a color value as well as setting and accessing it. The function readColor should read a color as a string and store the corresponding color value in the value attribute of a type color object. The function writeColor should display as a string the value stored in the value attribute of a type color object (see Figure 7.5). Modify class circle and the driver function in Listing 10.9 to include and use this class. You'll need to remove the declaration for color in class circle. Test your modified driver function with the new color and circle classes.
The second class will be to design and implement a rectangle class similar to class circle. Be sure to incorporate the new color class you have written and tested in the first part of the programming exercise. Write a client program that asks the user to enter a shape name (circle or rectangle) and then asks the user for the necessary data for an object of that class. The program should create the object and display all its attributes.
The circle class .h and .cpp files as well as the original driver function will be supplied. You are to provide the .h and .cpp files for the new color class and the modified driver function as well as the .h and .cpp files for the rectangle class and the client program that uses all three classes.
color.h
Code:
//color.h
//Color class definition
#include "stdafx.h"
#ifndef COLOR_H
[Code].....
View 3 Replies
View Related
Oct 5, 2013
I have an array of (Student)classes created in Manager.h, which contains a new instance of class Name (name),(in Student.h)How would I go about accessing the SetFirstName method in Name.cpp if I was in a class Manager.cpp? I have tried using Students[i].name.SetFirstName("name");
// In Manager.h
#include"Student.h"
class Manager
{
[Code]....
View 2 Replies
View Related
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
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
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
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
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
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
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
Oct 14, 2012
find the address of function? mail the ans on (email removed)
View 4 Replies
View Related