Friday, December 30, 2011

Frequency Of A Number

#include
#include
void main()
{
int i,n,d,count=0;
clrscr();
printf("Enter The Number:");
scanf("%d",&n);
printf("\nEnter The Number To Be Searched:");
scanf("%d",&i);
while(n!=0)
{
d=n%10;
if(d==i)
count++;
n=n/10;

}
if(count>0)
printf("\nThe Frequency Is:%d",count);
else
printf("Number Not Found");
getch();
}

Binary To Decimal

#include
#include
#include
void main()
{
int n,i,d,sum=0;
clrscr();
printf("Enter a Number:");
scanf("%d",&n);
for(i=1;n!=0;i++)
{
d=n%10;
sum=sum+(pow(2,i)*d);
n=n/10;
}
printf("Num Is:%d",sum);
getch();
}

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.

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.

#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.

Sum Of Natural Numbers

#include< stdio.h>
#include< conio.h>
void main()
{
    int n,i,sum=0;
    clrscr();
    printf("Enter the 'n' value:");
    scanf("%d",&n);
    for(i=1; i<=n; i++)
    {
        sum=sum+i;
    }
    printf("\nSum of the 'n' terms: %d",sum);
    getch();
}

Odd Number Series

#include< stdio.h>
#include< conio.h>
void main( )
{
    int i,n;
    clrscr( );
    printf("Enter the range:");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        if(i%2!=0)
        printf("%d ",i);
    }
    getch( );
}

Output:-
Enter the range:11
1 3 5 7 9 11

Even Number Series

#include< stdio.h>
#include< conio.h>
void main( )
{
    int i,n;
    clrscr( );
    printf("Enter the range:");
    scanf("%d",&n);
    for(i=2;i<=n;i=i+2)
    {
        printf("%d",i);
    }
    getch( );
}

Output:-
Enter the range:10
2 4 6 8 10

Sum of Exponential Series

Write a C program to find the exponential series of 1+x+x2/2!+x3/3!+.......+xn/n!
#include<stdio.h>
#include<conio.h>
#include< math.h>
void main( )
{
    int x, n, fact, i, j;
    float sum=1;
    clrscr( );
    printf("Enter the 'x' value:");
    scanf("%d",&x);
    printf("\nEnter the 'n' value:");
    scanf("%d",&n);
    for(i=1; i< =n ; i++)
    {
        fact=1;
        for( j=i ; j >=1; j--)
        fact=fact*j;
        sum=sum+(pow(x,i)/ fact);
    }
    printf("\nSum of the series : %f ",sum);
    getch( );
}

GCD and LCM

#include< stido.h>
#include< conio.h>
void main( )
{
    int a,b,gcd,lcm;
    clrscr( );
    printf("Enter the two integer values:");
    scanf("%d%d", &a, &b);
    lcm=a*b;
    while(a!=b)
    {
        if(a>b)
        a=a-b;
        else
        b=b-a;
    }
    gcd=a;
    lcm=lcm/gcd;
    printf("\n The GCD is : %d",gcd);
    printf("\n The LCM is : %d",lcm);
    getch( );
}

Bubble Sort {Strings}

#include<stdio.h>
#include<string.h>
void main()
{
    int i,k,n;
    char name[100][50],temp[50];
    printf("\nEnter number of persons:");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    gets(name[i]);
    for(k=0;k<(n-1);k++)
    {
        for(i=0;i<(n-k-1);i++)
        {
            if(strcmp(name[i],name[i+1]>0)
            {
                strcpy(temp,name[i]);
                strcpy(name[i],name[i+1]);
                strcpy(name[i+1],temp);
            }
        }
    }
    printf("\nSorted List....\n");
    for(i=0;i<n;++i)
    puts(name[i]);

}

Factorial {Recursion}

#include<stdio.h>
#include<conio.h>
int factorial(int);
void main()
{
    int n,result;
    clrscr();
    printf("enter the no.:");
    scanf("%d",&n);
    result=factorial(n);
    printf("factorial of %d is %d",n,result);
    getch();
}
int factorial(int n)
{
    int f;
    if(n==0||n==1)
    {
        return 1;
    }
    else
    {
        f=n*factorial(n-1);
        return f;
    }
}

Roots of Quadratic Equation

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
    float a,b,c,r1,r2,d;
    clrscr();
    printf("enter value of a,b,c");
    scanf("%f%f%f",&a,&b,&c);
    d=b*b-4*a*c;
    if (d<0)
    {
        printf("roots are imaginary");
    }
    else if(d==0)
    {
        r1=-b/(2*a);
        r2=r1;
        printf("r1 and r2 are%f%f",r1,r2);
    }
    else
    {
        r1=(-b+sqrt(d))/2*a;
        r2=(-b-sqrt(d))/2*a;
        printf("r1 andr2 are%f%f",r1,r2);
    }
getch();
}

