Hello friends. I'm very new to C and am still learning how to do basic stuff. I got the code to work in C++ (which I'm much more fluent in) but I'm having trouble converting it to C code.
#include <iostream>
using namespace std;
double factorial( int n);
int main()
{
cout<<"N Factorial"<<endl;
cout<<"--------------------"<<endl;
for(int i = 15; i>0; --i)
{
cout<<i<<" "<<factorial(i)<<endl;
}
char wait;
cout<<"enter";
cin>>wait;
return 0;
}
double factorial( int n)
{
double result = 1.0;
for( int i = 2; i <= n; ++i)
result *= i;
return result;
}
This is what I have for C:
#include <stdio.h>
#include <time.h>
#include <math.h>
double factorial (int num);
int main()
{
printf("N Factorial");
printf("\n");
printf("--------------------");
printf("\n");
int i;
for(i = 15; i>0; --i)
{
printf("N %2.2f, factorial %2.2f \n" ,i,factorial(i));
printf("\n");
}
char wait;
printf("Enter to exit");
scanf(&wait);
return(0);
}
double factorial( int n)
{
double result = 1.0;
int i;
for( i = 2; i <= n; ++i)
result *= i;
return result;
}
This C code is not working properly. Can someone give me some ideas on how to resolve this. The problem is not with the factorial but more about style and format. I want the C program to have the same style as the C++ program.
Thanks for the help.