Monday, April 8, 2024

Fibonacci Series

/*

prints fibonacci series upto a given term.
This code made a different approach for printing terms.

*/

#include<stdio.h>

void main()
{
        int n,c,a=0,b=1;
        printf("enter number of terms : ");
        scanf("%d",&n);
        c=n/2+n%2;
        printf("Fibonacci Series : \n\n");
        while(c>0)
        {
                if(n%2==1 && c==1)
                        printf("%d ",a);
                else
                        printf("%d %d ",a,b);
                a=a+b;
                b=a+b;
                c--;
        }
        printf("\n");
}

Sunday, February 8, 2015

displaying sin, cos and tan series


#include< stdio.h >
#include< math.h >
long double sine(float);
long double cosine(float);
long double tangent(float);
void main()
{
    int count=0;
    float x;
    long double y1,y2,y3,yt;
    clrscr();

    for(x=0;x<=360;x+=5)
    {
        y1=sine(x);
        y2=cosine(x);
        y3=tangent(x);
        if(y3<0 br="">            yt=-y3;
        else
            yt=y3;
        count++;
        printf("sin(%3.0f) = %12.8Lf  cos(%3.0f) = %12.8Lf",x,y1,x,y2);
        if(yt>80)
            printf("  tan(%3.0f) = **undefined**\n",x);
        else
            printf("  tan(%3.0f) = %12.8Lf\n",x,y3);
        if(count%20==0)
        {
            printf("\npress any key to continue...");
            getch();
            clrscr();
        }

    }
    getch();
}
long double sine(float x)
{
    long double csinx,psinx,cps,fact,xp,xrad;
    int n,sf,i;
    xrad=x*3.141592653589793/180;
    csinx=xrad;
    psinx=1;
    cps=csinx-psinx;
    if(cps<0 br="">        cps=-cps;
    sf=-1;
    n=3;
    while(cps>1e-16)
    {
        psinx=csinx;
        xp=1;
        fact=1;
        for(i=1;i<=n;i++)
        {
            xp=xp*xrad;
            fact=fact*i;
        }
        csinx=csinx+sf*xp/fact;
        cps=csinx-psinx;
        if(cps<0 br="">            cps=-cps;
        n=n+i;
        sf=-sf;
    }
    return csinx;
}
long double cosine(float x)
{
    long double ccosx,pcosx,cps,fact,xp,xrad;
    int n,sf,i;
    xrad=x*3.141592653589793/180;
    ccosx=1;
    pcosx=0;
    cps=ccosx-pcosx;
    if(cps<0 br="">        cps=-cps;
    sf=-1;
    n=2;
    while(cps>1e-16)
    {
        pcosx=ccosx;
        xp=1;
        fact=1;
        for(i=1;i<=n;i++)
        {
            xp=xp*xrad;
            fact=fact*i;
        }
        ccosx=ccosx+sf*xp/fact;
        cps=ccosx-pcosx;
        if(cps<0 br="">            cps=-cps;
        n=n+i;
        sf=-sf;
    }
    return ccosx;
}
long double tangent(float x)
{
    long double y;
    y=sine(x)/cosine(x);
    return y;
}

counts size of different variables used in c language

 /* simple program that counts size of different variables used in c language */

#include< stdio.h>
void main()
{
    clrscr();

    printf("Size of different basic data types : \n\n");
    printf("data type    size (byte)\n");
    printf("\nint        %d",sizeof(int));
    printf("\nchar        %d",sizeof(char));
    printf("\nfloat        %d",sizeof(float));
    printf("\nlong        %d",sizeof(long));
    printf("\nshort        %d",sizeof(short));
    printf("\ndouble        %d",sizeof(double));

    getch();
}

counting vowles and consonents from a file

 /*This program counts  :
1. number of alphabets
2. number of vowles
3. number of consonents
4. number of lines

from a specified file.
*/

#include< stdio.h >
void main()
{
    FILE *fp1;
    char file1[50];
    unsigned char ch;
    long int noa,nov,nol,noc;
    char ch1;
    do
    {
        clrscr();
        printf("enter file name : ");
        gets(file1);
        fp1=fopen(file1,"rb");
        noa=nov=noc=nol=0;
        while(fscanf(fp1,"%c",&ch)>0)
        {
            if((ch>='A' && ch<='Z')||(ch>='a' && ch<='z'))
            {
                noa++;
                if(ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U' || ch=='a' || ch=='e' || ch=='i' ||ch=='o' || ch=='u' )
                    nov++;
            }
            else if(ch==13)
                nol++;
        }
        noc=noa-nov;
        fclose(fp1);
        printf("\nnumber of alphabets = %ld\nnumber of vowles = %ld\nnumber of consonents = %ld\nnumber of lines = %ld\n",noa,nov,noc,nol);
        printf("do you want to continue (y/n?) : ");
        ch1=getch();
    }while(ch1=='y'||ch1=='Y');
}

Thursday, February 5, 2015

rearranging numbers according to sign

 /*
prints positive and negative numbers separately from a series of numbers.
eg. 1 8 -5 -9 3 -4
will print 1 8 3 -5 -9 -4
*/

#include< stdio.h>
#include< conio.h>

void main()
{
    int n,a[20],b[20],c[20],i,j,k;
    char ch;
    do
    {
        clrscr();
        a[0]=0;
        j=k=0;
        printf("Enter a decimal no (1-10) : ");
        scanf("%d",&n);
        printf("\nEnter numbers -->\n\n");
        for(i=0;i        {
            printf("a[%d] = ",i);
            scanf("%d",&a[i]);
        }
        for(i=0;i        {
            if(a[i]>=0)
            {
              b[j]=a[i];
              j++;
            }
            else
            {
              c[k]=a[i];
              k++;
            }
        }
        printf("\n\n:: RESULT ::\n\n");
        for(i=0;i            printf("%d  ",b[i]);
        for(i=0;i            printf("%d  ",c[i]);

        printf("\n\n\nContinue with another no : (y/n) ? : ");

        ch=getch();
    }while(ch=='y'||ch=='Y');
}

Thursday, January 29, 2015

To find Prime numbers within a given range

#include < stdio.h >
#include < conio.h >
void main()
{
    int p,i,flag,j;

    clrscr();
    printf("To find Prime numbers within a given range\n\n");
    while(1)
    {
        printf("Enter a number (1-1000) : ");
        scanf("%d",&p);
        if(p<1 p="">1000)
            printf("\n\n\n***invalid input***\n\n\n");
        else
            break;
    }
    clrscr();
    printf("\n\nprime numbers between 1-%d are listed below :\n\n",p);
    for(i=1;i<=p;i++)
    {
        flag=1;
        for(j=2;j<=i-1;j++)
        {
            if(i%j==0)
            {
                flag=0;
                break;
            }
        }
        if(flag==1)
            printf("%5d",i);
    }
    getch();
}

fibonacci series by using recurcusion method

#include < stdio.h >
#include < time.h >
long double fib(int);
void main()
{
    int i,n;
    long double f;
    long int t1,t2,t3,*t=NULL;
    char ch;
    do
    {
        clrscr();
        printf("\nenter upper limit : ");
        scanf("%d",&n);
        for(i=1;i<=n;i++)
        {
            t1=time(t);
            f=fib(i);
            t2=time(t);
            t3=t2-t1;
            printf("fib(%3d) = %30.0Lf time = %ld sec\n",i,f,t3);
        }
        printf("\ndo you want to continue (y/n?) : ");
        ch=getch();
    }while(ch=='y'||ch=='Y');
}
long double fib(int n)
{
    if(n<=2)
        return 1;
    else
        return n=fib(n-1)+fib(n-2);
}