generating basic sequence number

i want to generate a number with n digits and all digits being same
eg the number can be 22 88888 44444 3333

but this block of code

#include <iostream>
#include<math.h>
using namespace std;

int main()
{
long long a=0,j=0,e=3;
int c=6;

for(int i=j;i<c;i++){

a=a+pow(10,i-j)*e;

cout<<a<<endl;

}
return 0;
}



produces output

333331

instead of 333333

Can anyone point out the silly mistake i am doing ?


PS: I Know the above code can be simplified but my usage requires it as it is , so please help me in this block only
Watch your 0th power. (Eliminate it.)
Hold on, the code he has seems to work perfectly for me (unchanged).
OP, recompile it and test again.

10 to the 0 = 1 * 3 = 3, 0+3 = 3, a = 3 on that iteration.

and requirements that force you to use this are terrible. at the very least getting rid of the pow() would be good. And a little creativity would make it O(1) ish. I mean, 111 * 3 is 333. 111*9 is 999. The function could be reduced to return X*Y (or at worst, x*lookup[y]).
Last edited on
Oops, I mis-read it. I should have paid attention. Sorry. :-\
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
    long long a=0,j=0,e=3;
    int c=6;

    for(int i=j;i<c;i++){

        a=a+pow(10,i-j)*e;

       cout<<a<<endl;

    }
    return 0;
}


The code works properly. No way to reproduce '333331 '.
Last edited on
thankyou everyone , that was a compiler issue
Topic archived. No new replies allowed.