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

Thursday, 26 January 2012

Write a C program to print the Fibonacci Series upto to the first 'n' terms

#include<stdio.h>
#include<conio.h>
void main()
{
    int f=0, s=1, t, n, ct;
    clrscr();
    printf("Enter the value of 'n': ");
    scanf("%d",&n);
    printf("\nFibonacci series:\n");
    printf("%5d%5d", f, s);
    ct=3; //becoz we already displayed 2 values and we r going the third value
    while(ct<=n)
    {
        t=f+s;
        printf("%5d", t);
        f=s;
        s=t;
        ct++;
    }
    getch();
}

Write a C program to genarate all the prime numbers between 1 to 'n'

#include<stdio.h>
#include<conio.h>
void main()
{
    int st, n, x, flag;
    clrscr();
    printf("Enter the value of 'n': ");
    scanf("%d", &n);
    for(st=1; st<=n; st++)
    {
        x=2; flag=0;
        while(x<=st/2)
        {
            if(st%x==0)
            {
                flag=1; break;
            }
            x++;
        }
        if( flag==0)
        printf("%5d", st);
    }
    getch();
}

Write a C program to calculate the following Sum:- sum=1-x2/2! +x4/4!-x6/6!+x8/8!-x10/10!

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
    int ct, ft;
    float sum=0, x, power, fact;
    clrscr();
    printf("--------PROGRAM FOR SUM OF EQ. SERIES------");
    printf("\n\nEnter the value of X : ");
    scanf( "%f", &x);
    ct=0;
    for(power=0; power<=10; power=power+2)
    {
        fact=1; for(ft=power; ft>=1; ft--)
        fact =fact* ft;
        sum=sum+(pow(-1,ct)*(pow(x,power)/fact)); //EQ. FOR SUM SERIES
        ct++;
    }
    printf("\n Sum : %f",sum);
    getch();
}

Write a C program to find both the largest and smallest numbers in a list of integers.

#include<stdio.h>
#include<conio.h>
void main(void)
{
    int a[50], max, min, i, n;
    clrscr( );
    printf("Enter how many integers do u want? :");
    scanf(" %d", &n);
    for( i=0; i< n ; i++)
    {
        printf("\nEnter the %d number: ", i );
        scanf(" %d", &a[ i ]);
        if(i==0)
        {
            max=a[i ]; min=a[i ];
        }
        if(a[ i]>max)
        max= a[i ];
        if(a[i ]< min)
        min= a[i ];
    }
    printf("\n Maximum : %d", max);
    printf("\n Minimum : %d", min);
    getch( );
}

Write a C program which copies one file to another.

#include<stdio.h>
#include<conio.h>
void main()
{
    FILE *fp, *fq ;
    char file1[20], file2[20];
    char ch;
    clrscr( );
    printf("Enter the source filename to be copied:");
    gets(file1);
    fp=fopen(file1,"r");
    if(fp==NULL)
    {
        printf("\nCannot open %s",file1);
        exit(0);
    }
    printf("\nEnter the destination filename:");
    gets(file2);
    fq=fopen(file2,"w");
    while((ch=getc(fp))!=EOF )
    {
        putc(ch,fq);
    }
    printf("\n Copied Sucessfully..");
    fclose(fp);
    fclose(fq);
    getch( );
}

Write a C program to construct a pyramid of numbers.

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15



#include<stdio.h>
#include<conio.h>
void main( )
{
    int r, c, num=1 ,len;
    clrscr( );
    printf("Enter the required no. of rows:");
    scanf("%d", &len);
    for( r=1; r<=len; r++ )
    {
        printf("\n");
        for(c=1 ; c<=r; c++)
        {
            printf("%2d ", num);
            num++;
        }
    }
    getch( );
}

