What's wrong with my code?

+1 Hithesh KK · December 25, 2014
/*This is to print factorial of an integer n*/

 #include<iostream.h>
 void main()
        {
        int i,fact,n;                                               

        cin>>n;

        fact = 1;
        for(i=2;i<=n;++i)
            {
            fact=fact*i;
            }
        cout<<fact;
        }


 /*It shows the following errors

Error NONAME01.CPP 8: Not an allowed type in function main()
Warning NONAME01.CPP 11: Possible use of 'n' before definition in function main()

*/

Post a Reply

Replies

Oldest  Newest  Rating
0 c student · December 25, 2014
#include <iostream>

using namespace std;

int main() {

    int i,fact,n;

    cin>>n;

    fact = 1;
    
    for(i=2;i<=n;++i) {
        fact=fact*i;
    }
    
    cout<<fact;
    
    return 0;
}


just wanted to say that the factorial of negative values is not 1.
0 ragmar thomas · December 25, 2014
in line 3 of your code there is an asterics(*).
0 Hithesh KK · December 26, 2014
Thanks ragmar. I checked the edit window so many times. It was far away from the words int but it is in the same line.

It was like this and i corrected it. Thanks


int i,n,fact;                                                                             *


Hope you saw the asterisk
  • 1

C++

128,017 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator