| | |
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 arrays binarysearch calculate centimeter char character cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hacking highest homework i/o inches incrementoperators intmain() iso km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. lowest match matrix microsoft mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pattern pdf performance posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprograming socketprogramming stack standard strchr string suggestions test unix urboc user variable voidmain() whythiscodecausesegmentationfault win32api windows.h windowsapi






