Advertisement
Guest User

Untitled

a guest
Nov 24th, 2020
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.43 KB | None | 0 0
  1. #include<stdio.h>
  2. int sumRecursive(int n)
  3. {
  4. if(n!= 0)
  5. return n+sumRecursive(n - 1);
  6. return 0;
  7. }
  8. int sumIterative(int n)
  9. {
  10. int i,sum=0;
  11. for(i=0;i<=n;i++)
  12. sum+=i;
  13. return sum;
  14. }
  15. int main()
  16. {
  17. int n;
  18. printf("Enter an integer between 1 and 1000: ");
  19. scanf("%d",&n);
  20. if(n>0 && n<=1000)
  21. {
  22. printf("Recursive sum = %d\n",sumRecursive(n));
  23. printf("Iterative sum = %d\n",sumIterative(n));
  24. }
  25. else
  26. printf("Invalid input\n");
  27. }
  28.  
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement