12. C PROGRAMMING LAB | Check Now

C PROGRAMMING LAB –12] Develop a program to find the square root of a given number N and execute for all possible inputs with appropriate messages.


C PROGRAMMING -Algorithm

  • Step-1: Start
  • Step-2: Read the number n
  • Step-3: Check for the number if less than zero. If it is true than goto step-4 other wise goto step-5
  • Step-4: Display the error than goto step-7
  • Step-5: In the while loop check (sqrt!=temp) if true execute, otherwise goto step-6
    • Temp = sqrt
    • Sqrt = (n/temp+temp) / 2
  • Step-6: Display the square root of n
  • Step-7: Stop

Flow Chart

C PROGRAMMING-find the square root of a given number N

Program -12 source code

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
int n;
 float sqrt, temp;
printf("Enter a value whose square root has to be calculated\n");
scanf("%d",&n);
if(n<=0)
{ 
 printf("Sqrt can’t be determined");
 exit(0);
}
 sqrt = n / 2;
 temp = 0;
 while(sqrt != temp)
 { 
 temp = sqrt;
 sqrt = ( n/temp + temp) / 2;
 }
 printf("The square root of %d is %f”, n, sqrt);
}

Output

Enter a value whose square root is to be calculated

4

The Square root of 4.0000 = 2.0000

C PROGRAMMING -Viva Questions

1] Difference between float and double data types

2] Write the syntax of for loop?

3] What is the use of the break statement?

Leave a Reply

Your email address will not be published. Required fields are marked *