C Language Program - Newton Raphson's Method : To find Square Root
#include #include #include float f(float x,int n) { return(x*x-n); } float df(float x) { return 2*x; } void main() { float b=5,m,e=0.0001,num; int i; clrscr(); printf("\n Newton Raphson's Method : To find Square Root"); printf("\n Enter Number whose Square root to be found :- \n"); scanf("%f",&num); m=(b)-((f(b,num))/df(b)); i=1; while(fabs(f(m,num))>e) { b=m; m=(b)-((f(b,num))/df(b)); i=i+1; printf("\n Square root of %.2f is %.2f",num,m); getch(); } }
|
Author: Shivshanker Cheral | Member Level: Bronze | Revenue Score:      |
#include< stdio.h > #include< conio.h > #include< math.h >
float f(float x,int n) { return(x*x-n); }
float df(float x) { return 2*x; }
void main() { float b=5,m,e=0.0001,num; int i; clrscr(); printf("\n Newton Raphson's Method : To find Square Root");
printf("\n Enter Number whose Square root to be found :- \n"); scanf("%f",&num); m=(b)-((f(b,num))/df(b)); i=1; while(fabs(f(m,num))>e) { b=m; m=(b)-((f(b,num))/df(b)); i=i+1; printf("\n Square root of %.2f is %.2f",num,m); getch(); } }
OUtput
Newton Raphson's Method : To find Square Root Enter Number whose Square root to be found :- 7
Square root of 7.00 is 2.69 Square root of 7.00 is 2.65 Square root of 7.00 is 2.65
|