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

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

C PROGRAM TO CALCULATE NUMBER OF POSITIVE, NEGATIVE AND ZEROS ENTER BY USER. ALLOW USER TO ENTER THE NUMBER TILL HE/SHE WANTS

#include<stdio.h>
main()
{
    int no,pcount,ncount,zcount;
    char ch;
    ncount=0;
    pcount=zcount=ncount;
    do
    {
        printf("enter a no  ");
        scanf("%d",&no);
        if(no>0)
            pcount++;
        else if(no<0)
            ncount++;        else

            zcount++;
        printf("Do you want to enter more numbers(y/n):");
        getchar();
        ch=getchar();
    }
        while(ch=='y');
        printf("\n positive numbers   negative numbers     zeros");
        printf("\n ---------------------------------------------");
        printf("\n %8d  %15d %17d",pcount,ncount,zcount);
}



OUTPUT

enter a no  46
Do you want to enter more numbers(y/n):y
enter a no  89
Do you want to enter more numbers(y/n):nPositive numbers    negative numbers    zeros
---------------------------------------------
        2                    0              0

Program to calculate binary equivalent of any given number.


#include<stdio.h>
main()
{
   int n=0,i=0,j=0;
   int a[31];
   printf("\nEnter the number \n");
   scanf("%d",&n);
   for(i=0;i<32;i++)
    {
      a[i]=n%2;
      n=n/2;
     }
    for(i=31;i>=0;i--)
    {
   printf("%d",a[i]);
  }
}


OUTPUT:

Enter the number
6
000000000000000000000000000110

Program to reverse any given number.


#include<stdio.h>
main()
{
int n,rem,rev,no;
printf("enter a no  ");
scanf("%d",&no);
n=no,rev=0;
while(n!=0)
{
rem = n %10;
if(rem ==0)
printf("0");
rev = rev*10+rem;
n =n/10;
}
printf("The reverse of %d is %d", no,rev);
return 0;

}


OUTPUT:



enter a no  456
The reverse of 456 is 654


Program to calculate sum of digits of a five digit number.



#include<stdio.h>
main()
{
int sum,rem,no;
printf("enter a five digit number  ");
scanf("%d",&no);
sum=0,rem=0;
if(no==0)
printf("%d",no);
while(no!=0)
{
rem = no %10;
sum = sum+rem;
no = no/10;
}
printf("Sum is %d",sum);

}


OUTPUT:


enter a five digit number  12345
Sum is 15