Sunday, June 22, 2014

Sum of Natural Numbers (Recursion) - C Program

Problem Question


Write a recursive function to obtain the running sum of first 25 Natural Numbers.

Explanation of Problem


I have made a more general program that could handle most of the numbers in the integer limits. The user can obtain the desired output by inputting 25 to the program.

Code


#include <stdio.h>

/**@Title: recSumNat.c*
*@Language: ANSI C*
*@Compiler: GNU GCC*
*@IDE: Code::Blocks 13.12*
*@Author: Toxifier*
*@URL: http://letsplaycoding.blogspot.com/*
*@Date: 22-06-2014*
*/

int recSumNat(int a)
{
  if (a < 1)
    return a;
  else if (a == 1)
    return 1;
  else
    return (a + recSumNat(a-1));
}

int main()
{
  int number;
  printf("\n\nEnter a number: ");
  scanf("%d", &number);
  printf("\n\nSum of Natural numbers upto %d: %d\n\n", number, recSumNat(number));
  system("pause");
  return 0;
}


Explanation of Code


#include <stdio.h> -> This is the step which occurs before compilation starts. The compiler calls the C Preprocessor to include the STDIO(Standard Input Output) header file into the program, thus letting the use of the standard input/output functions like printf() and scanf() which come from STDIO.H

int main() -> The entry point of the program where the execution starts. This function has to be named main. As per the ANSI specification, the return type has to be int. If you use the traditional C, you may use void as the return type. Since the return type is specified as int in my program, I have to use a return statement at the end of my code. So I use return 0 since zero returned from a function, by convention, implies a correct execution of the program. The return values are used to debug the program.

printf() -> This is a standard output function used to print something on the screen. We have to pass a string to this function which will be displayed on user's terminal.

scanf() -> This is the scanf() function which waits for the user to enter certain value using his/her keyboard. We store the user input at the location in memory which is pointed to by the variable whose address is passed to this function.

if (a < 1) return a; -> If the user enters an integer which is not a Natural Number, the program would return the number itself, since the program is trying to find the sum of natural numbers upto the number entered by the user, that is, first n natural numbers.

else if (a == 1)
return 1;
-> As soon as the value of the 'a' argument becomes 1, the function returns 1.

else
return (a + recSumNat(a-1));
-> For any natural number other than 1, the function returns the sum of the current value of 'a' with the current call to the function, and the value returned by the function when called with 'a-1'. Thus, we are guaranteed to exit the recursive stack as soon as the value of 'a' reaches 1.

system("pause") -> This statement is used to pause the program, until user presses a key. This function is not necessary in your program, I use it to see my outputs paused. If you use cmd to run your programs, you might not need this. If you use linux/unix you might not need this. Depending on your compiler, this function may or may not work. Moreover, removing this line of code from this program, doesn't affect the functionality of the program.

Output(s)





Download Source Code


No comments:

Post a Comment

Need help?