473,387 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

factorial program in C

I code in RPG but recently took a software testing test with that included the following code:

n! = n*((n-1)!)
int f=1;
if (n<0){
fprintf(stderr, 'factorial; Negative argument.'n';
}else if ((n==0) ||(n==1)){
f=1;
}else {
f=n*factorial(-n);
}
return(f)
}

what does the statement: {else if ((n==0)||(n--1)){ mean? The question is what inputs represent test cases that would give decision coverage with the minumum number of test cases? The answer is -1, 1, 5. I understand the -1 and the 5 but not the positive 1. I thought the answer would be -1, 0, 5. I code in RPG not in C so I attributed my incorrect answer to my lack of knowledge of C. Any help would be much appreciated.
Nov 29 '08 #1
4 3591
oler1s
671 Expert 512MB
> what does the statement: {else if ((n==0)||(n--1)){ mean?
Else if (n is 0) or (n is 1):

The answer is -1, 1, 5. I understand the -1 and the 5 but not the positive 1. I thought the answer would be -1, 0, 5. I code in RPG not in C so I attributed my incorrect answer to my lack of knowledge of C.
I doubt your answer was wrong because of not knowing C. Actually, between their answer and your answer, I would favor yours. They have 1 and 0 as a base case for factorial, but it is possible to implement factorial with only 0 as a base case. For that reason, 0 should be tested, and 1 can be omitted.
Nov 30 '08 #2
weaknessforcats
9,208 Expert Mod 8TB
It means that both 0! and 1! are 1.

Without this test, the recursion would be ininfite.
Nov 30 '08 #3
svlsr2000
181 Expert 100+
by the way you should be having f=n*factorial(n-1); instead of f=n*factorial(-n);
Dec 1 '08 #4
Ganon11
3,652 Expert 2GB
Unless he forgot to type in an extra - making it f=n*factorial(--n);
Dec 1 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

11
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) */
35
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;
8
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: ";
1
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...
59
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...
3
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...
12
by: dynamo | last post by:
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...
1
by: doctor309 | last post by:
hello friends iam not able to get logic behind solving the question mentioned below,,,,i tried a lot using various books.....online tutorials but at the end just lost it...... you ppl are kindly...
2
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...
3
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.