Saturday 15 December 2012

Fibonacci series in c program | Mathematical functions code with execution and comments

Fibonacci series  in c program |  Mathematical functions code with execution and comments

How to print first n terms of Fibonacci series in c language
series of addition of last two number is third number.
0,1,2,3,5.........so on
0+1=1
1+2=3
2+3=5.
we can find out fibbonacci series at given number just we have to enter that number.
in this article we will see how to write series program in c language.

/*Program to print first n terms of Fibonacci series*/
#include<stdio.h> // including header files.
#include<conio.h>
void main() // start of main
{
int a,b,c,n,i; // initializing variables
clrscr();

//Accept no. of terms
printf("Enter no. of terms u want in the series: \n");
scanf("%d",&n);
a=1;  // first we initialized a=1 and b as 0. so we can start to in  the series.
b=0;

//print fibbonacci series
for(i=n;i!=0;i--)  //supose n=25 then  check i not equal 0 and decrement of i
{
c=a+b; //c=0+1=1
a=b;    // now now a=2
b=c;   //now b=1
ptint c=1
next execution
c=a+b; //c=0+1=1
a=b;    // now now a=0
b=c;   //now b=1

print 0,1

next execution
c=a+b; //c=2+2=1
a=b;    // now now a=2
b=c;   //now b=2

print 2  so on..


Like this it will print
0,1,2,3,5,8,13,21


printf("%d ",c);
}

getch();
}

Armstrong number example in c program with coments

Armstrong number example in c program with coments

Armstrong number in c program with source code | comments | execution line by line


In this article i have shown how to Write program for Armstrong number in c  using function with while loop.
Basically we can write simple program for Armstrong number but by using function if we are

writing then its  scope will be different.
first of all get all the idea about what is Armstrong number .
its definition then only one programmer can develop his logic.
Armstrong number is number which is equal to sum its individual number cube.
cube means multiplication of same number thrice or three times.
suppose i want to check number 153 is Armstrong or not, then answer is yes .
because , 153=(1*1*1) + (5*5*5) + (3*3*3) =1+125 + 27 =126 + 27=153.
in this line 1 and 5 and 3 are 153 's individual number .and sum of cube of all three

numbers that number.
so , i think you got the little idea about Armstrong number.
so here we are going to write Armstrong number program in c language.

#include<stdio.h>  // including header files
#include<conio.h>
 void armstrong(int n);   // calling function armstrong(int n)
void main()
{
int n;   //initializing variable n
clrscr();
printf("enter the number");
scanf("%d",&n);   //153
arm(n);    // now n=153
getch();
}
void arm (int n)
{
int r,sum=0,no;
no=n;   //no=153
while(n>0)
{
r=n%10;   //taking mod  (153%10)=15 and r=3 and n=15
sum=(r*r*r)+sum; //3*3*3 + sum=27+0=27 so sum=27
n=n/10;   // now n=15

    // after first execution


r=n%10;   //taking mod  (15%10)=1 and mod =5 and n=10
sum=(r*r*r)+sum; //5*5*5 + sum=125+27=152
n=n/10;   // now n=10

     // after second execution

r=n%10;   //taking mod  (10%10)=0 and mod =1 and n=0
sum=(r*r*r)+sum; //1*1*1 + sum=152+1=153
n=n/10;     // after last execution
}
 
if(no==sum)        // now sum=153 and no=153 both are same hence 153 is armstrong number
printf("given number is armstrrong");
else
printf("not armstrong");

}


Armstrong number program is very simple program just we  should know about c logic and loop

syntax and concept about program.

C program code for swap the number | example code and comments

C program code for swap the number | example code and comments

Swapping means change value of one variable into another variable.

suppose int a=9 int b=7
After swapping b=9 and a=7 .
In this program I have shown how to do swaping of number in c program using function


void swap(int,int); // function name is swap
void main()  // opening of main  method
{
int a=10,b=20;  // initializing variable
clrscr();
swap(a,b);  // calling function
getch();
}
void swap(int x, int y)
{
int temp;  // declaring temp variable
temp=x;  // storing value of x into temp    temp=10
x=y;       // storing value of y into x            x=20
y=temp;  // storing vallue of temp which was x value into y  y=10
printf("\n a=%d\n b=%d",x,y);
}

Typecasting in C | Miscelleneous Features |Definition with example and with comments

 Typecasting in C | Miscelleneous Features |Definition with example and  with comments

What is typecasting?
Converting one datatype into another data type is known as typecasting.
for example conversion of int to float and float to  int , int to char and so on..
Basically there is two types of typecasting ..
1->Implicit typecasting
2->Explicit type casting


1:-Implicit typecasting -type casting done by system.
int a=10.8 float

now here float values is  typecasted to int now a=10 and 0.8 is ignored
float b=25
integer value is typecasted to  float b=15.0
Now let see invalid example of  typecasting.
int a ="25"
char a[]=75;
there is no typecasting when string is involved.



2;>Explicit type casting .
type casting  is done by programmer.
int a=9
int b=2

a/b=9/2=4
(float)a/b=9.0/2=4.5
a/(float)b=9/2.0=4.5 variable a and b are type casted to float . they  become 9.0 and 2.0 .
Their values continued to be 9 & 2 in rest of program.



/*How to write Program to illustrate use of typecasting*/
#include<stdio.h>
#include<conio.h>

//main function
void main()
{
float a;
int x=6,y=4;
clrscr();

//Without typecasting x/y
a=x/y;
printf("\n Value of a without typecasting =%f",a);


////After typecasting x/y to float
a=(float) x/y;
printf("\n Value of a after typecasting =%f",a);
getch();
}