@moreme
You have a few problems.
1. You declared your function as int, but returning a char. Same with the variable, result
2. Your for loop in the function.
for(int i=1; sqr>=n; i++)
sqr should be an i. And it's <=, not >=
3. The checking in your program, should be checking the int value, not a char value
4. Your going through the entire number checking in the for loop, before you check it sqr was equal. ( Assuming that the loop was correct to begin with)
You should check after each multiplication, for equals.
Here's the program, corrected.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
// Perfect Square.cpp : main project file #include<iostream>
#include <iostream>
using namespace std;
int perfect_square(int);
int main()
{
int result;
int num=0;
cout<<"enter a number: ";
cin>>num;
result = perfect_square(num);
if (result==1)
{
cout<<"perfect square"<<endl;
}
else
{
cout<<"not perfect square"<<endl;
}
}
int perfect_square(int n)
{
int sqr=1;
int counter=1;
for(int i=1; i<=n; i++)
{
sqr = i*i;
if(sqr==n)
return 1;
}
return 2;
}
|