Kindly check if my code is correct with the logic of the problem to make my code better. thank you Problem: Write a function-oriented program that emulates the sqr() function. Display the square root values of input N. Thank you.🙏 heres my code: https://code.sololearn.com/cSlDTS9yW2G8/?ref=app
5/17/2022 8:29:45 AM
raf12 Answers
New Answer#include<stdio.h> #include<iostream> using namespace std; float sqr(int n){ float temp,sqt; sqt=n/2; temp=0; while(sqt!=temp) { temp=sqt; sqt=(n/temp+temp)/2; } return sqt; } int main() { int n; float res; printf("Enter a Number: "); cin >> n; res = sqr(n); printf("The square root of %d is %f",n,res); return 0; }
Thank you so much for your advice sir I'm still learning this functions on c++ ...thank you for helping me appreciate a lot🙏❤️
And you have used c functions like scanf, printf you are using cpp compiler so you should use cout(for print) and cin(for input) function for input output operation
Instead of printing result in function, return to calling statement. Like cout << sqrt(9) ; // will output 3 Similarly write for cout << sqr(9) ; So take input in main, and pass to sqr function and get result back.. Hope it helps..
raf Several mistakes. The sqr function have no return value. float sqr is expected to return a float. Do the input and output handling in the main function.
Sololearn Inc.
535 Mission Street, Suite 1591Send us a message