Thursday, September 6, 2012

Miscellaneous – IX – Number and Numbers.


A Cyclic Number is a number with n digits when multiple by 2 ... n (n = nos. of digits presents in the number itself) generated number will have same digits but in different place.

Example : N = 142857 (No. of digits are 6)
142857 * 2 = 285714
142857 * 3 = 428571
142857 * 4 = 571428
142857 * 5 = 714285
142857 * 6 = 857142



#include <stdio.h>
#include <conio.h>
#include <string.h>


// Ex. No. 142857

  void Sort(char S[])
      {
           int i,j;
           char c;
           for(i=0;i<strlen(S);i++)
              {
                   for(j=0;j<strlen(S)-i-1;j++)
                        {
                            if(S[j]>S[j+1])
                                {
                                      c=S[j];
                                      S[j]=S[j+1];
                                      S[j+1]=c;
                                }
                        }
              }
      }
  int Count(long n)
      {
           int c=0;
           while(n>0)
              {
                   c++;
                   n/=10;
              }
           return c;
      }

  void main()
      {
           long n,m;
           int i,f=0,c;
           char St[15],St1[15];
           clrscr();
           printf("\n\tEnter a No ");
           scanf("%ld",&n);
           c=Count(n);
           ltoa(n,St,10);
           Sort(St);

           for(i=2;i<=c;i++)
              {
                   m=n*i;
                   ltoa(m,St1,10);
                   Sort(St1);
                   if(strcmpi(St,St1)!=0)
                        {
                            f=1;
                            break;
                        }
              }

           if(f==0)
              {
                   printf("\n\tInput No. is = %ld  ",n);
                   for(i=2;i<=c;i++)
                        {
                            printf("\n\t\t  %ld  ",n*i);
                        }
                            printf("\n\t\t it is a Cyclic No ");
              }
              else
                   {
                        printf("\n\t%ld is not a Cyclic Number  ",n);
                   }
           getch();
      }





Narcissistic Number – Where sum of the digit raised to the power of C(where C is the number of digits presents in the number itself) equal to the number is call Narcissistic Number.
ex. N=371 (No. of digits are 3) – 33+73+13=371
N  = 9474 (Nos. of digits is 4) – 94 + 44 + 74 + 44 = 9474



#include <stdio.h>
#include <conio.h>
#include <string.h>

// Ex. No. 371     9474

  int Count(int n)
      {
           int c=0;
           while(n>0)
              {
                   c++;
                   n/=10;
              }
           return c;
      }
  int Sum(int n,int c)
      {
           long s=0;
           while(n>0)
              {
                   s=s+pow(n%10,c);
                   n/=10;
              }
           return s;
      }

  void main()
      {
           long s=0;
           int n,c;
           clrscr();
           printf("\n\tEnter a No ");
           scanf("%d",&n);
           printf("\n\tInputted : %d ",n);
           c=Count(n);
           s=Sum(n,c);
           if(s==n)
              {
                   printf("\n\t\t sum is %ld it is a Narcissistic No ",s);
              }
           else
              {
                   printf("\n\t sum is %ld is not a Narcissistic Number  ",s);
              }
           getch();
      }



Mysterious Number – 6174 is a mysterious no. Input a four number where all digits should not be same (1111 or 2222). Subtract the smallest combination from largest combination of the no to form new number. Repeat the same for 30 times and if the generated no equal to 6174 then it is a mysterious no else it is not.
Ex. N = 2005
     Largest           Smallest                           New No
     5200         -     0025                =              5175
     7551         -     1557                =              5994
     9954         -     4599                =              5355
     5553         -     3555                =              1998
     9981         -     1899                =              8082
     8820         -     0288                =              8532
     8532         -     2358                =              6174
     7641         -     1467                =              6174


#include <stdio.h>
#include <conio.h>
#include <string.h>

  void Asort(char S[])
      {
           int i,j;
           char c;
           for(i=0;i<strlen(S);i++)
              {
                   for(j=0;j<strlen(S)-i-1;j++)
                        {
                            if(S[j]>S[j+1])
                                {
                                      c=S[j];
                                      S[j]=S[j+1];
                                      S[j+1]=c;
                                }
                        }
              }
      }
  void Dsort(char S[])
      {
           int i,j;
           char c;
           for(i=0;i<strlen(S);i++)
              {
                   for(j=0;j<strlen(S)-i-1;j++)
                        {
                            if(S[j]<S[j+1])
                                {
                                      c=S[j];
                                      S[j]=S[j+1];
                                      S[j+1]=c;
                                }
                        }
              }
      }

  void main()
      {
           long n,max,min,S;
           int i,f=0;
           char St[15],St1[15];
           clrscr();
           printf("\n\tEnter a No ");
           scanf("%ld",&n);
           printf("\n\tThe %ld is ",n);
           for(i=1;i<=30;i++)
              {
                   ltoa(n,St,10);
                   ltoa(n,St1,10);
                   Asort(St1);
                   Dsort(St);
                   printf("\n\t%s  -  %s  = ",St,St1);
                   max=atol(St);
                   min=atol(St1);
                   printf(" %ld  ",max-min);
                   if(max-min==6174 && S == 6174)
                        {
                            f=1;
                            break;
                        }
                   S=max-min;
                   n=S;
              }
           if(f==1)
              {
                   printf(" \n\tis a Mysterious No as it is returned 6174");
              }
           else
              {
                   printf("\n\tnot a Mysterious No as it failed to return 6174");
              }
           getch();
      }

No comments: