Thursday, August 25, 2011

Simple Numbers and Series in C – Digit Separation (Part III)



Here is a series of a few source codes on Series and Numbers we getting by separating the digits of a number

Input a number, count the numbers of digits present and find the sum of digits.

//Digit separation

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

  void main()
      {
           int n, c=0,s=0,m,r;
           clrscr();
           printf("Enter a Number : ");
           scanf("%d",&n);
           m=n;
           printf("\n Number  :  %d \n Digits   ",m);
           do
              {
                   r=n%10;  //getting the last digit
                   s=s+r;     //adding the digits
                   n=n/10;
                   c++;       //count
                   printf("  %d " ,r);
              }while(n>0);
           printf(" Sum of the digits = %d Total Nos. Digits %d ",s,c);
           getch();
      }

Armstrong number. Sum of cube of each digit equal to the number. e.g. 153 = 13 + 53 + 33.

//Armstrong Number

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

 void main()
  {
      int r,m,n,s=0;
      clrscr();
      printf("Enter a no : ");
      scanf("%d",&n);
      m=n;
      while(n>0)
           {
              r=n%10;
              n=n/10;
              s=s+pow(r,3);
           }
      if(s==m)
           printf("%d is a Armstrong No",m);
      else
           printf("%d is not a Armstrong No",m);
      getch();
  }

Palindrome : Number that reads the same forward and backward. e.g. 121.

//Palindrome

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

  void main()
      {
           int n, s=0,m,r;
           clrscr();
           printf("Enter a Number : ");
           scanf("%d",&n);
           m=n;
           do
              {
                   r=n%10;
                   s=s*10+r;
                   n=n/10;
              }while(n>0);
           if(s==m)
                   printf("%d  is Palindrome",m);
           else
                   printf("%d is not a Palindrome",m);
           getch();
      }

Automorphic Numbers : After removing the leftmost digit from the square of a number return the number itself. e.g. 25 = 625 or 6 = 36.

//Automorphic Number

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

  int DigiCount(int n)
      {
           int c=0;
           while(n>0)
              {
                   n/=10;
                   c++;
              }
           return(c);
      }

 void main()
  {
      int n,c,s;
      clrscr();
      printf("Enter a Number : ");
      scanf("%d",&n);
      c=DigiCount(n);
      s=(n*n)%(int)pow(10,c);
      if(s==n)
              printf("%d is a Automorphic Number",n);
      else
              printf(" %d is not a Automorphic Number",n);
      getch();
  }

Kraprekar No – First count the digits and then separate same numbers of digits from its square and then add it to the left most remainder. The Kaprekar numbers are named after D. R. Kaprekar. e.g. 2972 = square of it = 88209. separate 88 and 209 and add. 88 + 209 = 297.  452 = 2025 = 20 + 25= 45.

//Kaprecker Number

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

  long cal(long x)
      {
           long num,s,c=0;
           num=x*x;
           while(x>0)
              {
                   x=x/10;
                   c++;
              }
           s=num/(pow(10,c))+num%(int)pow(10,c);
           return s;
      }

  void main()
      {
           long n,s;
           clrscr();
           printf("Enter the number : ");
           scanf("^d",&n);
           s=cal(n);
           if(y==n)
                   printf("The given number is a Kuprecker number ");
           else
                   printf("The given number is not a Kuprecker number ");

           getch();
      }


Decimal to Binary : Convert decimal numbers to binary.

//Decimal to Binary Number

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

  void main()
      {
           int n,m,s=0,c=0;
           clrscr();
           printf("\nEnter a No : ");
           scanf("%d",&n);
           m=n;
           while(m>0)
              {
                   s=s*10+m%2;
                   m=m/2;
                   if(s==0)   //incase MSB is Zero
                        c++;
              }
           s=s*pow(10,c);
           printf("\nBinary Equivalent %d = %d ",n,s);
           getch();
      }

Binary to Decimal : Converting Binary numbers to decimal

//Binary to Decimal Number

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


void main()
      {
           long n,m,r,s=0,i=0;
           clrscr();
           printf("\nEnter a a Binary No : ");
           scanf("%ld",&n);
           m=n;
           while(m>0)
              {
                   r=m%10;
                   if(r>1 || r<0)
                        {
                            printf("\nInvalid input!! ");
                            exit(0);
                            getch();
                        }
                   s=s+r*pow(2,i);
                   i++;
                   m=m/10;
              }
           printf("\nDecimal Equivallent  %ld = %ld ",n,s);
           getch();
      }


Thursday, August 11, 2011

Simple Numbers and Series in C (Part II)




Here is a series of a few source codes on Series and Numbers. The series are nothing only logic and the relations between the numbers all you have to find.

Fibonacci Series, a series of numbers in which each member is the sum of the two preceding numbers. Discovered by Italian mathematician Leonardo Fibonacci. Example - 1,1,2,3,5,8,...etc

//C - Fibonacci Series

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

  void fibo(int n)
      {
           int i,a=1,b=0,c;
           for(i=1;i<=n;i++)
              {
                   printf("%d ",c);
                   c=a+b;
                   a=b;
                   b=c;
              }
           }

  void main()
      {
           int n;
           clrscr();
           printf("\nEnter The Term : ");
           scanf("%d",&n);
           fibo(n);
           getch();
      }


