Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

239
Visualizações
C program to calculates the number of ways to choose k objects from n distinct objects. 'k' and 'n' both are integers

I wrote a C program to calculate the number of ways to choose k objects from n distinct objects using functions.

    #include<stdio.h>
    long f(int a)
    {
     if(a==1||a==0)return(0);
     else return(a*f(a-1));
    }

    int combination(int N,int K)
    {
     long int NF,KF,NMKF;
     NF=f(N);
     KF=f(K);
     NMKF=f(N-K);
     return(NF/(KF*NMKF));

    }
    int main()
    {
     int n,k;
     scanf("%d%d",&n,&k);
     combination(n,k);
    }

But the compiler shows following error message

floating point exception (core dumped)

How to avoid it?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

The problem is in this line

if(a==1||a==0)return(0);

It should be

if(a==1||a==0)return(1);

While calculating factorial, n*(n-1)*(n-2)...*(2)*(1). Notice in the end, we multiply by 1 and not zero. multiplying with 0 would make the factorial 0. And later when you are performing division, the 0 comes in the denominator, and floating point exception is occurring. That's why your program is giving error.

For cases when factorial of 0 is needed. Then also this would work, because factorial of 0 is 1 and not 0.. Check this out.

over 4 years ago · Santiago Trujillo Relatório

0

Two problems:

  1. if(a==1||a==0) you should return 1, not return 0. Because 1!=1, 0!=1.
  2. Your intention is choose k objects from n distinct objects. But You should add param checking in order to not occur the n<k. If we input n=2, k=3.The program will go to error. It is bad! I hope this can help you.
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda