#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Lightbulb Number generation


    Hi,

    I recently started learning C language. I am thinking of a logic to build such a big number like.
    0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 1
    Basically requirement is like, the number should be as long as the number i give as input... Example: if i say a=101,b=101 so the number will have 101 digits and the last number will be 1.
    And if i say a=2,b=1 the number of digits will be Two(10) and first digit will be 1.

    Thanks,
    Raj
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,023
    Rep Power
    1285
    You can store the digits of the number in a dynamic array.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by bullet
    You can store the digits of the number in a dynamic array.
    ________________________________________

    I wrote this code which worked fine. But, i guess this is not at all a best practice to loop a string for such a long time.

    char a[251]=" ";
    int i,input1,input2,count;
    count = 100;

    input1= count;
    input2 = rand()% count;
    if(input2==0&& input1!=0){input2=1;}
    strcpy(a, " ");
    for(i=1;i<=input1;i++)
    {if(i==input2)
    {strcat(a,"1");}
    else{strcat(a, "0");}
    }

    Can anyone please improve my code to eliminate loops.

IMN logo majestic logo threadwatch logo seochat tools logo