Write a C program to read in two numbers, x and n, and then compute the sum of this geometric progression: 1+x+x2+x3+………….+xn

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main( )
{
    int x, n, sum,power;
    clrscr( );
    printf("Enter the values of x and n:");
    scanf("%d%d", &x, &n);
    if(n<0)
    {
        printf("\nSorry, the formula does not make sense for negative exponents & values");
        printf("Enter the values of x and n:");
        scanf("%d%d", &x, &n);
        sum=1;
        for(power=1; power <=n; power++)
        {
            sum=sum+ pow(x,power);
        }
        printf("X value is: %d \nN value is: %d", x, n);
        printf("\nSum of the given geometric progression: %d", sum);
    }
    else
    {
        sum=1;
        for(power=1; power <=n; power++)
        {
            sum=sum+ pow(x,power);
        }
        printf("X value is: %d \nN value is: %d", x, n);
        printf("\nSum of the given geometric progression: %d", sum);
    }
    getch( );
}

Write a C program to insert a sub-string in to given main string from a given position.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main( )
{
    char st[20],sub[10],temp[10];
    int pos, i, j;
    clrscr( );
    printf("Enter the main string:");
    gets(st);
    printf("\nEnter the substring to insert:");
    gets(sub);
    printf("Enter the index position:");
    scanf("%d",&pos);
    if(pos<=strlen(st))
    {
        for(i=0;i<=strlen(st);i++)
        {
            if(i==pos)
            {
                for(j=0;st[i]!='\0';j++) // to store the 'st' to 'temp' from given position.
                {
                    temp[j]=st[i];
                    i++;
                }
                temp[j]='\0';
                i=pos;
                for(j=0;sub[j]!='\0';j++) // to insert a sub-str to main string.
                {
                    st[i]=sub[j];
                    i++;
                }
                for(j=0;temp[j]!='\0';j++) // Lastly to insert the 'temp' to 'st' after sub-str.
                {
                    st[i]=temp[j];
                    i++;
                }
                st[i]='\0';
            }
        }
        printf("\nAfter adding the sub-string: %s",st);
    }
    else
    printf("\nSorry, it is not possible to insert a substring in that position.");
    getch();
}

Write a C program to delete n Characters from a given position in a given string.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
    char st[20],temp[20];
    int pos,i,j,ct=0,n;
    clrscr();
    printf("Enter the string:");
    gets(st);
    printf("\nEntre the index position:");
    scanf("%d",&pos);
    printf("\nEnter the no.of characters:");
    scanf("%d",&n);
    if(pos<=strlen(st))
    {
        for(i=0;i<=strlen(st);i++)
        {
            if(i==pos)
            {
                for(j=0;st[i]!='\0';j++) // to store the 'st' in 'temp' from a giv pos
                {
                    temp[j]=st[i];
                    i++;
                }
                temp[j]='\0';
                i=pos;
                //to delete the 'n' char and after restore the temp in st.
                for(j=0;temp[j]!='\0';j++)
                {
                    ct++;
                    if(ct>n)
                    {
                        st[i]=temp[j];
                        i++;
                    }
                }
                st[i]='\0';
            }
        }
        printf("\n\nAfter deletion the string: %s",st);
    }
    else
    printf("\nSorry, we cannot delete from that position.");
    getch();
}

Write a C program to find the GCD (greatest common divisor) of two given integers using Recursive function.

#include<stdio.h>
#include<conio.h>
int gcd (int, int); //func. declaration.
void main( )
{
    int a, b, res;
    clrscr( );
    printf("Enter the two integer values:");
    scanf("%d%d", &a, &b);
    res= gcd(a, b); // calling function.
    printf("\nGCD of %d and %d is: %d", a, b, res);
    getch( );
}
int gcd( int x, int y) //called function.
{
    int z;
    z=x%y;
    if(z==0)
    return y;
    gcd(y,z); //recursive function
}

Write a C program to check whether the given integer is Magic number or not.

Magic number:- A number which is divisible by 9 and also after reverse if it is divisible by 9, then it is said to Magic number.

Example:- Suppose take 18
We know '18' is divisible by 9 (ie., 9 * 2 = 18)
Now After reverse '18' becomes 81.
Here '81' is also is divisible by 9 (ie., 9 * 9 =81 )
So we say '18' is a Magic number.


