Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- void DisplayNaturalNumber(int lowerLimit, int upperLimit);
- int main()
- {
- int limit;
- printf("Print all natural numbers from 1 to : ");
- scanf("%d", &limit);
- printf("All natural numbers from 1 to %d are: ", limit);
- DisplayNaturalNumber(1, limit);
- return 0;
- }
- void DisplayNaturalNumber(int lowerLimit, int upperLimit)
- {
- if(lowerLimit > upperLimit)
- return;
- printf("%d, ", lowerLimit);
- DisplayNaturalNumber(lowerLimit+1, upperLimit);
- }
- 1. Function initially sets lowerLimit as 1.
- 2. Function checks if 1 is more than 5 which is false so it then adds 1 to the lowerLimit making it 2.
- 3. Function recursively checks if 2 is more than 5 which is false so it then adds 1 to the lowerLimit making it 3.
- 4. Function recursively checks if 3 is more than 5 which is false so it then adds 1 to the lowerLimit making it 4.
- 5. Function recursively checks if 4 is more than 5 which is false so it then adds 1 to the lowerLimit making it 5.
- 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