# Thread: a program recursive print from 1 to 2^n

1. ## a program recursive print from 1 to 2^n

i will write a program with function that recursively print from 1 to 2^n
for example i give number 3 to pragram and it will be print 1 2 4 8
sorry for my bad english, plz help me on this program  2. Well....
Code:
```void foo ( int depth ) {
if ( depth >= 0 ) {
foo(depth-1);
}
}``` 3. but this not works  4. That's because it's an "idea", not an "answer" you can just run back to teacher with, without thinking about it. 5. i know the way of a recursive, but i will know the algorithm of this program, i thinked very much, but i can't 6. Well, show us what you've tried. Post your code in code tags (read this link), and we'll help you fix whatever problems you're having. 7. > for example i give number 3 to pragram and it will be print 1 2 4 8
So figure out how to do it using just a simple for loop, then you'll have your answer. 8. i dont write any code but i thinked about it really 9. Well, then just tell your teacher you thought about it and thought up a correct answer. I'm sure s/he will give you a good grade for "thinking about it". You have nothing to lose by trying. It's not like making a mistake will cause your computer to blow up, taking your hands and arms with it. Besides, you might even learn something along the way. Regardless, we sure wont do it for you, so you try something, and post your attempt, and we will help you. 10. Just in case you come back for help, here's an additional hint. Take Salem's code and add one print statement to it; that's all you need to do. Also, look up the left shift operation.
Bitwise operation - Wikipedia, the free encyclopedia 11. I you aren't even going to go as far as making an attempt and posting the code then you obviously aren't cut out to be a programmer.
As a programmer you need to be able to push the boundaries, step outside of the box, and try things you haven't tried before. Otherwise you'll never get past "Hello World". 12. i writed this program, this program good works until a[k] but a[i] don't print correctly, plz help on this.

Code:
```#include<conio.h>
#include<stdio.h>

void baz(int,int,int);
void main(){
int n;
printf("enter n : ");
scanf("%d",&n);
printf("\n");
int k=n-1,l=n;
baz(n,k,l);

getch();
}

void baz(int n,int k,int l){
int i,s=1,a;
if (n>0){
for(i=0;i<n;i++){
s=s*2;}
a[k]=s;
printf("%d ",a[k]);
baz(n-1,k-1,l);
}
else{
a[k]=1;
for(i=0;i<l;i++)
printf("%d ",a[i]);}
}``` 13. i changed to this shape, see that, a[k]'s is true but a[i] is wrong. why? Code:
```void baz(int n,int k,int l){
int i,s=1,a;
if (n>0){
for(i=0;i<n;i++){
s=s*2;}
a[k]=s;
printf("  %d  ",k);
printf("%d ",a[k]);
baz(n-1,k-1,l);
}
else{
printf("%d",k);
a[k]=1;
for(i=0;i<=l;i++){
printf("%d  ",i);
printf("%d ",a[i]);}
}
}``` 14. I correct it on my own, the truth code is this:

Code:
```void baz(int n,int k,int l,int* a){
int i,s=1;
if (n>0){
for(i=0;i<n;i++){
s=s*2;}
a[k]=s;
baz(n-1,k-1,l,a);
}
else{
a[k]=1;
for(i=0;i<=l;i++)
printf("%d ",a[i]);
}
}``` 15. but maximum number is 14 when you enter 15 it will be negative 32768, i changed a type to unsigned int but the maximum number is 14 again, how can I up this range? Popular pages Recent additions 