#include<stdio.h>
#include<conio.h>
void main( )
{
    int rem, rev=0, n;
    clrscr( );
    printf("Enter the required number:");
    scanf("%d",&n);
    while(n>0)
    {
        rem=n%10;
        rev=(10*rev)+ rem;
        n=n/10;
    }
    if(rev%9== 0)
    printf("\nMagic Number.");
    else
    printf("\nNot a Magic number.");
    getch( );
}

Output:-
Enter the required number: 27
Magic Number.

Write a C program to generate the Fibonocci series using Recursion.

#include<stdio.h>
#include<conio.h>
int fibno(int); // function declaration.
void main()
{
    int ct, n, disp;
    clrscr( );
    printf("Enter the no. of terms:");
    scanf("%d", &n);
    printf("\n The Fibonocci series:\n");
    for( ct=0; ct<=n-1; ct++)
    {
        disp= fibno(ct); //calling function.
        printf("%5d", disp);
    }
    getch( );
}

int fibno( int n)
{
    int x, y;
    if(n==0)
    return 0;
    else if(n==1)
    return 1;
    else
    {
        x= fibno( n-1);
        y= fibno( n-2);
        return (x+y);
    }
}


Output:- Enter the no. of terms: 10

The Fibonocci series:
0 1 1 2 3 5 8 13 21 34

Write a C program to find the factors of a given integer.

#include<stdio.h>
#include<conio.h>
void main( )
{
    int no,x;
    clrscr( );
    printf("Enter the required number:");
    scanf("%d",&no);
    printf("\nThe factors are:");
    for(x=1;x<=no;x++)
    {
        if(no%x==0)
        printf("\n%d",x);
    }
    getch( );
}


Output:-
Enter the required number: 27
The factors are:
1
3
9
27

Write a c program which gives you the temperature in Centigrade and in Fahrenheit

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

void centigrade();
void fahrenheit();

void main()
{
    int ch;
    char choice;
    do
    {
    clrscr();
    printf("Press 1 for Fahrenheit\n");
    printf("Press 2 for Centigrade\n");
    printf("Enter your choice:");
    scanf("%d",&ch);
    switch(ch)
    {
    case 1:
    centigrade();
    break;

    case 2:
    fahrenheit();
    break;

    default:
    printf("You have Entered wrong choice....\n");

    }
    printf("\nIf u want to continue then press y\n");
    choice=getch();
    if(choice!='y')
    break;
    else
    continue;
    }
    while(1);

    getch();
}

void centigrade()
{
    float c,f;
    printf("Enter the fahrenheit degrees:");
    scanf("%f", &f);
    c= (f-32)/1.8 ;
    printf("\nAfter Fahrenheit degrees, Centigrade Degrees = %f", c);
}

void fahrenheit()
{
    float c,f;
    printf("Enter the Centigrade degrees:");
    scanf("%d",&c);
    f= 1.8*c + 32.0;
    printf("\nAfter Centigrade degrees, Fahrenheit degrees = %f", f);
}

Write a C program to Calculate the sum of the series sum=1+1/x+1/x2+1/x3+...........+1/xn

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
    int i, x, n;
    float sum, p;
    clrscr();
    printf("Enter the base value 'x':");
    scanf("%d", &x);
    printf("\nEnter the power value 'n':");
    scanf(" %d", &n);
    sum=1;
    for( i=1; i<=n ; i++)
    {
        p=pow(x, i);
        sum=sum+(1/p);
    }
    printf("\nSum of the series: %f", sum);
    getch( );
}

