Check The Given Number Is A Krishnamurthy Number Or Not



Source code :-

#include<stdio.h>
int main()
{
    int n,z,r,s,i,f;
    printf("Enter the number: ");
    scanf("%d",&n);
    s=0;
    z=n;
    while(z!=0)   
    {
        r=z%10;
        f=1;
        z=z/10;
        for(i=1;i<=r;i++)
        {
            f=f*i;
        }
        s=s+f;
    }
    if(n==s)
        printf("\nThe entered number is a krishnamurti number\n");
    else
        printf("\nThe entered number is not a krishnamurti number\n");
    return 1;
}

Input-Output :-

abhi@hp-15q-laptop:~$ gcc krishnamurti.c
abhi@hp-15q-laptop:~$ ./a.out
Enter the number: 145
The entered number is a krishnamurti number

abhi@hp-15q-laptop:~$ gcc krishnamurti.c
abhi@hp-15q-laptop:~$ ./a.out
Enter the number: 123
The entered number is not a krishnamurti number

Print All The Krishnamurthy Number Of The Given Range


Source code :

#include<stdio.h>
int main()
{
    int n,z,r,s,a,b,i,f;
    printf("Enter the lower limit of the range: ");
    scanf("%d",&a);
    printf("\nEnter the upper limit of the range: ");
    scanf("%d",&b);
    printf("\nKrishnamurty numbers are: \n\n");
    for(n=a;n<=b;n++)
    {
        s=0;
        z=n;
        while(z!=0)   
        {
            r=z%10;
            f=1;
            z=z/10;
            for(i=1;i<=r;i++)
            {
                  f=f*i;
            }
               s=s+f;
        }
        if(n==s)
        {
            printf("%d ",n);
        }
    }
    return 1;
}

Input-Output :

abhi@hp-15q-laptop:~$ gcc krishnamurti.c
abhi@hp-15q-laptop:~$ ./a.out
Enter the upper limit of the range: 1
Enter the lower limit of the range: 300
Krishnamurty numbers are:
1 2 145

Post a Comment

أحدث أقدم