+ 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
12 odpowiedzi
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;
}
+ 1
Thank you sir.
+ 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🙏❤️
+ 1
use void instead of float when creating a function
as there are no return value.
+ 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
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..
0
Thank you sir Bob_Li🙏
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.
0
Which compiler you are using c Or cpp?
0
im using cpp
0
thank you sir appreciate a lot
0
Most welcome