Wednesday, October 21, 2015

Fibonacci Series

Fibonacci Series

The Fibonacci sequence is the series of numbers:
First, the terms are numbered from 0 onwards like this:
n =
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
...
xn =
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
...
  

 The next number is found by adding up the two numbers before it.

The Rule is :  xn = xn-1 + xn-2


The sequence works below zero also, like this:

n =
...
-6
-5
-4
-3
-2
-1
0
1
2
3
4
5
6
...
xn =
...
-8
5
-3
2
-1
1
0
1
1
2
3
5
8
...

(Prove to yourself that each number is found by adding up the two numbers before it!)

In fact the sequence below zero has the same numbers as the sequence above zero, except they follow a +-+- ... pattern. It can be written like this:

x−n = (−1)n+1 xn

Which says that term "-n" is equal to (−1)n+1 times term "n", and the value (−1)n+1 neatly makes the correct 1,-1,1,-1,... pattern.



History

Fibonacci was not the first to know about the sequence, it was known in India hundreds of years before.

Fibonacci’s real name was Leonardo Pisano Bogollo, and he lived between 1170 and 1250 in Italy.

"Fibonacci" was his nickname, which roughly means "Son of Bonacci".

As well as being famous for the Fibonacci Sequence, he helped spread Hindu-Arabic Numerals (like our present numbers 0,1,2,3,4,5,6,7,8,9) through Europe in place of Roman Numerals (I, II, III, IV, V, etc). That has saved us all a lot of trouble! Thank you Leonardo.


Fibonacci Day is November 23rd, as it has the digits "1, 1, 2, 3" which is part of the sequence. So next Nov 23 let everyone know!


Program in C:



//COPY AND PASTE IT :D


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

int main()
{
   int n, fab1=0,fab2=1,fab=0,i=0;

   printf("Enter the number of terms\n");
   scanf("%d",&n);

   printf("First %d terms of Fibonacci series are :-\n",n);

   for (i = 0 ; i <= n ; i++ )
   {
      printf("%d ",fab1);
   fab  = fab1+fab2;
   fab1 = fab2;
   fab2 = fab;
   }
      

    getch();
   return 0;

}





#include<stdio.h>
#include<conio.h>
int fab(int);

int main(char arg , int **p){
   int n, i =0,c;
   printf("\nEnter the number till you want fibonacci series\n");
   scanf("%d",n);

   for(c=0;c<=n;c++)
   {
      printf("%d",fab(i));
      i++;
   }

getch();
return 0;
}

int fab(int n){

   if(n ==0)
      return 0;
   else if (n ==1)
      return 1;
   else return fab(n-1) + fab(n-2);
}

Tuesday, October 20, 2015

Concept of the Pointer

Pointers in C programming


Theory:
Pointer – Pointers are variables that hold a memory location.
   
   Pointer is a variable that contain the address of another variable.Every Location has a number associated with it, it is called as an address. 

The Symbol '&' Ampersand is used to represent the address.

The Symbol '*' is used to represent a pointer. It is called the dereferencing or indirect operator.

int *p;  here p is a pointer variable. Its size is 4 byte and it accept the address of integer variable.

For float , char etc. we can type.char *ch;float *ft;double *db;Each one of them will get same size of the memory i.e. 4 bytes.  

Harold Lawson is credited with the invention of the pointer, PL/I the first Procedure Oriented Language(POL) supported pointer in 1964.


Program snapshot:


Declaration of a Pointer : 

     Declaration of a pointer is nothing but assigning a memory location. And Letting our compiler known about the variable, what is its data type , its value , and address of memory location.








Pointer with printf() Statement
=================================

printf(“\n%u\n”,*p);
The above statement will print the value at the address stored in P pointer.

As we know P is a pointer and according to the definition, it contain the address of a variable.

Working of printf(); :

printf() statement require
1) Format Specifier e.g. %d , %u, %f etc.
2) Data to print. E.g. “Hello World! ”, 10 , -123 etc.

If we write

printf(“\n%d\n”,P);

This is a  valid statement, but the content of P will be echoed on the console , and content of P is the address of variable n.

*P -> will give the Value stored at the address in the pointer.

SNAPSHOT



Pointer with scanf() Statement
================================

scanf() statement is used to accept the input from the user via i/o devices.

scanf() function require:
1) Format Specifier.
2) Address of the memory where value to be stored.

