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.

Recommended Answers

All 2 Replies

Good start! C is different in a few ways though. Declarations need to be the first thing in a block unless you are compiling under C99, so your C code will not compile with most compilers.

printf("N %2.2f, factorial %2.2f \n" ,i,factorial(i));

You need to be very careful with printf() and scanf() format strings because mixing up types can give weird results. In this one, the first modifier should be %d, not %2.2f, because i is an int.

char wait;
printf("Enter to exit");
scanf(&wait);

scanf() is just like printf() in that it requires a format string. Your code will compile because &wait is the right type, but anything could happen because &wait does not point to a string. The whole thing should look like this:

/* unnamed block so wait can be declared */
{
    char wait;

    printf("Enter to exit");

    /* flush the stream if you do not print '\n' */
    fflush(stdout);
    scanf("%c", &wait);
}

But in both your C and C++ code, that whole wait thing is more verbose than it needs to be. In C++ you can do cin.get(); and in C you can do getchar(); for the same effect as a char variable and operator>> or scanf().

Using the same style, I would write the code like this:

#include <stdio.h>

double factorial(int n);

int main()
{
    int i;

    printf("N          Factorial\n");
    printf("--------------------\n");

    for(i = 15; i > 0; --i)
    {
        printf("%-11d%.2f\n", i, factorial(i));
    }

    printf("Enter to exit");
    fflush(stdout);
    getchar();

    return 0;
}

double factorial(int n)      
{                                       
    double result = 1.0;
    int i;

    for(i = 2; i <= n; ++i)
    {
        result *= i;
    }

    return result;
}
commented: Thanks for the help +1

Thanks alot, that worked exactly as I wanted it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.