Small Help Please🥺 Function Oriented in C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Small Help Please🥺 Function Oriented in C++

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

17th May 2022, 8:29 AM
raf
raf - avatar
12 Answers
0
#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; }
17th May 2022, 9:09 AM
Bob_Li
Bob_Li - avatar
+ 1
Thank you sir.
17th May 2022, 8:43 AM
raf
raf - avatar
+ 1
Thank you so much for your advice sir I'm still learning this functions on c++ ...thank you for helping me appreciate a lot🙏❤️
17th May 2022, 9:16 AM
raf
raf - avatar
+ 1
use void instead of float when creating a function as there are no return value.
18th May 2022, 10:32 AM
sahil somyani
sahil somyani - avatar
+ 1
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
19th May 2022, 2:25 AM
Numan
Numan - avatar
0
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..
17th May 2022, 8:42 AM
Jayakrishna 🇮🇳
0
Thank you sir Bob_Li🙏
17th May 2022, 9:03 AM
raf
raf - avatar
0
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.
17th May 2022, 9:12 AM
Bob_Li
Bob_Li - avatar
0
Which compiler you are using c Or cpp?
19th May 2022, 2:22 AM
Numan
Numan - avatar
0
im using cpp
19th May 2022, 2:23 AM
raf
raf - avatar
0
thank you sir appreciate a lot
19th May 2022, 2:27 AM
raf
raf - avatar
0
Most welcome
19th May 2022, 2:27 AM
Numan
Numan - avatar