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 Stan Botha · December 25, 2014
line 1: should be #include<iostream> as apposed to #include <iostream.h>
        : include using namespace std; below line 1.
line 2: main is a reserved word in C++ for the main function. It should be of type int. It then follows that your program should
          have a return type. Insert return 0; before the last curly brace
line4: asterics in your code as mentioned by ragmar thomas

All these changes was made by c student . Just thought I throw in my 2 cents worth:angel:
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++

106,983 followers
About

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

Links
Moderators
Bucky Roberts Administrator