StudyLover
  • Home
  • Study Zone
  • Profiles
  • Typing Tutor
  • Contact us
  • Sign in
StudyLover Program #34 (U3): Recursion — Sum from 1 to N
Download
  1. C Programming
  2. C Programming Practice
Program #33 (U3): Recursion — Factorial and Fibonacci : Program #35 (U3): Recursion — Reverse a String
C Programming Practice

Task: Read N and compute 1 + 2 + … + N recursively with a clear base case. Print the sum.

 
#include <stdio.h>

 
long long sum_rec(long long n){ return (n<=0)?0:n+sum_rec(n-1); }

 
int main(void){

    long long n;

    printf("Enter N (>=0): ");

    if(scanf("%lld", &n)!=1 || n<0){ printf("Invalid N.\n"); return 0; }

    printf("Sum = %lld\n", sum_rec(n));

    return 0;

}

 

 

Program #33 (U3): Recursion — Factorial and Fibonacci Program #35 (U3): Recursion — Reverse a String
Our Products & Services
  • Home
Connect with us
  • Contact us
  • +91 82955 87844
  • Rk6yadav@gmail.com

StudyLover - About us

The Best knowledge for Best people.

Copyright © StudyLover
Powered by Odoo - Create a free website