#include <stdio.h> float factorial(float num) { float result = 1; for(int i = 1;
ID: 3727209 • Letter: #
Question
#include <stdio.h>
float factorial(float num)
{
float result = 1;
for(int i = 1; i < num; i ++)
result *= i;
return result;
}
void bells(int n)
{
float sum = 1;
printf("Bell Number [0] = 1 ");
for(int i = 1; i <= n; i++)
{
printf("Bell Number [%d] = %.0lf ", i, sum);
sum += factorial(n) / (factorial(i)*factorial(n - i));
}
}
int main()
{
int sum = 0;
int restart = 1;
//while is used to restart program on users request
while(restart == 1)
{
int userNum;
printf("Enter a number :");
scanf("%d",&userNum);
bells(userNum);
//Restarting program on users request
printf(" Restart program? (1 = yes : 0 = no) ");
scanf("%d", &restart);
}//end of restart while
}//end of main
Explanation / Answer
#include <stdio.h>
float factorial(float num)
{
float result = 1;
// you made a mistake here
// you didn't include num while multiplying
//for(int i = 1; i < num; i++ )
for(int i = 1; i <= num; i++ )
result *= (float)i;
return result;
}
void bells(int n)
{
float sum = 1;
int i;
printf("Bell Number [0] = 1 ");
for(i = 1; i <= n; i++)
{
// we should add the value of nCk before we print sum
sum += factorial((float)n) / (factorial((float)i)*factorial((float)n - (float)i));
printf("Bell Number [%d] = %f ", i, sum);
}
}
int main()
{
int sum = 0;
int restart = 1;
//while is used to restart program on users request
while(restart == 1)
{
int userNum;
printf("Enter a number :");
scanf("%d",&userNum);
bells(userNum);
//Restarting program on users request
printf(" Restart program? (1 = yes : 0 = no) ");
scanf("%d", &restart);
}//end of restart while
}//end of main
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.