I can't run this program with values >5

#include<iostream>
using namespace std;
int c,d;
int func(int a,int b){
if (b==0){
c=1;
}
else{
while(d<=b)
c=c*a;
a++;
}
return c;
}
main(){
int A;
int B;
int C=0;
int D=0;
cin >> A;
while (C<=0){
B=A%2;
C=C+B*func(10,D);
D++;
A=A/2;
}
cout << C;
}
I have a potato laptop actually.
cpu speed reaches upto 2.5 ghz.
th eprogram uses upto 10% of the cpu.
Please repost your question, state the result you expect and the problem. There is a icon to click under format, it looks like this <>, place your source code between those.
Last edited on
d is not initialized.
d is not modified.
func is therefore doing something totally random, which likely includes infinitely looping.
while(d<=b) <---- this line can't be right as coded. Either this line is wrong or you need more code.

that said, presentation of your question and code will get you a lot farther going forward. ask good questions... what does it do, what should it do, what do you know about what is broken, etc... and formatted code.
Last edited on
A value of 6 will cause you to enter your while loop, which leads to an infinite loop. I suggest using a debugger, and debugging your program line by line, examining what variables change each line.
I cannot overstate how helpful using a debugger would be in this scenario -- what compiler or IDE are you using?
1
2
while(d<=b) // how does this loop exit once entered?
    c=c*a;

To expand on what jonnin said, this makes no sense.

Jonnin, one minor nitpick: The "d" variable is global so it is 0-initialized. But I hate programs that rely on that, because it more than likely will cause a bug in the future.

Ayush, furthermore, you have a bunch of one-character variable names, with mixed capitalization. Please have meaningful variable names. Despite your program being pretty simple, it's unreadable because of this (that, and the lack of indentation).

Also, main must return int. Please update your compiler.
Last edited on
Topic archived. No new replies allowed.