Showing posts with label C programming. Show all posts
Showing posts with label C programming. Show all posts

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.