Triangle Types

#include<stdio.h>
#include<conio.h>
void main()
{
    int a,b,c;
    clrscr();
    printf("enter value of a:");
    scanf("%d",&a);
    printf("enter value of b:");
    scanf("%d",&b);
    printf("enter value of c:");
    scanf("%d",&c);
    if((a+b)>c&&(b+c)>a&&(a+c)>b)
    {
        printf("triangle is valid");
        if(a==b&&b==c&&c==a)
        {
            printf("triangle is equilateral");
        }   
        else if(a!=b&&b!=c&&c!=a)
        {
            printf("triangle is scalene");
        }
        else
        {
            printf("triangle is isoceles.");
        }
    }
    else
    {
        printf("triangle is invalid");
    }
    getch();
}

Student Grades

#include<stdio.h>
#include<conio.h>
void main()
{
    float m1,m2,m3,m4,m5,avg;
    clrscr();
    printf("enter value of m1:");
    scanf("%f",&m1);
    printf("enter value of m2:");
    scanf("%f",&m2);
    printf("enter value of m3:");
    scanf("%f",&m3);
    printf("enter value of m4:");
    scanf("%f",&m4);
    printf("enter value of m5:");
    scanf("%f",&m5);
    avg=(m1+m2+m3+m4+m5)/5;
    if(avg>=60)
    {
        printf("grade A");
    }
    else if (avg<60 && avg>=50)
    {
        printf("grade B");
    }
    else if (avg<50 && avg>=40)
    {
    printf("grade C");
    }
        else if ( avg<40)
    {
    printf("grade F");
    }
    getch();
}

Monday, October 17, 2011

Complex Numbers {Addition}

#include<stdio.h>
#include<conio.h>
struct complex
{
   int real;
   int img;
};
void main()
{
   struct complex a, b, c;
   clrscr();
   printf("Enter a and b where a + ib is the first complex number.");
   printf("\na = ");
   scanf("%d", &a.real);
   printf("b = ");
   scanf("%d", &a.img);
   printf("Enter c and d where c + id is the second complex number.");
   printf("\nc = ");
   scanf("%d", &b.real);
   printf("d = ");
   scanf("%d", &b.img);
   c.real = a.real + b.real;
   c.img = a.img + b.img;
   if ( c.img >= 0 )
      printf("Sum of two complex numbers = %d + %di",c.real,c.img);
   else
      printf("Sum of two complex numbers = %d %di",c.real,c.img);
   getch();
}

Sunday, October 16, 2011

Matrices {Multiplication}

#include<stdio.h>
#include<conio.h>
void main()
{
   int m, n, p, q, c, d, k, sum = 0;
   int first[10][10], second[10][10], mul[10][10];
   clrscr();
   printf("Enter the number of rows and columns of first matrix\n");
   scanf("%d%d",&m,&n);
   printf("Enter the elements of first matrix\n");
   for (  c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++ )
         scanf("%d",&first[c][d]);
   printf("Enter the number of rows and columns of second matrix\n");
   scanf("%d%d",&p,&q);
   if ( n != p )
      printf("Matrices with entered orders can't be multiplied with each other.\n");
   else
   {
      printf("Enter the elements of second matrix\n");
      for ( c = 0 ; c < p ; c++ )
         for ( d = 0 ; d < q ; d++ )
            scanf("%d",&second[c][d]);
      for ( c = 0 ; c < m ; c++ )
      {
         for ( d = 0 ; d < n ; d++ )
         {
            for ( k = 0 ; k < p ; k++ )
            {
               sum = sum + first[c][k]*second[k][d];
            }
            mul[c][d] = sum;
            sum = 0;
         }
      }
      printf("Product of entered matrices:-\n");
      for ( c = 0 ; c < m ; c++ )
      {
         for ( d = 0 ; d < q ; d++ )
            printf("%d\t",mul[c][d]);
         printf("\n");
      }
   }
   getch();
}