| | |
Recursion
Thread Solved
![]() |
I was trying to do some simple exercises on recursive functions:
1. Write a function using Recursion to print numbers from n to 0.
My solution:
The next exercise was
2. Write a function using Recursion to print numbers from 0 to n. (You just need to shift one line in the program of problem 1.)
Well i dont know how to do it using recursion; at least not by shifting just one line of the previous code. And also did not want to use any other variables.
Is there any simple solution to the 2nd problem. Help me out?
1. Write a function using Recursion to print numbers from n to 0.
My solution:
C Syntax (Toggle Plain Text)
int integer(int n) { if(n>=0) { printf("\n%d",n); integer(--n); } }
The next exercise was
2. Write a function using Recursion to print numbers from 0 to n. (You just need to shift one line in the program of problem 1.)
Well i dont know how to do it using recursion; at least not by shifting just one line of the previous code. And also did not want to use any other variables.
Is there any simple solution to the 2nd problem. Help me out?
"He who mixes with people and endures the harm they do is better than he who does not mix and endures." (Tirmidhi)
#include <stdio.h> void foo(int n) { if ( n >= 0 ) { printf("%d ", n); foo(n - 1); } } void bar(int n) { if ( n >= 0 ) { bar(n - 1); printf("%d ", n); } } int main() { int value = 5; foo(value); putchar('\n'); bar(value); putchar('\n'); return 0; } /* my output 5 4 3 2 1 0 0 1 2 3 4 5 */
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
![]() |
Similar Threads
- Recursion: when do you use it? (C++)
- C++ Beginner - #include recursion problem (C++)
- Recursion (C++)
- number formatting using recursion (Java)
- Need help with recursion and arrays (C++)
- powers of two, recursion. (C)
- Create menu from properties file by recursion (Java)
Other Threads in the C Forum
- Previous Thread: how can i make the program depend on time
- Next Thread: Quick Polymorphism Tutorial
| Thread Tools | Search this Thread |
* adobe ansi api array asterisks binarysearch calculate centimeter changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory feet fflush fgets file fork forloop frequency function givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux hacking highest histogram i/o inches infiniteloop input intmain() iso kernel keyboard km linked linkedlist linux linuxsegmentationfault list locate looping loopinsideloop. lowest match microsoft mqqueue mysql number oddnumber odf open opendocumentformat owf pattern pdf performance posix probleminc process program programming radix recv recvblocked repetition research reversing scanf scheduling segmentationfault send sequential socket socketprograming stack standard string systemcall threads turboc unix user variable voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi






