Answer to Question #288523 in C++ for Prajwal

Question #288523

Given an integer N, count the numbers having an odd number of factors from 1 to N (inclusive).

 

Example 1:

Input:
N = 5
Output:
2
Explanation:
From 1 - 5 only 2 numbers,
1 and 4 are having odd number
of factors.
1
Expert's answer
2022-01-18T14:07:13-0500
using namespace std;
int main()
{
    int N=5, res = 0, div=0;


    for (int i = 1; i <= N; ++i) 
	{
        div = 0;
        for (int j = 1; j <= i; ++j) 
		{
            if (i % j == 0) div++;
        }
        if (div % 2) res++;
    }
    cout <<"\n\tNo. of factors = "<<res;
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment