A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

Write a C++ program to check whether a given positive integer is a perfect square or not
Q:

Write a C++ program to check whether a given positive integer is a perfect square or not

0

Write a C++ program to check whether a given positive integer is a perfect square or not

Sample Input: n = 1
Is 1 is perfect number? 1
Sample Input: n = 13
Is 13 is perfect number? 0

Sample Output:

Is 1 is perfect number? 1

Is 13 is perfect number? 0

Is 16 is perfect number? 1

Is 125 is perfect number? 0

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <iostream>
#include <cmath>

using namespace std;

bool is_Perfect_Square(int num) {
        long long start_num = 0;
        long long end_num = num;
        
        while(start_num + 1 < end_num)
        {
            long long mid_num = start_num + (end_num - start_num) / 2;
            if (mid_num * mid_num < num)
            {
                start_num = mid_num;
            }
            else if(mid_num * mid_num > num)
            {
                end_num = mid_num;
            }
            else
            {
                return true;
            }
        }
        
        return start_num * start_num == num || end_num * end_num == num;
    }
    
int main() 
{
    int n = 1;
    cout << "\nIs "<< n << " is perfect number? " << is_Perfect_Square(n) << endl;   
    n = 13;
    cout << "\nIs "<< n << " is perfect number? " << is_Perfect_Square(n) << endl; 
    n = 16;
    cout << "\nIs "<< n << " is perfect number? " << is_Perfect_Square(n) << endl; 
    n = 125;
    cout << "\nIs "<< n << " is perfect number? " << is_Perfect_Square(n) << endl;    
	return 0;    
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now