Pages

Monday, 10 August 2015

Magic number

C program for Magic number
Coding:
#include<stdio.h>

int main()
{
    int n,s=0,r,m;
    printf("Please enter the number:");
    scanf("%d",&n);
    m=n;
    while(n>0)
    {
       s=0;
        while(n>0)
        {
            r=n%10;
            n=n/10;
            s=s+r;
        }
        n=s;
        if(n==1)
        {
            printf("%d is a magic number",m);
            break;
        }
        else if(n>1 && n<10)
        {
            printf("%d is not a magic no",m);
            break;
        }
    }
    return 0;
}


Output:
Please enter the number:406
406 is a magic number