Monday, December 9, 2013

Newton Raphson Method to solve non-linear equations

Introduction

It is one of the most widely used methods of solving equation as it is more rapidly convergent than other methods. Starting from single initial estimate, x0, that is not too far from a root, we move along the tangent to its intersection with the x-axis, and take that as the next approximation. This is continued as
x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)} \,
until either the successive x-values are sufficiently close or the value of the function is sufficiently near zero.


Newton Raphson Method in Codes

/*A program to find a root of function using Newton Raphson Method */

#include<stdio.h>
#include<math.h>

float f(float x)
{
    return x*sin(x) + cos(x);
}

float g(float x)
{
    return x*cos(x);        /* Derivative of function f(x) */
}

int main()
{
    float x0, x1, a, b, c;

    printf("Enter the 'a' and 'b' of interval (a,b)\n\n");
    scanf("%f%f",&a,&b);

    x0= (a+b)/2;

    for(c=0;c<20;c++)     /* The loop is repeated just 20 times as this method is expected to give root in 5/10 steps generally */
    {
        if(g(x0)==0)
            printf("\n\nErooooooooorrrrrr");
        else
            x1 = x0 - f(x0) / g(x0);

        x0=x1;
    }

    printf("\n\nThe required root is %f\n\n",x0);
    return 0;

}

Friday, December 6, 2013

Roots by Bisection Method

Introduction

Bisection method is one of the most ancient and surely the simplest method to find the root of a function. But it is relatively time consuming method. First, we must know an interval in which a root lies. Root is found by repeatedly bisecting an interval. Successive values converge on a root of function f(x) when we begin with a pair of values that bracket the root. Let's take the interval (x1, x2) , x3 is halfway between x1 and x2, x4 is halfway between x2 and x3. We always take the next x-value as the midpoint of the last pair that bracket the root : these values bracket the root when there is sign change of f(x) at the two points. We repeat the process until we converge to the root.



Using C programming language to solve a function by Bisection Method


/*  To determine the root of a function by Bisection Method */

#include<stdio.h>
#include<math.h>

float f(float x)
{
    return x*sin(x) + cos(x);                   /* A continuous equation between given interval*/
 
}


int main()
{
    float a,b,c,d,e;

    printf("\nEnter  the interval (a,b)\n");
    scanf("%f%f",&a,&b);

    printf("\nEnter the tolerance value\n");
    scanf("%f",&d);

    do
    {
        c = (a+b)/2;
        if(f(c) * f(a) < 0)
            b=c;
        else
            a=c;
    }while( fabs(a-b)>d||f(c)==0);

    printf("Root is %f",c);

    return 0;

}

Sunday, February 19, 2012

String concatenation using pointer


/* A program to concatenate two string using pointer  */

#include<stdio.h>

void concatenate_string(char*, char*);

int main()
{
    char original[100], add[100];

    printf("Enter source string\n");
    gets(original);

    printf("Enter string to concatenate\n");
    gets(add);

    concatenate_string(original, add);

    printf("String after concatenation is \"%s\"\n", original);

    return 0;
}

void concatenate_string(char *original, char *add)
{
   while(*original)
      original++;

   while(*add)
   {
      *original = *add;
      add++;
      original++;
   }
   *original = '\0';
}

Matrix multiply using pointer


/*A program to multiply two matrices using pointer */

#include<stdio.h>
int main()
{
    int a[10][10],b[10][10],c[10][10],sum=0;
    int m1,m2,n1,n2,i,j,k;
    int *ptr1,*ptr2,*ptr3;
    ptr1=a;ptr2=b;ptr3=c;

    printf("enter no of row and column for 1st matrix\n");
    scanf("%d%d",&m1,&n1);
    printf("enter no of row and column for 2nd matrix\n");
    scanf("%d%d",&m2,&n2);

    if(n1==m2)
    {
        printf("for 1st matrix\n");
        for(i=0;i<m1;i++)
            for(j=0;j<n1;j++)
            {
                printf("a[%d][%d]=",i,j);
                scanf("%d",ptr1+i*10+j);
            }

        printf("\nfor 2nd matrix\n");
        for(i=0;i<m2;i++)
            for(j=0;j<n2;j++)
            {
                printf("b[%d][%d]=",i,j);
                scanf("%d",ptr2+i*10+j);
            }


        for(i=0;i<m1;i++)
        {
            for(j=0;j<n2;j++)
            {
               for(k=0;k<n2;k++)
                 sum+=*(ptr1+i*10+k)**(ptr2+k*10+j);
            *(ptr3+i*10+j)=sum;
            sum=0;
            }
        }

        printf("product of entered matrices :-\n");
        for(i=0;i<m1;i++)
        {
            for(j=0;j<n2;j++)
                printf("%d\t",*(ptr3+i*10+j));
            printf("\n");
        }


    }
    else
    printf("multiplication is not possible");
    return 0;
}

Matrix multiply


/* A program to multiply two matrices */

#include<stdio.h>
int main()
{
    int a[10][10],b[10][10],c[10][10],sum=0;
    int m1,m2,n1,n2,i,j,k;

    printf("enter no of row and column for 1st matrix\n");
    scanf("%d%d",&m1,&n1);
    printf("enter no of row and column for 2nd matrix\n");
    scanf("%d%d",&m2,&n2);

    if(n1==m2)
    {
        printf("for 1st matrix\n");
        for(i=0;i<m1;i++)
            for(j=0;j<n1;j++)
            {
                printf("a[%d][%d]=",i,j);
                scanf("%d",&a[i][j]);
            }

        printf("\nfor 2nd matrix\n");
        for(i=0;i<m2;i++)
            for(j=0;j<n2;j++)
            {
                printf("b[%d][%d]=",i,j);
                scanf("%d",&b[i][j]);
            }
        for(i=0;i<m1;i++)
        {
            for(j=0;j<n2;j++)
            {
               for(k=0;k<n2;k++)
                 sum+=a[i][k]*b[k][j];
            c[i][j]=sum;
            sum=0;
            }
        }

        printf("product of entered matrices :-\n");
        for(i=0;i<m1;i++)
        {
            for(j=0;j<n2;j++)
                printf("%d\t",c[i][j]);
            printf("\n");
        }


    }
    else
    printf("multiplication is not possible");
    return 0;
}

Sunday, January 15, 2012

sum using recursive function


/* A program to find the sum of given non-negative integer numbers using a recursive function */

#include<stdio.h>

int sum(int a)
{
    scanf("%d",&a);
    if(a<=0)
        return a; /*can return zero value as well*/
    else
        return(a+sum(a));
}
int main()

{
    int a;
    printf("enter numbers\n");
    printf("sum= %d",sum(a));
    return 0;

}

Saturday, January 14, 2012

Vowel count


/*A program to  Count vowel*/

#include<stdio.h>

int count_vowels(char []);
int check_vowel(char);

main()
{
char array[100];
int c;

printf("Enter a string\n");
gets(array);

c = count_vowels(array);

printf("Number of vowels: %d\n", c);

return 0;
}

int count_vowels(char a[])
{
int count = 0, c = 0, flag;
char d;

do
{
d = a[c];

flag = check_vowel(d);

if ( flag == 1 )
count++;

c++;
}while( d != '\0' );

return count;
}

int check_vowel(char a)
{
if ( a >= 'A' && a <= 'Z' )
a = a + 'a' - 'A'; /* Converting to lower case */

if ( a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
return 1;

return 0;
}

Sunday, January 8, 2012

Add two matrices


/*A program to add two matrices*/

#include<stdio.h>

int main()
{
   int m, n, c, d, mat1[10][10], mat2[10][10], sum[10][10];

   printf("Enter the number of rows and columns of 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",&mat1[c][d]);

   printf("Enter the elements of second matrix\n");

   for ( c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++ )
            scanf("%d",&mat2[c][d]);

   for ( c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++ )
         sum[c][d] = mat1[c][d]+ mat2[c][d];

   printf("Sum of entered matrices:-\n");

   for ( c = 0 ; c < m ; c++ )
   {
      for ( d = 0 ; d < n ; d++ )
         printf("%d\t",sum[c][d]);

      printf("\n");
   }
   return 0;
}

largest element of an array


/*A program to find largest number in an array*/

#include<stdio.h>

int main()
{
    int array[10], large, c, location = 1;
    printf("Enter 10 integers\n");

    for ( c = 0 ; c < 10 ; c++ )
        scanf("%d", &array[c]);

    large = array[0];

    for ( c = 1 ; c < 10 ; c++ )
    {
        if ( array[c] > large )
        {
           large = array[c];
           location = c+1;
        }
    }

    printf("Largest number is %d at location number %d.\n",  large,location);
    return 0;
}

Saturday, January 7, 2012

sorting in ascending order


/*A program to sort integers in ascending order */

#include<stdio.h>

int main()
{
   int array[100], n, c, d, temp, k;

   printf("Enter number of elements\n");
   scanf("%d", &n);

   printf("Enter %d integers\n", n);

   for ( c = 0 ; c < n ; c++ )
       scanf("%d", &array[c]);

   for ( c = 1 ; c < n ; c++ )
   {
       for ( d = 0 ; d < c  ; d++ )
       {
           if ( array[c] < array[d] )
           {
              temp = array[d];
              array[d] = array[c];

     for ( k = c ; k > d ; k-- )
        array[k] = array[k-1];

     array[k+1] = temp;
           }
       }
   }

   printf("Sorted list in ascending order:\n");

   for ( c = 0 ; c < n ; c++ )
       printf("%d\n", array[c]);

   return 0;
}

Palindrome string


/*A program to check whether a string ia a palindrome or not*/

#include <stdio.h>

int main()
{
   char text[100];
   int begin, middle, end, length = 0;

   gets(text);

   while ( text[length] != '\0' )
      length++;

   end = length - 1;
   middle = length/2;

   for( begin = 0 ; begin < middle ; begin++ )
   {
      if ( text[begin] != text[end] )
      {
         printf("is not a palindrome.\n");
         break;
      }
      end--;
   }
   if( begin == middle )
      printf("is a Palindrome.\n");

   return 0;
}

Palindrome Number


/*A program to check whether given number is a palindrome or not */

#include<stdio.h>

int main()
{
   int n, reverse = 0, temp;

   printf("Enter a number to check whether it is a palindrome or not:\n");
   scanf("%d",&n);
   temp = n;

   while( temp != 0 )
   {
      reverse = reverse * 10;
      reverse = reverse + temp%10;
      temp = temp/10;
   }

   if ( n == reverse )
      printf("\n%d is a palindrome number.\n", n);
   else
      printf("\n%d is not a palindrome number.\n", n);

   return 0;
}

Thursday, December 29, 2011

Pyramid of numbers

/* A program to print pyramid of numbers */

#include <stdio.h>
int main()
{
    int n, c, k, start = 1, temp;
    printf("Enter number of rows:");
    scanf("%d",&n);
    temp = n;
    for ( c = 1; c <= n; c++ )
    {
        for ( k = temp; k > 1; k-- )
            printf(" ");
        temp--;
        for ( k = 1; k <= 2*c - 1; k++ )
        {
            if ( k <= c)
            {
                 printf("%d", start);
                 if ( k < c )
                 start++;
            }
            else
            {
                start--;
                printf("%d", start);
            }
        }

        start = 1;
        printf("\n");
    }

    return 0;
}


Print Fibonacci series


/* A program to Print Fibonacci Series */

#include<stdio.h>
int main()
{
   int n, term_1 = 0, term_2 = 1, next_term, c;
   printf("Enter the number of terms in Fibonacci series:");
   scanf("%d",&n);
   printf("\nFirst %d terms of fibonacci series are:\n",n);
   for ( c = 0 ; c < n ; c++ )
   {
      if ( c <= 1 )
         next_term = c;
      else
      {
         next_term = term_1 + term_2;
         term_1 = term_2;
         term_2 = next_term;
      }
      printf("%d\n",next_term);
   }
   return 0;
}


Thursday, December 22, 2011

Using Ternary Operator


/* A program to read a number and print -1 if it is negative, 0 if it is zero and 1 if it is positive */

#include<stdio.h>
int main()

{
int a,b;

printf("\n The Program will print :\n 1 if input number is +ve\n 0 if input number is 0\n -1 if input number is -ve\n");
printf("Enter a number:\n");
scanf("%d",&a);

b=a>0?1:(a<0?-1:0);
printf("\nResult = %d",b);

return 0;

}

Circle and a point


/*A program to check whether a given point lies inside the circle, on  the circle or outside the circle */

#include <stdio.h>
#include <math.h>

int main()

{
    int h,k,x,y,r;
    float a;
    printf("enter center (h,k) of a circle\n");
    scanf("%d%d",&h,&k);
    printf("\nenter radius\n");
    scanf("%d",&r);
    printf("\nenter a point to be checked\n");
    scanf("%d%d",&x,&y);
    a=sqrt(pow(x-h,2)+pow(y-k,2));
    if (a<r)
        printf("\npoint (%d, %d) lies inside the circle",x,y);
    else if (a==r)
        printf("\npoint (%d, %d) lies on the circle",x,y);
    else
        printf("\npoint (%d, %d) lies outside the circle",x,y);
    printf(" having centre (%d,%d) and radius %d\n",h,k,r);
    return 0;

}

Monday, December 19, 2011

Pass/fail check


/* A program to check whether a student is pass or fail */

#include <stdio.h>
int main()

{
    int thermo, drawing, chemistry, math, c,total;
    printf("\n Marks in:\n\n");
    printf(" thermo    = ");
    scanf("%d",&thermo);
    printf(" drawing   = ");
    scanf("%d",&drawing);
    printf(" chemistry = ");
    scanf("%d",&chemistry);
    printf(" math      = ");
    scanf("%d",&math);
    printf(" c         = ");
    scanf("%d",&c);
    total=math+thermo+chemistry+drawing+c;
    if(thermo<40 || chemistry<40 || drawing<40 || math<40 || c<40)
        printf("\n Result: FAIL");
    else
        printf("\n Result: PASS");
    printf("\n Percentage:%0.2f\n",total/5.0);
    printf("\n NOTE: Total for each subject is 100\n");
    return 0;
}

Sunday, December 18, 2011

Reversing a number


/*A program to print a given integer in reverse order and sum it with original */

#include<stdio.h>

int main()  /* use 'long' instead of 'int' if integer is a long integer */
{
    int n, reverse = 0,temp;
    printf("Enter a number to reverse\n");
    scanf("%d",&n);
    temp=n;
    while( n != 0 )
   {
      reverse = reverse * 10;
      reverse = reverse + n%10;
      n = n/10;
   }

    printf("Reverse of entered number is %d\n", reverse);
    printf("sum of original number %d and reversed number %d is %d\n",temp,reverse,temp+reverse);
    return 0;
}

Find smallest number


/* C Program to Find Smallest Number out of 4 Number Using Array */

#include <stdio.h>
#include <conio.h>
int main()
{
int a[4],i,low;
printf("Enter 4 Numbers\n\n");
for(i=0;i<4;i++)
scanf("%d",&a[i]);
low=a[0];
for(i=0;i<4;i++)
{
if(a[i]<low)
low=a[i];
}
printf("\nSmallest Number is=%d ",low);
getch();
return 0;
}

Check straight line condition


/* A program to check whether given three points fall on one straight line or not */


#include<stdio.h>


int main()

{
    int x1, x2, x3, y1, y2, y3 ;

printf("\nEnter the Co-ordinates of first point(x1,y1)");
scanf("%d%d",&x1, &y1);

printf("\nEnter the Co-ordinates of 2nd point(x2,y2)");
scanf("%d%d",&x2, &y2);

printf("\nEnter the Co-ordinates of 3rd point(x3,y3)");
scanf("%d%d",&x3, &y3);

if( (y2-y1)/(x2-x1)==(y3-y2)/(x3-x2) )
printf("The three points lie on straight line");

else
printf("The three points do not lie on straight line");

return 0;

}