C++/help plz

Advertisement


Question
hi
can you help me about this program,it doesn't workwhen n is mor than 30
what should i do for solving this problem?

#include<stdio.h>
int hanoi(int n);
void main(){
  int n;
  printf("Enter your number:\n");
  scanf("%d",&n);
  hanoi(n);
  printf("%d\n",hanoi(n));
}
int hanoi(int n){
  if(n!=1)
     return (1+2*hanoi(n-1));
  return 1;
}



thanx
bita

Answer
One workaround is to use __int64.  As you have it, the int is overflowing at 30 (or 31 maybe).  I'm not sure how to scanf or printf __int64's, but if you scanf to an int and assign that to an __int64 that gets you an input.  I was able to use 60 but didn't printf it.

Bill

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Bill A

Expertise

I can answer questions about C++, programming algorithms, Windows programming in MFC (which is C++). I cannot answer questions about STL (templates) and I have no experience with Linux. I do enjoy reviewing code and critiquing it or finding problems in it. I will also gladly show better algorithms or methods if you want to take advantage of that.

Experience

I've developed a commercial embedded C compiler/assembler and IDE with debugger toolset, of which the IDE and debugger are written in C++. I work in the industry writing high tech embedded programs and Windows programs to communicate with the embedded devices.

Publications
Book: Embedded Systems Design using the Rabbit 3000 Microprocessor Authored Chapter 10 in its entirety.

Education/Credentials
BS Computer Engineering

©2016 About.com. All rights reserved.