Sunday, October 23, 2011

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();
}