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();
  }


No comments: