1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Simple Math

Discussion in 'C' started by Amphibian, Aug 31, 2010.

  1. Amphibian

    Amphibian New Member

    Joined:
    Aug 31, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    This code worked perfectly at school in the lab but when I copied it to notepad and sent it to myself via email and pasted the code into Dec-C++ it no longer worked. When I run the program it should allow me to type a number such as four and give me an answer (in this case six). Instead of working and giving me the correct answer when i type in the number and press enter the command prompt window closes. I tried to fix it last night but I am not sure what could be wrong.


    Code:
    int sum(int n)
    {
    if(n==0)
        return(0);
        else 
        return(sum(n-2)+n);
    }
    main()
     
     {
              int N;
              scanf("%d",&N);
              printf("The sum is %d.\n", sum(N));
              }
     

Share This Page