Program in C to get Kaprekar’s constant

Write a C program to get kaprekar’s constant i.e 6174 as the ultimate answer of difference between ascending and descending arrangement of digits of a 4 digit number having at least two different digits.

What is Kaprekar’s constant?

When we take a 4-digit integer, generate the largest and smallest integers from its digits, and then subtract these two values, we get the Kaprekar’s constant, or 6174. We will always arrive at the number 6174 if we continue this procedure of creating and subtracting.

How can we get Kaprekar’s constant?

  • First of all we need to take a 4 digit number as input. For example: let’s take number 7803.
  • Now, we have to arrange it in ascending and descending order: Ascending order(a): 0378, Descending order(d): 8730
  • Then take difference of a and d: diff= d-a = 8730-0378 = 8352.
  • Here, diff is not equal to 6174, so we have to repeat the same process by taking the diff as input number.
  • input number=8352
  • a=2358, d=8532
  • diff= d-a = 8532-2358 = 6174
  • Finally we get Kaprekar’s constant: 6174.

C Programming Codes to find Kaprekar’s Constant

#include <stdio.h>
int main()
{
int a,d,i,ds[6],as[6],s[6],j,m;
printf("Enter a 4 digit number: ");
re:
scanf("%d",&a);
d=a/1000;
if(d==0 || d>=10) //check if the input number is a 4 digit number
{
printf("\nInvalid input, please renter a 4 digit number");
goto re;
}
else
  {
        int count=1;
       while(a!=6174)// to repeat the process until we get Kaprekar's constant
           {
                    m=a;
                    for(i=0;m!=0;i++)//separating the digits
                       {
                        s[i]=m%10;
                       m=m/10;
                       }
                  for(i=0;i!=4;i++)//arranging the digits in descending order
                   {
                 for(j=i+1;j!=4;j++)
                     {
                    if(s[i]<s[j])
                        {
                        m=s[i];
                        s[i]=s[j];
                        s[j]=m;
                        }
                     }
                 }
                for(i=0;i!=4;i++)//arranging the digits in ascending order
                  {
                     if(i==0){as[i]=s[3];}
                    if(i==1){as[i]=s[2];}
                    if(i==2){as[i]=s[1];}
                   if(i==3){as[i]=s[0];}
                 }
            int asn=0,dsn=0;
           for(i=0;i!=4;i++)
          {
             asn=asn10+as[i]; //ascending order number
            dsn=dsn10+s[i];//descending order number
           }
             a=dsn-asn;/difference of ascending and descending number
             printf("NO. %d iteration answer is %d\n ",count,a);
            count++;
     }
  }
}

Comments

2 responses to “Program in C to get Kaprekar’s constant”

  1. This code doesnt work!!! please fix it

  2. But you didnt assigned asn10 and dsn as int asn10, dsn10. it will just show error
    asn=asn10+as[i]; //ascending order number
    dsn=dsn10+s[i];//descending order number

Leave a Reply

Your email address will not be published. Required fields are marked *