I'm trying to find nCr value. There is no error but I'm getting 1 as the answer for all the inputs. Help me find the solution please.
#include <stdio.h>
int fact(int num)
{
int f=1,i;
for(i=1;i<=num;i++)
{
f=f*1;
}
return f;
}
int main(void)
{
int n,r,ncr=0;
printf("\n enter n and r values");
scanf("%d%d",&n,&r);
ncr=(fact(n) / (fact(r) * fact(n-r)));
printf("\n ncr for %d and %d is %d",n,r,ncr);
return 0;
}
It should not be f = f *1, but rather f = f * i
Your factorial code is incorrect.
You set f = 1, then do f = f * 1 a bunch of times. Then return f which is still 1. I think you mean f = f*i right?
int fact(int num)
{
int f=1,i;
for(i=1;i<=num;i++)
{
f=f*i;
}
return f;
}
Your method to compute factorial needs a correction. Should be :
int fact(int num)
{
int f=1,i;
for(i=1;i<=num;i++)
{
f=f*i;
}
return f;
}
Also, nCr is not defined if r > n. You should add this check after your scanf call.
Something like :
if (r > n) printf("r cannot be greater than n.").
A more standard form of computing factorial is one that uses recursion.
int fact(int num)
{
if (num == 1 || num == 0)
return 1;
else
return (num * fact(num - 1));
}
A better and faster approach would be to calculate nCr like this
int nCr(int n, int r) {
if (r > n / 2) r = n - r;
int ans = 1, i;
for (i = 1; i <= r; i++) {
ans *= n - r + i;
ans /= i;
}
return ans;
}
Related
the question is you have write a program that find the GCD of two number and you should define a function to do that but the challenge is that the body of function shouldn't be more than 1 line and you can use only one semicolon! I tried so far and wrote this. do you have any idea?
#include <stdio.h>
int bmm(int m, int n) {
while(m!=n) int a = m > n ? m -= n : n -= m;
return m;
}
int main() {
int m, n;
scanf("%d%d", &m, &n);
if (m < 0)m = -m;
if (n < 0)n = -n;
printf("%d", bmm(m,n));
return 0;
}
You can solve it in one line in a recurive way:
int gcd(int a, int b)
{
return (b == 0 ? a : gcd(b, a % b));
}
You can use if...else statement like this:
#include <stdio.h>
int bmm(int m, int n)
{
while(m!=0&&n!=0)
{
if(m>n)m-=n;
else n-=m;
}
return m;
}
int main()
{
int m, n;
scanf("%d%d", &m, &n);
if (m < 0)m =- m;
if (n < 0)n =- n;
printf("\n GCD = %d", bmm(m,n));
return 0;
}
#include <stdio.h>
int rev(int num);
int main()
{
int num=123;
printf("\n%d",rev(num)/10);
return 0;
}
int rev(int num)
{
if(num==0)
{
return 0;
}
else
{
return ((num%10)+rev(num/10))*10;
}
}
long reverse(int n) {
static int r = 0;
if (n == 0)
return 0;
r = r * 10;
r = r + n % 10;
reverse(n/10);
return r;
}
in each iteration you take the once digit added to the static r and multiply the result with 10,
and call the recursive function with the number without this digit n/10
I wanted to write a simple program to calculate the factorial of a given number using C. Yet my code seems to have some logical error that I can't detect. Would be glad for help.
int fact(int n);
int main(void)
{
int num = get_int("Type number: ");
printf("%i\n", fact(num));
}
//define function
int fact(int n)
{
for (int i = 1; i < n; i++)
{
n *= i;
}
return n;
}
You can't use n to calculate.
You have to save total with another variable
int fact(int n)
{
int product = 1;
for (int i = 1; i <= n; i++)
{
product = product * i;
}
return product;
}
In mathematics, the factorial of a positive integer N, denoted by N!, is the product of all positive integers less than or equal to N:
N!=N*(N-1)*(N-2)*(N-3)*.......*1
+-------------------------+
notice that this is: (N-1)! <==> So, N! = N*(N-1)!
we can use these mathematical facts to implement the factorial function in 2 different forms, recursive and iterative approaches:
recursive approach
size_t rec_factorial(size_t n)
{
/*Base case or stopping condition*/
if(n==0)
{
/* 0! = 1 */
return 1;
}
/*n! = n * (n-1)!*/
return n * rec_factorial(n-1);
}
iterative approach
size_t factorial(size_t n)
{
size_t j = 1;
size_t result = 1;
while(j <= n){
result *= j; /* n!=n*(n-1)*(n-2)*(n-3)*....1 */
++j;
}
return result;
}
I'm trying to write a code in C to calculate the accurate of Stirling's approximation from 1 to 12.
Here's my code:
#define PI 3.1416
#define EULERNUM 2.71828
float stirling_approximation(int n) {
int fact;
float stirling, ans;
fact = factorial(n);
stirling = sqrt(2.0*PI*n) * pow(n / EULERNUM, n);
ans = fact / stirling;
return ans;
}
int factorial(int input) {
int i;
int ans = 0;
for (i = 1; i <= input; i++)
ans += i;
return ans;
}
int main(void) {
int n;
printf(" n\t Ratio\n");
for (n = 1; n <= 12; n++) {
printf("n: %2d\t %f\n", n, stirling_approximation(n));
}
return 0;
}
I'm getting the recursive calculation correctly, but my Stirling's approximation method value is way off. And what's even more puzzling is the answers for n = 1, 3 is correct.
I think it has something to do with calling the approximation function from the main function. I know it must be such a simple mistake but I've been trying to fix this for the entire day! Could anyone please help me?
Thank you!
You incorrectly implemented the factorial method
int factorial(int input) {
int i;
int ans = 0;
for (i = 1; i <= input; i++)
ans += i;
return ans;
}
It should be
int factorial(int input) {
int i;
int ans = 1;
for (i = 2; i <= input; i++)
ans *= i;
return ans;
}
I would also change the return value from int to long int.
I'm trying to write a function that adds the odd digits of one number to the end of a second number, without using arrays. Here's what I have so far:
#include <stdio.h>
#include <math.h>
int NumberAdd (int n, int m)
{
int power=1,x=0,counter=0,newnumber;
while(n!=0)
{
if(n%2!=0)
{
x=x+(n%10)*power;
power*=10;
counter++;
}
n=n/10;
}
newnumber=m*pow(10,counter)+x;
return newnumber;
}
int main ()
{
int m,n;
scanf("%d %d", &n, &m);
printf("%d\n", NumberAdd(n,m));
return 0;
}
Now, from my experience, this should work, and it does work if the first number is an odd number, but if the number I'm trying to get the odd digits from is an even number I get problems. For example:
For 3457 and 3458 I get 3458357 as a result. But, for 3456 and 3457 I get 345734. Any ideas why?
It is better to avoid floating point arithmetic due to possible rounding issues. Try something like this (could be made shorter, but I tried to keep the original structure):
int NumberAdd(int n, int m)
{
int power = 1, x = 0, newnumber;
while (n != 0)
{
if (n % 2 != 0)
{
x = x + (n % 10)*power;
power *= 10;
}
n = n / 10;
}
newnumber = m * power + x;
return newnumber;
}
#define MAXNUM 1000000000
int NumberAdd (int n, int m)
{
int quotient, divisor = MAXNUM;
do {
quotient = n / divisor;
if (quotient & 1)
m = m * 10 + quotient;
n = n - quotient * divisor;
divisor /= 10;
}
while (divisor);
return m;
}