Use C++ to write code to find the Nth non-square number
We all know numbers that are not the square of any number, such as 2, 3, 5, 7, 8, etc. There are N non-square numbers and it is impossible to know every number. So, in this article, we will explain everything about squareless or non-square numbers and ways to find the Nth non-square number in C.
Nth non-square number
If a number is the square of an integer, the number is called a perfect square. Some examples of perfect square numbers are -
1 is square of 1 4 is square of 2 9 is square of 3 16 is square of 4 25 is square of 5
If a number is not the square of any integer, then the number is called non-square. For example, the first 15 non-square numbers are -
2, 3, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 17, 18, 19
How to find the Nth non-square number?
Here is an example of finding the Nth non-square number -
Input : 2 Output : 3 Explanation : 2nd Non square number is 3 (after 2 which is first non square number) Input : 5 Output : 7 Explanation : 7th Non square number is 7 ( after 2,3,5,6 which are first four non square
After looking at the above example, we can come up with a solution: In order to find the Nth non-square number, we need Start counting from the nth number and check if each integer is a perfect square and don't count
Create a C program to find the Nth non-square number
We created a Complete syntax for finding the Nth non-square number in C.
Example
#include <bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; // Taking input from the user. int i = 2; // as 0 and 1 are squares of themselves so we start calculating from 2. int cnt = 0; // declaring counter variable; while(cnt != n){// the loop will terminate when out counter will have the same value as n. int a = sqrt(i); if(i != a*a) cnt++; if(cnt != n) i++; } cout << i << "\n"; // printing the nth non square number. }
Output
5
(When we provide 3 as input, we get 5 as output)
Let’s do the above code A brief description.
Step 1 - Get user input and set count to 0.
cin >> n; // Taking input from the user. int i = 2; // as 0 and 1 are squares of themselves so we start calculating from 2. int cnt = 0; // declaring counter variable;
Step 2 - Count non-square numbers and skip square numbers.
while(cnt != n) // the loop will terminate when out counter will have the same value as n.{ int a = sqrt(i); // finding square root using sqrt() function. if(i != a*a) // check whether the number is a perfect square or not. cnt++; // incrementing counter if found non perfect number. if(cnt != n) i++; }
Step 3 - Print the Nth square number.
cout << i << "\n"; // printing the nth non square number.
Conclusion
In this article, we explained non-square numbers and ways to find the Nth non-square number in C. Apart from C, we can also use this program in different programming languages like Java, Python, C or any other language. We hope this article was helpful and informative for you as we have described everything in the simplest way possible.
The above is the detailed content of Use C++ to write code to find the Nth non-square number. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Can you enter squares and cubes in Excel? Here are several quick input methods for you to choose from. 1. Input method input Nowadays, input methods are very smart. When you type "square" or "cube", [²] and [m³] will automatically appear in the options. The input method demonstrated here is a certain dog, and the specific one is not specified. Chinese input method. You can test your own input method. 2. Set the superscript input. First enter [m3] or [m2] in the cell, then select the number, right-click the mouse and select cell format settings, then set 3 and 2 as [superscript], and then confirm. It will be displayed as cube or square; 3. Alt + small keyboard input. Hold down Alt in the input cell and don’t release it, then press the number on the small keyboard.

Standby is a lock screen mode that activates when the iPhone is plugged into the charger and oriented in horizontal (or landscape) orientation. It consists of three different screens, one of which is displayed full screen time. Read on to learn how to change the style of your clock. StandBy's third screen displays times and dates in various themes that you can swipe vertically. Some themes also display additional information, such as temperature or next alarm. If you hold down any clock, you can switch between different themes, including Digital, Analog, World, Solar, and Floating. Float displays the time in large bubble numbers in customizable colors, Solar has a more standard font with a sun flare design in different colors, and World displays the world by highlighting

Representing numbers as output is an interesting and important task when writing a program in any language. For integer types (data of type short, long, or medium), it is easy to represent numbers as output. For floating point numbers (float or double type), sometimes we need to round them to a specific number of decimal places. For example, if we want to represent 52.24568 as three decimal places, some preprocessing is required. In this article, we will introduce several techniques to represent floating point numbers to a specific number of decimal places by rounding. Among the different approaches, it is important to use a C-like format string, use the precision argument, and use the round() function from the math library. Let’s look at them one by one. with

The ability to generate random numbers or alphanumeric strings comes in handy in many situations. You can use it to spawn enemies or food at different locations in the game. You can also use it to suggest random passwords to users or create filenames to save files. I wrote a tutorial on how to generate random alphanumeric strings in PHP. I said at the beginning of this post that few events are truly random, and the same applies to random number or string generation. In this tutorial, I'll show you how to generate a pseudo-random alphanumeric string in JavaScript. Generating Random Numbers in JavaScript Let’s start by generating random numbers. The first method that comes to mind is Math.random(), which returns a float

The average of the squares of natural numbers is calculated by adding all the squares of n natural numbers and then dividing by that number. The first two natural numbers in the example are 2.5, 12+22=5=>5/2=2.5. There are two methods of calculation in programming - Using loops Using formulas Calculating the average of squares of natural numbers using loops This logic works by finding the squares of all natural numbers. Find the square of each by looping from 1 to n and add to the sum variable. Then divide that sum by n. Program to calculate the sum of squares of natural numbers - sample code real-time demonstration #include<stdio.h>intmain(){ intn=2;

When we use Excel software to create tables, sometimes we need to use the square symbol. So how do we enter the square symbol? Now I will share with you how to enter the square symbol in Excel, which is what many friends want to know recently. How to enter the square symbol in Excel. I hope it can help you! We want to enter the square in an Excel file, first open the Excel file. I will use square meters as an example to demonstrate. First, enter m2 in the Excel table. Then use the mouse to select the number 2 in m2, then right-click and you will see a [Format Cells] function. At this time, select the [Format Cells] button. At this time, the [Format Cells] dialog box will pop up in the Excel file, and then find

In this article, we will learn about the reversal algorithm to rotate a given array to the right by k elements, for example −Input:arr[]={4,6,2,6,43,7,3,7},k= 4Output:{43,7,3,7,4,6,2,6}Explanation:Rotatingeachelementofarrayby4-elementtotherightgives{43,7,3,7,4,6,2,6}.Input:arr[]={8 ,5,8,2,1,4,9,3},k=3Output:{4,9,3,8,5,8,2,1} Find the solution

In this article, we will discuss the problem of finding numbers between 1 and n (given) that are not divisible by any number between 2 and 10. Let us understand this with some examples - Input:num=14Output:3Explanation:Therearethreenumbers,1,11,and13,whicharenotdivisible.Input:num=21Output:5Explanation:Therearefivenumbers1,11,13,17,and19,whicharenotdivisible. Solved Simple method if
