-->

Facebook

Jasper Roberts - Blog

Monday, March 2, 2015

A C Program to represent Secant Method.

#include<stdio.h>
#include<conio.h>
double f(double x)
{
return (2*x*x*x-3*x*x+5*x-6);
};
void main()
{
int iter,ctr=1;
double x0,x1,r;
clrscr();
printf("\nEnter the first approximate value : ");
scanf("%lf",&x0);
printf("\nEnter the second approximate value : ");
scanf("%lf",&x1);
printf("\nEnter the number of itteration : ");
scanf("%d",&iter);
while(ctr<=iter)
{
r=((x0*f(x1))-(x1*f(x0)))/(f(x1)-f(x0));
printf("\nAfter %d itteration root is = %lf",ctr,r);
x0=x1;
x1=r;
ctr++;
};
printf("\n\nThe approximate root is = %lf",r);
getch();
}


RELATED TOPICS
A C Program to represent N-R Method.
A C Program to represent the Bisection Method.

A C Program to represent N-R Method.

#include<conio.h>
#include<stdio.h>
double f(double x)
{
return (x*x*x+2*x*x+10*x-20);
};
double df(double x)
{
return (3*x*x+4*x+10);
};
void main()
{
int iter,ctr=1;
double x0,x1,l1,l2,r,x2;
clrscr();
printf("\nEnter the first approximate value : ");
scanf("%lf",&x0);
printf("\nEnter the second approximate value : ");
scanf("%lf",&x1);
printf("\nEnter the number of itteration : ");
scanf("%d",&iter);
x2=((x0+x1)/2);
while(ctr<=iter)
{
r=x2-(f(x2)/df(x2));
printf("\nAfter %d itteration root is = %.4lf",ctr,r);
x2=r;
ctr++;
}
printf("\n\nThe approximate root is = %.4lf",r);
getch();
}
RELATED TOPICS
A C Program to represent the Bisection Method.
A C Program to represent Secant Method.

A C Program to represent the Bisection Method.

#include<stdio.h>
#include<conio.h>
double F(double x)
{
return(x*x*x-x-1);
};
void main()
{
int iter,ctr=1;
double x0,x1,l1,l2,r;
clrscr();
printf("\nEquation is :\n\n\tx^3-x-1");
printf("\nEnter the first approximate value : ");
scanf("%lf",&x0);
printf("\nEnter the second approximate value : ");
scanf("%lf",&x1);
printf("\nEnter the number of itteration : ");
scanf("%d",&iter);
while(ctr<=iter)
{
r=(x0+x1)/2;
printf("\nAfter %d  itteration root is : %lf",ctr,r);
l1=F(r);
printf("\t%lf",l1);
l2=F(x0);
if(l1*l2<0)
x1=r;
else
x0=r;
ctr++;
}
printf("\n\nThe approximate root is : %lf",r);
getch();
}
RELATED TOPICS
A C Program to represent N-R Method.
A C Program to represent Secant Method.

if...else if ladder(multiple if else)

- In c language, if else statement provides two way decision only.

- In some case, we need to check multiple choice.

- At that time, we can use if...else if ladder(multiple if else statement).

- if...else if ladder provides multi-way decision. There may be more than one condition in Program at that time use if..else if ladder.

syntax:
     if(condition-1)
          statement-1;

     else if(condition-2)
          statement-2;
     else if(condition-3)
          statement-3;
          .
          .
          .
     else if(condition-n)
          statement-n;
     else
          statement;

- In if..else if ladder, the condition-1 is true than it will execute the statement-1.

- If the condition-2 is true than it will execute the statement-2. Same way it will check
All the condition.

Flow Chart:
multiple if else statement
multiple if else


Example: A c program to check the given year is leap year or not.

#include<stdio.h>
#include<conio.h>

void main()
{
  int year;

  printf("Enter a year:");
  scanf("%d", &year);

  if(year%4 == 0)
      printf("\n This is a leap year.", year);
  else if (year%400 == 0)
      printf("\n This is a leap year.", year);

  else if(year%100 == 0)
      printf("\n This is not a leap year.", year);
  else
      printf("\n This is not a leap year.", year); 

  getch();
}

OUTPUT:
Enter a year: 2011
This is not a leap year.


if...else statement in C language.

 - In C programming language, c provides if...else statement to take two-way decision.
 - If the condition is true than it will execute the statement(s)-1, if the condition is false than it will execute the    statement(s)-2.
 - In C, Simple if statement provides only one way decision but if...else statement provides two-way                 decision.
 - If the statements are more than one than it will be written in { and } braces.

Syntax:
            if(condition)
                      statement(s)-1;
            else
                      statement(s)-2;

Example:
#include<conio.h>
#include<stdio.h>

Simple if statement.

-In C programming language, c provides if statement to take one way decision.

-If the condition is true than it will execute the statement(s) otherwise it will not execute the statement(s).

Syntax:
          if(condition)
                 statement(s);
if statement theory
simple if statement