About Me

My photo
Raipur, Chhattisgarh, India
Hi , I am Amit Thakur. I have worked as a QA Engineer for two years and as a Java Developer for one year in NIHILENT TECHNOLOGIES PVT. LTD., Pune.Currently I am working as DEAN (Research & Development) in Bhilai Institute of Technology, Raipur.

Thursday, April 17, 2014

Find the Roots of equation by Newton Raphson Method via C Programming

Newton's Method is mainly used for finding the roots of a polynomial. This method is also known as Newton Raphson method in the numerical analysis. We start with our approximated root of a function in this method which further computes the better approximation which is somewhere near the actual root.
Newton Raphson Method is also known as Method of tangents. Let us now demonstrate how to use this program:

Consider an example : f(x)=9x^3-9 . we need to find root of this polynomial and we assume its root to be 5. Now, max power of x will be 3 , x^0 = -9 , x^1 = 0 , x^2 = 0, x^3 = 9 and first approximation = 5 . After entering such details according to our polynomial , Newton Raphson C Program will display the root of polynomial and iterations as the output.

#include
#include
#include
#include
int maxpow,i=0,cnt=0,flag=0;
int coef[10]={0};
float x1=0,x2=0,t=0;
float fx1=0,fdx1=0;
int main()
{
    printf("\n NEWTON RAPHSON METHOD");
    printf("\nENTER THE MAXIMUM POWER OF X = ");
    scanf("%d",&maxpow);
    for(i=0;i<=maxpow;i++)
    {
        printf("\n x^%d = ",i);
        scanf("%d",&coef[i]);
    }
    printf("\n");
    printf("\nYour polynomial is  = ");
    for(i=maxpow;i>=0;i--)
    {
        printf(" %dx^%d",coef[i],i);
    }
    printf("\nFirst approximation x1 ----> ");
    scanf("%f",&x1);
     printf("\n\n---------------------------------------\n");
     printf("\n Iteration \t x1 \t F(x1) \t \tF'(x1)  ");
     printf("\n------------------------------------------\n");
    do
    {
            cnt++;
            fx1=fdx1=0;
            for(i=maxpow;i>=1;i--)
            {
                fx1+=coef[i] * (pow(x1,i)) ;
            }
            fx1+=coef[0];
            for(i=maxpow;i>=0;i--)
            {
                fdx1+=coef[i]* (i*pow(x1,(i-1)));
            }
            t=x2;
            x2=(x1-(fx1/fdx1));
            x1=x2;
            printf("\n\t %d \t%.3f \t %.3f\t\t%.3f ",cnt,x2,fx1,fdx1);
    }while((fabs(t - x1))>=0.0001);
    printf("\n THE ROOT OF EQUATION IS = %f",x2);
    getch();
}

Wednesday, September 4, 2013

C Program to Create Your Own Header File in C Programming

Make Your Own Header File ?
Step1 : Type this Code 
int add(int a,int b)
{
return(a+b);
}
  • In this Code write only function definition as you write in General C Program
Step 2 : Save Code
  • Save Above Code with [.h ] Extension .
  • Let name of our header file be myhead [ myhead.h ]
  • Compile Code if required.
Step 3 : Write Main Program
#include
#include"myhead.h"
void main()
{
int number1=10,number2=10,number3;
number3 = add(number1,number2);
printf("Addition of Two numbers : %d",number3);
}
  1. Include Our New Header File .
  2. Instead of writing < myhead.h> use this terminology “myhead.h”
  3. All the Functions defined in the myhead.h header file are now ready for use .
  4. Directly  call function add(); [ Provide proper parameter and take care of return type ]
Note
  1. While running your program precaution to be taken : Both files [ myhead.h and sample.c ] should be in same folder.

C Program to Swap two numbers using XOR Operator

Generally Swaping two number requires three variables , Let’s Take look atProcedure of swaping two Number
For Swaping Two numbers following procedure is used -
x = x ^ y --> x^=y -- (1)
y = y ^ x --> y^=x -- (2)
x = x ^ y --> x^=y -- (3)
Now we will Explaining above three statements using example ….
Let x = 12 and y = 9 [ For our sake and simplicity consider number is of 4 bits ]
x = 1100
y = 1001

X-OR Table :
  A   B  A X-OR B
110
101
011
000
Step 1 : After : x = x ^ y
x   = 1100
y   = 1001
----------
x^y = 0101
----------
x   = 0101    ..... New Value of x
Step 2 : After y = y ^ x
x   = 0101    ..... New Value is taken
y   = 1001    ..... Old Value of Y
----------
y^x = 1100
----------
y   = 1100    ..... New Value of y = Initial x
Step 3 : After x = x ^ y
x   = 0101    ..... New Value from step 1
y   = 1100    ..... New Value of y from Step 2
----------
y^x = 1001
----------
x   = 1001    ..... New Value of x = Initial y
Here is Program for : [Swap / Interchange two variables [numbers] without using Third Variable]
#include
#include
void main()
{
int num1,num2;

printf("\nEnter First Number : ");
scanf("%d",&num1);

printf("\nEnter Second Number : ");
scanf("%d",&num2);

num1 = num1 ^ num2;
num2 = num1 ^ num2;
num1 = num1 ^ num2;

printf("\nNumbers after Exchange : ");
printf("num1 = %d and num2 = %d",num1,num2);

getch();
}
Output :
Enter First Number : 20
Enter Second Number : 40

Numbers after Exchange : num1 = 40 and num2 = 20

C Program to Demonstrate Nested Printf Statements

nested Printf statements : Example 1

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("%d",printf("abcdefghijklmnopqrstuvwxyz"));
getch();
}

Output :
abcdefghijklmnopqrstuvwxyz26

How ?
  1. “abcdefghijklmnopqrstuvwxyz” will be first Printed while executing inner printf
  2. Total Length of the “abcdefghijklmnopqrstuvwxyz” is 26
  3. So printf will return total length of string
  4. It returns 26 to outer printf
  5. This outer printf will print 26

C Program to Demonstrate Printf inside Another Printf Statement

Printf inside printf in C : Example 1

#include
#include
void main()
{
int num=1342;
clrscr();
printf("%d",printf("%d",printf("%d",num)));
getch();
}

Output :
134241

How ?
  1. Firstly Inner printf is executed which results in printing 1324 
  2. This Printf Returns total number of Digits i.e  4 and second inner printf will looks like
  3. printf("%d",printf("%d",4));
  4. It prints 4 and Returns the total number of digits i.e 1 (4 is single digit number )
  5. printf("%d",1);
  6. It prints simply 1 and output will looks like 132441

Rule :
Inner printf returns Length of string printed on screen to the outer printf

C Program to Print Hello word without using semicolon

Part 1 : Printf Hello word in C without using semicolon [only ones ]

#include
void main()
{
   if(printf("Hello"))
   {
   }
}

Output :
Hello

Part 2 : Printf Hello word in C without using semicolon [infinite times]

#include
void main()
{
   while(printf("Hello"))
   {
   }
}

Part 3 : Printf Hello [Using Switch]

#include
void main()
{
   switch(printf("Hello"))
   {
   }
}

Part 4 : Using Else-if Ladder

#include
void main()
{
   if(printf(""))
      {
      }
   else if (printf("Hello"))
      {
      }
   else
      {
      }
}

Part 5 : Printf Hello [Using While and Not]

#include
void main()
{
    while(!printf("Hello"))
    {
    }
}

Part 6 : Using #define

#include
#define PRINT printf("Hello")
void main()
{
    if(PRINT)
    {
    }
}

C Program to Accept Paragraph using scanf

Accept Paragraph using scanf in C
#include
void main()
{
char para[100];
printf("Enter Paragraph : ");
scanf("%[^t]",para);
printf("%s",para);
}

Output :[Press Tab to Stop Accepting Characters ]
Enter Paragraph : C Programming is very easy to understand
C
Language
is backbone of
C++
Language

How ?
scanf("%[^t]",para);
  1. Here scanf will accept Characters entered with spaces.
  2. It also accepts the Words , new line characters .
  3. [^t]  represent that all characters are accepted except tab(t) ,whenever t will encountered then the process of accepting characters will be terminated.
Drawbacks :
  1. Paragraph Size cannot be estimated at Compile Time
  2. It’s vulnerable to buffer overflows.
How to Specify Maximum Size to Avoid Overflow ?
//------------------------------------
// Accepts only 100 Characters
//------------------------------------
scanf("%100[^t]",para);