Fibonacci Series in C

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

FIBONACCI SERIES IN C

I METHOD #include<stdio.h>

int main()

unsigned int i=0,j=0,sum=1,num;

printf("nEnter the limit for the series ");

scanf("%d",&num);

while(sum<num)

printf("%d ",sum);

i=j;

j=sum;

sum=i+j;

SECOND METHOD

nt main()
{
int n i f1=0 f2=1 f3=0;
printf("Enter number of terms : ");
scanf(" d" &n);
if(n==1)
printf("0");
if (n==2)
printf("0 1");
if(n>=3)
{
for(i=3;i<=n;i++)
{
f3=f1+f2;
f1=f2;
f2=f3;
printf(" d" f3);
}
return 0;
}getch(); }

PALLENDROME
#include<stdio.h>
#include<conio.h>
void main()
{
int n,s=0,m;
clrscr();
printf("enter any no");
scanf("%d",&n);
m=n;
while(n>0)
{
r=n%10;
s=s*10+r;
n=n/10;
}
if(m==s)
printf("the no is palindrome");
else
printf("no is not palindrome");
getch();
}
BINARY SEARCH

#include
#include

void main()
{
int array[10];
int i, j, N, temp, keynum;
int low,mid,high;

clrscr();

printf("Enter the value of N\n");


scanf("%d",&N);

printf("Enter the elements one by one\n");


for(i=0; i
{
scanf("%d",&array[i]);
}
printf("Input array elements\n");
for(i=0; i
{
printf("%d\n",array[i]);
}
/* Bubble sorting begins */
for(i=0; i< N ; i++)
{
for(j=0; j< (N-i-1) ; j++)
{
if(array[j] > array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
printf("Sorted array is...\n");
for(i=0; i
{
printf("%d\n",array[i]);
}

printf("Enter the element to be searched\n");


scanf("%d", &keynum);

/* Binary searching begins */

low=1;
high=N;

do
{
mid= (low + high) / 2;
if ( keynum < array[mid] )
high = mid - 1;
else if ( keynum > array[mid])
low = mid + 1;
} while( keynum!=array[mid] && low <= high); /* End of do- while */

if( keynum == array[mid] )


{
printf("SUCCESSFUL SEARCH\n");
}
else
{
printf("Search is FAILED\n");
}

} /* End of main*/
BINARY TREE

#include<stdio.h>
#include<conio.h>
struct rec
{long num;

struct rec *left;


struct rec *right;
};
struct rec *tree=NULL;
struct rec *insert(struct rec *tree,long num);
void *exchange(struct rec *tree);
struct rec *temp;
void main()
{ struct rec *tree=NULL;

int choice; long digit; do{

choice=select();
switch(choice)
{

case 1: puts("Enter integer: To quit enter 0");


scanf("%ld",&digit);
while(digit!=0)
{
tree=insert(tree,digit);
scanf("%ld",&digit);
}continue;

case 2: printf("%5d\n",tree->num);exchange(tree);continue;
case 3: puts("END");exit(0);

}}while(choice!=3);

}int select()

{int selection;

do

{puts("Enter 1: Insert a node");

puts("Enter 2: Exchange subtrees");


puts("Enter 3: End");
puts("Enter your choice");
scanf("%d",&selection);
if((selection<1)||(selection>3))
{puts("Wrong choice: Try again");

getchar();
}}while((selection<1)||(selection>3));
return selection;

}struct rec *insert(struct rec *tree,long digit)

{if(tree==NULL)

{tree=(struct rec *)malloc(sizeof(struct rec));

tree->left=tree->right=NULL;

tree->num=digit;
}else

if(digit<tree->num)
tree->left=insert(tree->left,digit);
else if(digit>tree->num)
tree->right=insert(tree->right,digit);
else if(digit==tree->num)
{puts("Duplicates Nodes: Program Exited");exit(0);
}return(tree);

}void *exchange(struct rec *tree)

{if((tree->left->num!=0)&&(tree->right->num!=0))

{temp=tree->left;

tree->left=tree->right;
tree->right=temp;
printf("%5ld\n",tree->left->num);
printf("%5ld\n",tree->right->num);
exchange(tree->left);
exchange(tree->right);
}}

FACTORIAL

#include<stdio.h>
#include<conio.h>
void main()
{
int i,fact=1,n;
clrscr();
printf("\nEnter the no of terms =");
scanf("%d",&n);
for(i=1;i<=n;i++)
fact*=i;
printf("\nFactorial value for %d terms = %d",n,fact);
getch();
}

You might also like