Home > Backend Development > C++ > body text

Print n numbers such that their sum is a perfect square

PHPz
Release: 2023-09-09 21:29:02
forward
1542 people have browsed it

Print n numbers such that their sum is a perfect square

Given n numbers, the program must find that the sum of these n numbers is a perfect square number

Input : 5
Output : 1 3 5 7 9
1+3+5+7+9=25 i.e (5)^2
Copy after login

Algorithm

START
   Step 1 : Declare a Macro for size let’s say of 5 and i to 1
   Step 2: loop While till i<=SIZE
      Step 2.1 -> printing (2*i)-1 Step
      Step 2.2 -> incrementing i with 1 Step
   Step3-> End loop While
STOP
Copy after login

Example## The Chinese translation of # is:

Example

#include <stdio.h>
# define SIZE 5
int main() {
   int i=1;
   while(i<=SIZE) {
      printf("</p><p> %d",((2*i)-1)); i++;
   }
}
Copy after login

Output

If we run the above program, it will generate the following output

1
3
5
7
9
Copy after login

The above is the detailed content of Print n numbers such that their sum is a perfect square. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!