How to write a C Program Give me an integer and I will sum it with the previous natural numbers in C Programming Language ?
This C Program Change an Integer Number with Addition of Previous Natural Numbers.Solution:
- #include <stdio.h>
- #include <stdlib.h>
- int geometric_sum (int a);
- int main ()
- {
- int a = 0, sum = 0;
- printf("Give me an integer and I will sum it with the previous natural numbers : ");
- scanf("%d", &a);
- sum = geometric_sum(a);
- printf("Result: %d", sum);
- }
- int geometric_sum (int a)
- {
- if (a == 0)
- {
- printf("Base case!\n");
- return a;
- }
- else
- {
- printf("1: sum (%d)\n", a);
- return a + geometric_sum(a-1);
- }
- }