Write a C program to find the roots of a Quadratic equation

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
    int a , b, c, d;
    float x1, x2,ab;
    ab:
    printf("Enter the value of a, b, c:");
    scanf("%d%d%d", &a, &b, &c);
    if(a==0)
    {
        printf("\nIt is a Linear Equation");
        printf("\nRe-Enter the data again.");
        goto ab;
    }
    d=(b*b)-(4*a*c);
    if(d==0)
    {
        printf("\nRoots are real and equal");
        x1= x2=(-b)/(2*a);
        printf("\nRoots are: %f \t %f ", x1, x2);
    }
    else
    {
        if(d<0)
        printf("\nRoots are imaginary");
        else
        {
            x1=((-b)+sqrt(d))/(2*a);
            x2=((-b)-sqrt(d))/(2*a);
            printf("\nRoots are real");
            printf("\nRoots are : %f \t %f", x1, x2);
        }
    }
    getch( );
}


Output:-
Enter the values of a, b,c : 5
2
4
Roots are imaginary.

Write a C program to print all Combinations of characters A, B, C

#include<stdio.h>
#include<conio.h>
void main()
{
    char ch1, ch2, ch3;
    clrscr();
    for(ch1='A' ; ch1<='C' ; ++ch1)
    {
        for(ch2='A' ; ch2<='C' ; ++ch2)
        for(ch3='A' ; ch3<='C' ; ++ch3)
        printf("%c%c%c\t", ch1, ch2, ch3);
    }
    getch();
}


Output:-
AAA AAB AAC ABA ABB ABC ACA ACB ACC
BAA BAB BAC BBA BBB BBC BCA BCB BCC
CAA CAB CAC CBA CBB CBC CCA CCB CCC

Write a C program to Insert an element at the 'n' th position

#include<stdio.h>
#include<conio.h>
void main()
{
    int a[20], len, i, j, x, n ;
    clrscr( );
    printf("Enter the array length:");
    scanf("%d", &len);
    printf("\nEnter the array elements:\n");
    for(i=0; i<len; i++) //remove the single quotes in for loop
    scanf("%d", &a[i]);
    printf("\nEnter the 'n' th position:");
    scanf("%d", &n);
    printf("\nEnter the array element you want to insert:");
    scanf("%d", &x);
    for(j=len-1;j>=n-1;j--)
    {
        a[j+1]= a[j];
    }
    a[n-1]= x;
    len=len+1;
    printf("\nThe New List is :\n");
    for( i=0; i<len; i++) //remove the single quotes in for loop
    printf("%5d",a[i]);
    getch( );
}

 
Output:-
Enter the array length : 4
Enter the array elements: 5
8
1
9
Enter the 'n' position: 4
Enter the array element you want to insert: 10
The new List is:
5
8
1
10
9

Write a C program to delete an element from the array.

#include<stdio.h>
#include<conio.h>
void main()
{
    int a[20],len,i,n,item,j;
    clrscr();
    printf("Enter how many elements you want to enter:");
    scanf("%d",&len);
    printf("\nEnter the array elements:\n");
    for(i=0;i<len;i++) // remove the single quotes in for loop
    scanf("%d",&a[i]);
    printf("\nEnter the location, where you want to delete:");
    scanf("%d",&n);
    item= a[n-1];
    printf("\nDeleted value is : %d",item);
    for( j= n-1;j<len-1;j++)
    {
        a[j]= a[j+1];
    }
    len=len-1;
    printf("\nThe new element list :");
    for(i=0;i<len;i++)
    printf("%5d", a[i]);
    getch();
}


Enter how many elements you want to enter: 4
Enter the array elements:
10
20
30
40
Enter the location, where you want to delete: 3
Deleted value : 30
The new element list: 10 20 40

Write a C program to print all Armstrong numbers between 1 to 1000.

#include<stdio.h>
#include<conio.h>
void main()
{
    int no,temp,rem,sum;
    clrscr();
    printf("Armstrong numbers between 1 and 1000 are:\n");
    for(no=1; no<=1000; no++)
    {
        temp=no;
        sum=0;
        while(temp>0)
        {
            rem=temp%10;
            sum=sum+(rem*rem*rem);
            temp=temp/10;
        }
        if(no==sum)
        printf("\n%d", no);
    }
    getch( );
}



Output:
Armstrong numbers between 1 and 1000 are:
1
153
370
371
407