Friday, 10 July 2020

Krishnamurti Number Program | C Language

Krishnamurti Number


Q : Write a program to print all the Krishnamurti number from 1 to n. Here, n is user dependent. A             Krishnamurti number is a number whose sum of factorial of individual digits equals the number.         For example, 145 = 1! + 4! + 5! = 1 + 24+ 120 = 145.

Answer :


#include<stdio.h>
#include<conio.h>
/*============================
     Girfa Student Help
     Krishnamurti number
==============================*/
int fact(int);
void main()
{

     int i,n,j,sum;
     clrscr();
     printf("Enter range for krishnamurti number>> ");
     scanf("%d",&n);
     for(i=1;i<=n;i++)
     {
           j=i;
           sum=0;
           while(j>0)
           {
                sum=sum+fact(j%10);
                j=j/10;
           }
           if(sum==i)
                printf("\t%d",i);
     }
     getch();
}
int fact(int n)
{
     if(n==0)
           return 1;
     else
           return n*fact(n-1);
}

No comments:

Post a Comment