[ create a new paste ] login | about

Link: http://codepad.org/7XIhT2zi    [ raw code | output | fork ]

C, pasted on Oct 16:
//http://en.wikipedia.org/wiki/D._R._Kaprekar
/*Kaprekar Numbers
Consider an n-digit number k. Square it and add the right n digits to the left n or n-1 digits.
If the resultant sum is k, then k is called a Kaprekar number.
For example, 9 is a Kaprekar number since 92 = 81 and 8 + 1 = 9
and 297 is a Kaprekar number since 2972 = 88209 and 88 + 209 = 297.
*/
//task is to write a function that identifies Kaprekar numbers
//and to determine the Kaprekar numbers less than a thousand.
#include <stdio.h>
#include<math.h>
 int nodigits( int p);
void kapre(long int num,long int d_num,long int sqr);

int main()
{
long int sqn,nD;
int  i;
clrscr();
for(i=1;i<1000;i++)
{
	nD=nodigits(i);
	sqn=pow(i,2);
	kapre(i,nD,sqn);
}
getch();
return 0;
}

  int nodigits(int p)
{
int j=0;
while(p!=0)
{	p=p/10;
	j++;
}
return j;
}

void kapre( long int num, long int d_num, long int sqr)
{
 long int sum1=0,revsum=0;
 int i;
for(i=0;i<d_num;i++)
{sum1=(sum1*10)+ sqr%10;
sqr/=10;
}
while(sum1!=0)
{
revsum=(revsum*10)+(sum1%10);
sum1/=10;
}
	if((revsum+sqr)==num)
	{
	printf("\n  %ld is A KARAMKAR NUMBER",num);
	}
}


Output:
1
2
3
In function `main':
undefined reference to `clrscr'
undefined reference to `getch'


Create a new paste based on this one


Comments: