0

Alright, so, I'm doing some homework right now for a course in C and in this exercise I need to do an application of Kaprekar's Constant(that thing where you always end up with the number 6174 after a few iterations of the same algorithm).

The input is a number n between 1 and 10000(excluding 10000). In case n has less than 4 digits, it must be augmented with 0's until it gets 4 digits. Two variables(which I've called asc and des, for obvious reasons) must come out of this number: des in the reordering of the number with digits in descending order, and asc is the reordering of the number with digits in ascending order(For asc, leading zeros must be included). Then n will take the value of des-asc, and the process will keep being repeated until the number 6174 is obtained. The program must then output the number of iterations it took, plus the calculation themselves, to reach 6174.

I can do everything else(as shown in the code I added below), besides the reorderings. I probably could do those as well, but the exercise is one step further complicated by not allowing us to use arrays. Does anyone have any ideas, please?

#include <stdio.h>
#include <stdlib.h>

int main() {
    int n, asc, des, k = 0;
    scanf("%d", &n);
    while (n != 6174) {
        des =    /*this is where I'm stuck */
        asc =     /* this is also where I'm stuck */

        n = des - asc;
        printf("%d, "-" %d "="  %d, des, asc, n);
        k++;
    }
    printf(%d, k);
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
BMC98
  • 11
  • 2
  • 1
    Possible duplicate of [Sorting digits of an integer](https://stackoverflow.com/questions/2130712/sorting-digits-of-an-integer) – klutt Aug 05 '18 at 08:59

2 Answers2

0

You have many mistakes in your code.

Change

#inlcude <stdio.h>  // wrong spelling

to

#include <stdio.h>

Change

printf("%d, "-" %d "="  %d, des, asc, n); // wrong format, too many "s

To

printf("%d  - %d =  %d\n", des, asc, n);

When to code something make sure that you do not make any syntax errors.

Modified code this will work :-

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n, asc, des, k = 0;
    scanf("%d", &n);
    while (n != 6174)
    {
        int arr[4] = {0, 0, 0, 0}, i = 0, j, temp;
        temp = n;
        // separating digits
        while (temp > 0)
        {
            arr[i] = temp % 10;
            temp = temp / 10;
            i++;
        }

        // sorting
        for (i = 0; i < 4; i++)
        {
            for (j = 0; j < 3; j++)
            {
                if (arr[j] > arr[j + 1])
                {
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }

        asc = arr[0] * 1000 + arr[1] * 100 + arr[2] * 10 + arr[3];
        des = arr[0] + arr[1] * 10 + arr[2] * 100 + arr[3] * 1000;

        n = des - asc;
        printf("%d  - %d =  %d\n", des, asc, n);
        k++;
    }
    printf("%d", k);
    return 0;
}

Output :-

1369
9631  - 1369 =  8262
8622  - 2268 =  6354
6543  - 3456 =  3087
8730  - 378 =  8352
8532  - 2358 =  6174
5
anoopknr
  • 3,177
  • 2
  • 23
  • 33
0

This is really a good exercise to help you progress.However it may be really complex or definitively impossible to add leading zero without the usage of arrays or strings which are also a kind of arrays.Maybe instead of real arrays you can use string like in this code(maybe your instructor will accept this -:) ):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SORT_ASC 0
#define SORT_DESC 1


static void purger(void);

static void clean (char *chaine);

static int getNumber(void);

static void insertionSort(char string[],int order);

int main ( int argc, char** argv )
{
    int again=1;
    printf("Welcome To an application of The Kaprekars's Constant!!!\n\n");
    while(again){


        int number=0,ascending,descending,k=0,length,u,v;
        char asc[5],desc[5]={'0','0','0','0'},complete[5]={'0','0','0','0'},tmp;

        while(number<1||number>9999)
        {
            printf("Please Enter a number between 1 and 10000? ");
            number=getNumber();
        }
       while(number!=6174)
       {
            char numberinstr[5];
            sprintf(numberinstr,"%d",number);
            length=strlen(numberinstr);
            if(length<4)
            {

               for(u=0;u<length;u++)
                {
                   tmp=numberinstr[u];
                   complete[u+(4-length)]=tmp;
                }
                strcpy(numberinstr,complete);
            }

            insertionSort(numberinstr,SORT_ASC);
            strcpy(asc,numberinstr);
            for(u=3,v=0;u>=0;u--,v++){
                desc[v]=asc[u];
            }
            sscanf(asc, "%d", &ascending);
            sscanf(desc, "%d", &descending);
            number=descending-ascending;
            printf("%s - %s = %d\n",desc,asc,number);
            k++;
        }

        printf("6174 reached after %d iterations\n Do you want to choose another number?\n Press:\n 1 for Yes \n 0 for No \n",k);
        again=getNumber();
        while(again!=0&&again!=1)
        {
            printf("Please Enter a valid option!!!\n");
            again=getNumber();
        }
    }
    return 0;
}

static void purger(void)
{
    int c;

    while ((c = getchar()) != '\n' && c != EOF)
    {}
}

static void clean (char *chaine)
{
    char *p = strchr(chaine, '\n');

    if (p)
    {
        *p = 0;
    }

    else
    {
        purger();
    }
}

static int getNumber(void){
    char chaine[100];
    int nombre;
    int ret=0;
    int i=0;
    while (ret != 1)  {
       if(i) printf("Veuillez Entrer uniquement des chiffres: ");
        fgets(chaine, sizeof chaine, stdin);
        clean(chaine);
        ret = sscanf(chaine, "%d", &nombre);
        i++;
    }
    return nombre;
}


static void insertionSort(char string[],int order){
    int x,c,i,n,m;
    char temp=0;
    char chaine[2];
    char chaineMoinsUn[2];
    if(order==0){
        for(x=0,c=4;x<c;x++)
        {
            for(i=x;i>0;i--)
            {
                chaine[0]=string[i];
                chaineMoinsUn[0]=string[i-1];
                sscanf(chaine, "%d", &n);
                sscanf(chaineMoinsUn, "%d", &m);
                if(n<m)
                {
                    temp=string[i];
                    string[i]=string[i-1];
                    string[i-1]=temp;

                }
                else
                {
                    break;
                }
            }
        }
    }
    else
    {
        for(x=0,c=4;x<c;x++)
        {
            for(i=x;i>0;i--){
                chaine[0]=string[i];
                chaineMoinsUn[0]=string[i-1];
                sscanf(chaine, "%d", &n);
                sscanf(chaineMoinsUn, "%d", &m);
                if(n>m)
                {
                    temp=string[i];
                    string[i]=string[i-1];
                    string[i-1]=temp;

                }
                else
                {
                    break;
                }
            }
        }

    }
}

This code do at least three things ,first run an infinite loop to allow more than one number input and give the choice to exit or not,it check the user input to make sure that user really entered a number ,it check that the input is less than 10000 and greater than 0,finally it produces sorting and make the iteration until 6174 is reached.Then the result is printed as expected with leading 000 and the number of iteration.

the output of the code above

After further thoughts you may also obtain the same result without usage of strings or arrays but the leading zero will only be added for the printing :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>



static void purger(void);
static void clean (char *chaine);
static int getNumber(void);
static int intlength(int number);
static int int_asc(int number,int length);
static int int_desc(int number,int length);


int main ( int argc, char** argv )
{
    int again=1;
    printf("Welcome To an application of The Kaprekars's Constant!!!\n\n");
    while(again){


        int number=0,asc,desc,k=0,length,u,breakbefore=0;


        while(number<1||number>9999)
        {
            printf("Please Enter a number between 1 and 10000? ");
            number=getNumber();
        }
       while(number!=6174)
       {
           length=intlength(number);
           u=4-length;
           asc=int_asc(number,length);
           desc=int_desc(number,length);
           if(asc==desc&&length==4){
               breakbefore=1;
                printf("We can never reach 6174 with a number formed of same digit!!!");
                break;
           }
           if(length<4){
                while(u>0){
                    desc*=10;
                    u--;
                }
           }

            number=desc-asc;
            switch(intlength(asc)){
                case 3:
                    printf("%d - 0%d = %d\n",desc,asc,number);
                    break;
                case 2:
                    printf("%d - 00%d = %d\n",desc,asc,number);
                    break;
                case 1:
                    printf("%d - 000%d = %d\n",desc,asc,number);
                    break;
                default:
                    printf("%d - %d = %d\n",desc,asc,number);
                    break;
            }

            k++;
        }
        if(breakbefore==1){
            printf("\n Do you want to choose another number?\n Press:\n 1 for Yes \n 0 for No \n");
        }else{
            printf("6174 reached after %d iterations\n Do you want to choose another number?\n Press:\n 1 for Yes \n 0 for No \n",k);
        }
        again=getNumber();
        while(again!=0&&again!=1)
        {
            printf("Please Enter a valid option!!!\n");
            again=getNumber();
        }
    }

}

static void purger(void)
{
    int c;

    while ((c = getchar()) != '\n' && c != EOF)
    {}
}

static void clean (char *chaine)
{
    char *p = strchr(chaine, '\n');

    if (p)
    {
        *p = 0;
    }

    else
    {
        purger();
    }
}

static int getNumber(void)
{
    char chaine[100];
    int nombre;
    int ret=0;
    int i=0;
    while (ret != 1)
    {
       if(i) printf("Veuillez Entrer uniquement des chiffres: ");
        fgets(chaine, sizeof chaine, stdin);
        clean(chaine);
        ret = sscanf(chaine, "%d", &nombre);
        i++;
    }
    return nombre;
}




static int intlength(int number)
{
    int i,j=0,temp;
    for(i=0;i<=9;i++){
        for (temp=number;temp>0;temp/=10)
        {
            if(temp%10==i)
            {
                    j++;
            }
        }
    }

    return j;
}


static int int_asc(int number,int length)
{
    if(length==1)
    {
        return number;
    }
    int i,j=0,temp,asc=0,u,power;

    j=length-1;


    while(j>0)
    {
        for(i=0;i<=9;i++)
        {
            for (temp=number;temp>0;temp/=10)
            {
                if(temp%10==i)
                {
                   for(u=j,power=1;u>0;u--)
                   {
                        power*=10;
                   }
                    asc += i*power;
                    j--;
                }
           }
        }

    }
    return asc;
}

static int int_desc(int number,int length)
{
      if(length==1)
      {
        return number;
      }
    int i,j=0,temp,desc=0,u,power;
    j=length-1;


    while(j>0)
    {
        for(i=9;i>=0;i--)
        {
            for (temp=number;temp>0;temp/=10)
            {
                if(temp%10==i)
                {
                    for(u=j,power=1;u>0;u--)
                    {
                        power*=10;
                    }
                    desc += i*power;
                    j--;
                }
           }
        }

    }
    return desc;
}
Elementary
  • 1,443
  • 1
  • 7
  • 17
  • @B.M.Chelu did you find a solution to your problem ? if yes can you share your solution with us ...? Or do you think my answer will do it? – Elementary Aug 06 '18 at 15:03