| | |
powers of two, recursion.
![]() |
•
•
Join Date: Oct 2004
Posts: 16
Reputation:
Solved Threads: 0
C Syntax (Toggle Plain Text)
int ans, guess, random, type, counter, num, i; char option[20], numb[10], input[5]; int power(int num) { ans=2; if(num==0) { return(1); } if(num<0) { printf("incorrect input, please input a positive integer:"); return(0); } else for(i=1; i!=num; i++) { return(ans=ans*2); } return(0); } int main() { /*before this there is all the switch case stuff*/ case 1 : /*powers of 2 calculator*/ printf("please enter a positive integer:\n"); fgets(numb, sizeof(numb), stdin); sscanf(numb, "%d",&num); printf("the answer is:%d\n",power(num)); break; ok so the origional was: case 1 : /*powers of 2 calculator*/ printf("please enter a positive integer:\n"); fgets(numb, sizeof(numb), stdin); sscanf(numb, "%d", &num); ans=2; if(num<0) printf("incorrect input, please input a positive integer:"); if(num==0) printf("the answer is 1\n"); for(i=1; i!=num; i++) ans=ans*2; printf("The answer is %d\n", ans); break;
and i had to take that and turn it into a recursive function. IDK what recursion is other than "it calls itself" or something... anyway im lost on this.
please help!
Last edited by alc6379; Nov 4th, 2004 at 11:57 pm.
•
•
Join Date: Oct 2004
Posts: 10
Reputation:
Solved Threads: 0
here is the recursive code for calculating power of 2;
int power(int num)
{
if(num==0)
return 1;
if(num==1)
return 2;
return 2*power(num-1);
}
well after including this function in ur prog, u dont need to check whether the input value is 0 or 1. but check for -ve entry..
here is ur modified switch statements ...
case 1 :
/*powers of 2 calculator*/
printf("please enter a positive integer:\n");
fgets(numb, sizeof(numb), stdin);
sscanf(numb, "%d", &num);
//ans=2;
if(num<0)
printf("incorrect input, please input a positive integer:");
else{
ans=power(num);
printf("The answer is %d\n", ans);}
break;
int power(int num)
{
if(num==0)
return 1;
if(num==1)
return 2;
return 2*power(num-1);
}
well after including this function in ur prog, u dont need to check whether the input value is 0 or 1. but check for -ve entry..
here is ur modified switch statements ...
case 1 :
/*powers of 2 calculator*/
printf("please enter a positive integer:\n");
fgets(numb, sizeof(numb), stdin);
sscanf(numb, "%d", &num);
//ans=2;
if(num<0)
printf("incorrect input, please input a positive integer:");
else{
ans=power(num);
printf("The answer is %d\n", ans);}
break;
![]() |
Similar Threads
- Recursion (C++)
- have you ever abused ur powers? (Geeks' Lounge)
- Monitor powers down (Windows 95 / 98 / Me)
- Recursion (C)
Other Threads in the C Forum
- Previous Thread: Input Buffer
- Next Thread: buffer
| Thread Tools | Search this Thread |
* adobe api array asterisks binarysearch calculate changingto char character cm copyanyfile copyimagefile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax database directory feet fgets file floatingpointvalidation forloop frequency function givemetehcodez global grade gtkgcurlcompiling gtkwinlinux hacking highest histogram homework i/o infiniteloop input interest intmain() iso kernel keyboard kilometer km linked linkedlist linux looping loopinsideloop. lowest meter microsoft mqqueue mysql number oddnumber odf open openwebfoundation owf pdf performance posix power probleminc process programming pyramidusingturboccodes radix read recv recvblocked repetition research reversing scheduling segmentationfault send sequential single socket socketprogramming stack standard string suggestions systemcall threads turboc unix urboc user variable wab whythiscodecausesegmentationfault win32api windows.h windowsapi





