I have a simple program which gives the wrong output, the expected output is the digits of the number.
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
using namespace std;
int main()
{
int n = 125521;
int d = floor(log10(n));
printf("%d Digits\n",d+1);
int t =0;
while(floor(log10(n))-t)
{ printf("%d-----%d\n",(n/(int)pow(10,floor(log10(n))-t)%10),t); t++;}
return 0;
}
This gives the output
6 Digits
1-----0
2-----1
5-----2
7-----3
2-----4
Strange output. Why does 7 come ?
I know how to get the digits by other ways but I want this solution to work.
Now as suggested in the answers I get rid of the bug in the while loop (>=0) and I get the output:
Aucun commentaire:
Enregistrer un commentaire