Wednesday, November 2, 2011

Square Root Without Functions {Newton's Method}

#include<stdio.h>
#include<conio.h>
void main()
{
    float g=1,q,num;
    int i;
    clrscr();
    printf("\nEnter a number:");
    scanf("%f",&num);
    g=1.0;
    for(i=0;i<10;i++)
    {
        q=num/g;
        g=(q+g)/2.0;
    }
    printf("\nSquare root:%f",g);
    getch();
}

Sunday, October 30, 2011

Sum Of Binomial Series

//This Program Finds the Sum of series=4+(4+16)+(4+16+36+64)+(4+16+36+......n)
#include<stdio.h>
#include<conio.h>
void main()
{
    int i,j=0,k,n,d,a[100],sum=0;
    clrscr();
    printf("\nEnter Nth Term:");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        j=j+2;
        a[i]=j;
    }
    i=0;
    j=0;
    printf("\nSeries is:");
    while(1)
    {
        k=2;
        for(j=0;j<=i;j++)
        {
            d=k*a[j];
            sum=sum+d;
            if(j==(n-1))
            {
                printf("%d=%d",d,sum);
                getch();
                exit(0);
            }
            else
            printf("%d+",d);
            k=k+2;
        }
        i++;
    }
}

Output:

Sum Of Series

// A Program to find Sum of Series:2^2+(2^2+4^2)+(2^2+4^2+6^2)+(2^2+4^2+6^2+......n^2)

#include<stdio.h>
#include<conio.h>
void main()
{
    int i,j,k,n,d,sum=0;
    clrscr();
    printf("\nEnter Nth Even Term:");
    scanf("%d",&n);
    i=0;
    while(1)
    {
        k=2;
        for(j=0;j<=i;j++)
        {
            d=k*k;
            sum=sum+d;
            if(k==n)
            {
                printf("\nSum Of Terms:%d",sum);
                getch();
                exit(0);
            }
            k=k+2;
        }
        i++;
    }
}

Friday, October 28, 2011

Decryption {File Reading}

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
    char a[100],b[100];
    int l,i,j;
    char ch;
    FILE *fp;
    fp=fopen("Encrypt.txt","r");
    clrscr();
    if(fp==NULL)
    {
        printf("\nFile Cannot Be Found");
        getch();
        exit(0);
    }
    fscanf(fp,"%s",a);
    printf("\nEncrypted String is:");
    puts(a);
    l=strlen(a);
    for(i=0;i<l;i++)
    {
        ch=a[i];
        for(j=0;j<l;j++)
        ch--;
        b[i]=ch;
    }
    puts("\nThe Decrypted String is:");
    puts(b);
    fclose(fp);
    if(remove("Encrypt.txt")==-1)
    perror("\nError in Deleating a file");
    getch();
}

Encryption {File Writing}

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
    char a[100],b[100];
    int l,i,j=0;
    char ch;
    FILE *fp;
    clrscr();
    fp=fopen("Encrypt.txt","w");
    printf("\nEnter The String To Be Encrypted:");
    gets(a);
    l=strlen(a);
    if(fp==NULL)
    {
        printf("\nNot Enough Memory");
        getch();
        exit(0);
    }
    for(i=0;i<l;i++)
    {
          ch=a[i];
          for(j=0;j<l;j++)
          ch++;
          b[i]=ch;

    }
    printf("\nAfter Encrypting The String is:");
    puts(b);
    fprintf(fp,"%s",b);
    fclose(fp);
    printf("\nData Has Been Succesfully Written in Encrypt.txt");
    getch();
}

Thursday, October 27, 2011

Electricity Payment

#include<stdio.h>
#include<conio.h>
void main()
{
    int units;
    float total_bill;
    clrscr();
    printf("Enter the no. of units");
    scanf("%d",&units);
    if(units <= 100)
    total_bill = units * 4;
    else if(units <= 300)
    total_bill = units * 4.5;
    else if(units <= 500)
    total_bill = units * 4.75;
    else
    total_bill = units * 5;
    printf("\nThe Bill Amount Is =%f", total_bill);
    getch();
}

Sunday, October 23, 2011

Perfect Number

Perfect number:- Sum of the factorials of a individual digit is known as Perfect number

#include<stdio.h>
#include<conio.h>
void main( )
{
    int no, rem, sum=0, temp, fact;
    clrscr( );
    printf("Enter the required number:");
    scanf("%d", &no);
    temp=no;
    while(no>0)
    {
        fact=1;
        for( rem= no%10 ; rem>=1; rem--)
        {
            fact=fact*rem;
        }
        sum = sum+ fact;
        no = no/10;
    }
    if(temp == sum)
    printf("\n%d is Perfect number", temp);
    else
    printf("\n%d is not a Perfect number", temp);
    getch();
}

Output:-
Enter the required number: 145

145 is Perfect number.