Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3.  
  4. void DisplayNaturalNumber(int lowerLimit, int upperLimit);
  5.  
  6. int main()
  7. {
  8. int limit;
  9.  
  10. printf("Print all natural numbers from 1 to : ");
  11. scanf("%d", &limit);
  12.  
  13. printf("All natural numbers from 1 to %d are: ", limit);
  14. DisplayNaturalNumber(1, limit);
  15.  
  16. return 0;
  17. }
  18.  
  19. void DisplayNaturalNumber(int lowerLimit, int upperLimit)
  20. {
  21. if(lowerLimit > upperLimit)
  22. return;
  23.  
  24. printf("%d, ", lowerLimit);
  25.  
  26. DisplayNaturalNumber(lowerLimit+1, upperLimit);
  27. }
  28.  
  29.  
  30.  
  31. 1. Function initially sets lowerLimit as 1.
  32. 2. Function checks if 1 is more than 5 which is false so it then adds 1 to the lowerLimit making it 2.
  33. 3. Function recursively checks if 2 is more than 5 which is false so it then adds 1 to the lowerLimit making it 3.
  34. 4. Function recursively checks if 3 is more than 5 which is false so it then adds 1 to the lowerLimit making it 4.
  35. 5. Function recursively checks if 4 is more than 5 which is false so it then adds 1 to the lowerLimit making it 5.
  36. 6. Function gets called and sent to the if loop where it looks for the value in lowerLimit.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement