Advertisement
dmilicev

task_01.c

Sep 14th, 2021
1,121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.82 KB | None | 0 0
  1. /*
  2.     task_01.c
  3.  
  4.     https://www.facebook.com/photo/?fbid=254402079891542&set=pcb.385071233009994
  5.  
  6. */
  7.  
  8. #include <stdio.h>
  9.  
  10. void func(int n){
  11.     int i;
  12.     static int last_result=0;
  13.  
  14.     for(i=0;i<=n;i++){
  15.         last_result = last_result + i;
  16.         printf("\n %2d \t %2d \n", i, last_result);
  17.     }
  18. }
  19.  
  20. void recursive_func(int i, int n){
  21.     static int last_result=0;
  22.  
  23.     if(i>n)
  24.         return;
  25.  
  26.     last_result = last_result + i;
  27.     printf("\n %2d \t %2d \n", i, last_result);
  28.     if(i<=n){
  29.         i++;
  30.         recursive_func(i,n);
  31.     }
  32. }
  33.  
  34. int main(){
  35.     int i, n=5, last_result=0;
  36.  
  37.     printf("\n Without function: \n");
  38.     for(i=0;i<=n;i++){
  39.         last_result = last_result + i;
  40.         printf("\n %2d \t %2d \n", i, last_result);
  41.     }
  42.  
  43.     printf("\n With function: \n");
  44.     func(5);
  45.  
  46.     printf("\n With recursive function: \n");
  47.     recursive_func(0,5);
  48.  
  49.     return 0;
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement