Thursday, 15 November 2012

Write a function to generate Fibonacci series upto given number.



#include<stdio.h>
 main()
 {
int i,n,sum,prev,curr;
prev=0,curr=1;
printf("Enter the no of values  " );
scanf("%d",&n);
printf("%d  %d", prev,curr);
for(i=1;i<=n-2;i++)
{
sum = prev+curr;
prev=curr;
curr=sum;
printf("  %d",sum);
}
 }


OUTPUT:


Enter the no of values  5
0  1  1  2  3


Write a function power(a,b) to calculate value of ’ a’ raised to’ b’



#include<stdio.h>
int power(int a,int b);
main()
{
int no1,no2,result;
printf("Enter two numbers for base and exponent: ");
scanf("%d %d",&no1,&no2);

result=power(no1,no2);
printf("\nThe Result of %d^%d is : %d",no1,no2,result);
}
int power(int x,int y)
{
int val,i;
val=1;
for(i=1;i<=y;i++)
{
val=val*x;
}
return val;
}

OUTPUT:

Entr two numbers fo base and exponent: 8
9

The Result of 8^9 is : 0

Write program to find largest of two numbers using function.


#include<stdio.h>
int findmax(int a,int b);
main()
{
int no1,no2,maxno;
printf("Enter two numbers : ");
scanf("%d %d",&no1,&no2);
maxno=findmax(no1,no2);
printf("Max number is %d",maxno);

}
int findmax(int x,int y)
{
if(x>y)
return x;
else
return y;
}



OUTPUT:


Enter two numbers : 5
6
Max number is 6

C PROGRAM TO DISPLAY SERIES 1 + 3 + 5.. AND FIND SUM TILL < 500

#include<stdio.h>
main()
{
    int i,no=500,prvsum,sum=0;
    i=1;
    while(i<=no)
    {
        prvsum=sum;
        sum=sum+i;
        if(sum>500)
        {
            sum=prvsum;
            break;
        }
        printf("%d ",i);
        i+=2;
        printf("+ ");
    }
    printf("\n The sum of this series is %d",sum);
}

OUTPUT

1 + 3 + 5 + 7 + 9 + 11 + 13 + 15 + 17 + 19 + 21 + 23 + 25 + 27 + 29 + 31 + 33 + 35 + 37 + 39 + 41 + 43 +
The sum of this series is 484 

Program to display series 1+3+5… and find sum Till given number


#include<stdio.h>
main()
{
int i,no,sum=0;
printf("Enter a number : ");
scanf("%d",&no);
i=1;
while(i<=no)
{
sum=sum+i;
printf("%d ",i);
i++;
printf("+ ");
}
printf("\nThe sum of this series is %d",sum);
}


OUTPUT:


Enter a number : 5
1 + 2 + 3 + 4 + 5 +
The sum of this series is 15