Function problem (recursion)

This is a discussion on Function problem (recursion) within the C Programming forums, part of the General Programming Boards category; I want to calculate the formula : 1+2+3+4+............+n = n (n+1)/2. anyone will help me to find the error of ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    73

    Function problem (recursion)

    I want to calculate the formula : 1+2+3+4+............+n = n (n+1)/2. anyone will help me to find the error of the program? it just show an error result ........





    Code:
    // Function (RECURSION)
    
           # include<stdio.h>
           
           int funct1(int n);
    
    	   void main()
    
    	   {
    		 int n,t;
    	     printf("Enter the value until which you want to sum up\n");
    		 scanf("%n", &n);
    
    		 t= funct1(n);
    
    		 printf("  summation= %d", t );
    	   
    	   }
    
    
    	   int funct1(int n)
    
    	   {
    	    
    		   if (n>0)
    
    			return (n+ funct1(n-1));
    
    	   }
    Last edited by chaklader; 05-27-2010 at 11:19 PM.

  2. #2
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    According to the scanf docs there's no such specifier as "%n". There are different ones to use depending on exactly what you need, the documentation describes them all.

    scanf - C++ Reference

    There's no 'bottom case' (can't remember what it's called) for your recursive function. You need to specify something to return for when it does get to zero.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Location
    INDIA
    Posts
    64
    Every thing is ok.other than the '%n' in the scanf .You have to use '%d' is it .

    Code:
      # include<stdio.h>
    
           int funct1(int n);
    
              int main()
               {
                     int n,t;
                     printf("Enter the value until which you want to sum up\n");
                     scanf("%d", &n);
    
                     t= funct1(n);
    
                     printf("  summation= %d", t );
    
               }
    
    
               int funct1(int n)
    
               {
    
                       if (n>0)
    
                            return (n+ funct1(n-1));
    
               }

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You forget base case.
    Code:
    int funct1(int n)
    {
      if(n > 0)
       return n + funct1(n-1);
     return 0;
    }
    Btw, do you enable compiler warning?
    Last edited by Bayint Naung; 05-27-2010 at 11:52 PM.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    73
    DeadPlanet, karthigayan and Bayint Naung @ its working now . thank you all .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tough problem with function pointers
    By Montejo in forum C Programming
    Replies: 10
    Last Post: 12-22-2009, 12:17 AM
  2. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 03:25 AM
  3. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  4. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  5. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 08:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21