Find any root of a number

letmec 0 Tallied Votes 169 Views Share

This is a program to calculate the n-th root of any number.
This program uses the NEWTON_RAPTION_ITERATION method for calculation.
The general formula to find the n-th root of any number is :

*-------------------------------*
* (((n-1)/n)*x0) + (1/n)*(N/d) *
*-------------------------------*

where n =1,2,3............ (n-th root).
x0 is First approximate root.
N is The number whose n-th root is to be calculated.
and d = x0*x0*x0*...........n-1.

For Example you had to calculate the square root of 16, then
n=2 (square root)
N=16 (the number)

/*
  This is a program to calculate the n-th root of any number.
  This program uses the NEWTON_RAPTION_ITERATION  method for calculation.
  The general formula to find the n-th root of any number is :

	     *-------------------------------*
	     * (((n-1)/n)*x0) + (1/n)*(N/d)  *
	     *-------------------------------*

  where n =1,2,3............ (n-th root).
	x0 is First approximate root.
	N is The number whose n-th root is to be calculated.
  and  d = x0*x0*x0*...........n-1.

  For Example you had to calculate the square root of 16, then
  n=2 (square root)
  N=16 (the number)
*/

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define NOS_OF_ITERATIONS 40

double cal_first_approx_root(double,double);
double Find_n_Root(double,double,double);
double n_mul(double,double);

void main()
{
 double x0=1,n;
 double N;
 double root;
 //clrscr();
 //textcolor(11);
 xx: cprintf("\n\rEnter which root do want FOR EXAMPLE:squar-root(2),cube-root(3).....etc ?");
 scanf("%lf",&n);
 if(n<=0)
 {
  fflush(stdin);
  printf("\n\nNumber should be Greater than 0...PRESS ANY KEY ENTER AGAIN");
  getch();
  goto xx;
 }
 yy:cprintf("\n\rEnter the number for which you want %0.0lf-Root:",n);
 scanf("%lf",&N);
 if(N<=0)
 {
  fflush(stdin);
  printf("\nNumber should be greater than 0....PRESS ANY KEY TO ENTER AGAIN");
  getch();
  goto yy;
 }

 x0 = cal_first_approx_root(n,N);

 printf("\n\nThe first assumed root is calculated as %lf\n",x0);
 root=Find_n_Root(N,n,x0);
 //textcolor(GREEN);
 printf("\n\n%0.0lf_Root of %0.0lf = ",n,N);
 cprintf("%lf",root);
 getch();
}

double cal_first_approx_root(double n,double N)
{
  int i,x=1;
  double j=1;
  while(1)
  {
  for(i=0;i<n;i=i+1)
  {
   x=x*j;
  }
  if(x>N)
  {
   return(j-1);
   //break;
  }
  j=j+1;
  x=1;
 }
}
double Find_n_Root(double NUM,double n,double X0)
{
 int i;
 double d=1.0;
 double firstterm,secondterm,root=X0;
 for(i=1;i<=NOS_OF_ITERATIONS;i++)
 {
  d=n_mul(root,n);
  firstterm=((n-1)/n)*root;
  secondterm=(1/n)*(NUM/d);
  root=firstterm+secondterm;
  printf("\n%lf\t%lf\t%lf",firstterm,secondterm,root);
 }
 return(root);
}

double n_mul(double x,double n)
{
 double d=1;
 int i;
 for(i=1;i<=n-1;i++)
 d=d*x;
 return(d);
}
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Moved this duplicate to here!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.