FORMAT
scanf(“%d”,p); -> Here p will provide the content. i.e the address of n.
so we can directly type P in scanf() function.
Lets see other options.
1) *p
2) &p
3) P
1) *p : It will give the value at the address it content. i.e 10
So the statement will be

scanf(“%d”,10); | as p -> address of n , and n content 10;
So when the scanf() function encounter it will hang.


2) &p : It will give the address of variable p.
As P is declared as pointer, so we can’t store any value in p except the address of other location.
So this will give error.

3) P : p will give the its content, which is nothing but the address of the other local variable i.e. n.
So this is valid statement.

scanf(“%d”,1654200); This will store the user entered value at the address 1654200 , i.e. in n.



Friday, October 16, 2015

Armstrong Number in C Programming.

Theory

Armstrong number: An n-digit number equal to the sum of the nth powers of its digits.


1. Raise each digit to a power equal to the number of digits in the number. 

     For instance, each digit of a four‑digit number would be raised to the fourth power; each digit of a five‑digit number would be raised to the fifth power; and so on.

2. Add the results.
3. If sum is equal to the number then it’s an Armstrong number.

Demonstrate this process for 1634—

         Because 1634 is a four-digit number, raise each digit to the fourth power, and add
14 + 64 + 34 + 44 = 1 + 1296 + 81 + 64 = 1634.
Hence 1634 is an Armstrong Number.

PROGRAM FOR ARMSTRONG NUMBER.

1.) Program for only 3 digit number

#include<stdio.h>

int main(){
    int num,r,sum=0,temp;

    printf("Enter a number: ");
    scanf("%d",&num);

    temp=num;
    while(num!=0){
         r=num%10;
         num=num/10;
         sum=sum+(r*r*r);
    }
    if(sum==temp)
         printf("%d is an Armstrong number",temp);
    else
         printf("%d is not an Armstrong number",temp);

    return 0;
}

Sample output:
Enter a number: 153
153 is an Armstrong number

2.) Program for any number of digits.

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

int main(){
    int num,r,t=0,sum=0,temp,count=0;

    printf("Enter a number: ");
    scanf("%d",&num);

    temp=num;
 while(temp>0){
  count++;
  temp = temp/10;
 }

 temp = num;

    while(num!=0){
         r=num%10;
         num=num/10;
    t=(int)pow((double) r, (double) count);
         sum=sum+t;
    }
    if(sum==temp)
         printf("%d is an Armstrong number",temp);
    else
         printf("%d is not an Armstrong number",temp);

 getch();
    return 0;
}
Sample output:
Enter a number: 1634
1634 is an Armstrong number


Wednesday, October 14, 2015

Write a program to check a number is a Prime number or Not?


Theory
Prime Number:  prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. 

A natural number greater than 1 that is not a prime number is called a composite number

For example, 5 is prime because 1 and 5 are its only positive integer factors, whereas 6 is composite because it has the divisors 2 and 3 in addition to 1 and 6. 

The fundamental theorem of arithmetic establishes the central role of primes in number theory

One is not a Prime Number:  Any integer greater than 1 can be expressed as a product of primes that is unique up to ordering. The uniqueness in this theorem requires excluding 1 as a prime because one can include arbitrarily many instances of 1 in any factorization, e.g., 3, 1 · 3, 1 · 1 · 3, etc. are all valid factorizations of 3.

The property of being prime (or not) is called primality.


As of September 2015, the largest known prime number has 17,425,170 decimal digits.


PROGRAM OF PRIME NUMBER IN C

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

int main()
{ 
   int n, i=3, c;

   printf("Enter the number of prime numbers required\n"); 
   scanf("%d",&n);

   if ( n <=1 ) 
     {
       printf("\n%d is not a Prime number: ",n);
       exit(0); //It is a Predefined Funtion in stdlib.h header file. To Normally Terminate the program.
     }

   for ( c = 2 ; c <n ; c++ ) 
     { 
      if ( n%c == 0 ) 
         break; // break will take execution out of the loop. 
     }  
   if ( c == n) // If my number is a prime it will successfully increment the value of c to n. 
    { 
       printf("\n%d Is a Prime number:\n",n); 
    }
   else //If My number is not a prime number it will never be equal to n.
    {
       printf("\n%d is not a Prime number: \n",n);
    }

getch(); 
return 0;
}