Non- Fibonacci Series – The numbers between the


//C – Non-Fibonacci Series

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

  void Nfibo(int n)
      {
           int i,a=1,b=0,c,j;
           for(i=1;i<=n;i++)
              {
                   c=a+b;
                   for(j=b+11;i<c;j++)
                        {
                            printf("%d  ",j);
                        }
                   a=b;
                   b=c;
              }
           }

  void main()
      {
           int n;
           clrscr();
           printf("\nEnter The Term : ");
           scanf("%d",&n);
           Nfibo(n);
           getch();
      }


Series : 0 7 26 63 124 ...n : It is a simple logical series, just deduct 1 from the cube of each values of natural nos. from 1 to ‘n’.

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

  void main()
      {
           int i,n;
           printf("\nEnter the Terms : ");
           scanf("%d",&n);
           clrscr();
           for(i=1;i<=n;i++)
              {
                   printf("%d  ",(i*i*i)-1);
              }
           printf("\n\npress any key to Cont...");
           getch();
      }

Sum of the series 1/2 + 1/3 + 1/4 + … 1/n


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

  void main()
      {
           int i,n;
           float sum =0;
           clrscr();
           printf("\nEnter the Terms : ");
           scanf("%d",&n);
           for(i=1;i<=n;i++)
              {
                   sum=sum+1.0/i;
              }
           printf("Sum of the Series = %10.3f ",sum);
           printf("\n\nPress any key to Cont...");
           getch();
      }

Taylor Series Formula or Sine Series – To find the sine of a given angle.
x – x3/3! + x5/5! – x7/7! + x9/9! – x11/11!

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

oid sinx(float,float);
float pw(float, int);
  void main()
      {
           int ang;
           float sum,x;
           clrscr();
           printf("Enter the angle");
           scanf("%d",&ang);
           x=ang/180.0*3.1415;
           sum=x;
           sinx(sum,x);
      }

  void sinx(float sum, float x)
      {
           int i,t,j;
           float f=1;
           for(i=3;i<=9;i=i+2)
              {
                   f=f*i*(i-1)*-1;
                   sum=sum+pw(x,i)/f;
              }
           printf("\nSine of %d angle is %f by Taylor Series ",ang,sum);
      }

  float pw(float x,int i)
      {
           int j;
           float y=1;
           for(j=1;j<=i;j++)
              {
                   y=y*x;
              }
           return y;
      }

Even Series – To find Cosine of a given angle.
1 – x2/2! + x4/4! – x6/6! + x8/8! – x10/10!

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

 void main()
  {
        int ang,i;
        float rad,fact=1,sum;
        clrscr();
        printf("\nEnter an angle ");
        scanf("%d",&ang);
        rad=22/7.0*ang/180.0;
        sum=1;
        for(i=2;i<=12;i=i+2)
           {
              fact=fact*(i)*(i-1)*(-1);
              sum=sum+pow(rad,i)/fact;
           }
        printf("Cosine of %d angle is %10.6f %f ",ang,sum,cos(rad));
        getch();
  }

Smith No. – is a number whose sum of the digit equal to sum of the prime factor. 666 = sum of the digits = 18, and prime factors 2, 3, 3, 37 and
666 /2 =33

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

int prime(int);
int sep(int);

  void main()
      {
           clrscr();
           int n,num,s,sum=0,f,i;
           printf("\nEnter a No : ");
           scanf("%d",&n);
           num=n;
           s=sep(n);
           f=0;
           for(i=2;i<=n;)
              {
                   if(n%i==0)
                        {
                            n=n/i;
                            f=prime(i);
                            if(f==0)
                                {
                                      if(i<10)
                                                          sum=sum+i;
                                      else
                                                          sum=sum+sep(i);
                                 }
                            }
                        else
                            i++;
              }
              if(s==sum)
                        printf("\n %d is a Smith Number",n);
              else
                        printf("\n %d is not a Smith Number",n);
              getch();
      }

  int prime(int n)
      {
           int i;
           for(i=2;i<=n/2;i++)
              {
                   if(n%i==0)
                        return 1;
              }
           return 0;
      }

  int sep(int n)
      {
           int s=0;
           while(n&glt;0)
              {
                   s=s+n%10;
                   n=n/10;
              }
           return s;
      }

Powerful No. is a number which divide by all its prime factors as well as square of it.

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

  int isprime(int n)
      {
           int i;
           for(i=2;i<=n/2;i++)
              {
                   if(n%i==0)
                        return 0;
              }
           return 1;
      }

  void main()
      {
           int i,j,f;
           clrscr();
           for(i=1;i<=150;i++)
              {
                   f=1;
                   for(j=2;j<=i/2;j++)
                        {
                            if(i%j==0)
                                {
                                     f=0;
                                     if(isprime(j)==1)
                                         {
                                            if(i%(j*j)!=0)
                                                {
                                                     f=1;
                                                     break;
                                                }
                                }
                        }
                   }
              if(f==0)
                   printf("%d ",i);
           }
      getch();
  }