Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

พิมพ์ n ตัวเลข ที่ผลรวมเป็นกำลังสองสมบูรณ์

ได้รับ n ตัวเลข ต้องหา n ตัวเลขที่ผลรวมเป็นกำลังสองสมบูรณ์ (Perfect Square)

Input: 5
Output: 1 3 5 7 9
1+3+5+7+9 = 25 = 5²

อัลกอริทึม

START
Step 1: Declare a Macro for size (e.g. 5) and i = 1
Step 2: loop While i <= SIZE
    Step 2.1 -> printing (2*i)-1
    Step 2.2 -> incrementing i by 1
Step 3 -> End loop While
STOP

ตัวอย่างโค้ด (C)

#include <stdio.h>
#define SIZE 5

int main() {
    int i = 1;
    while (i <= SIZE) {
        printf("\n %d", ((2*i)-1));
        i++;
    }
}

ผลลัพธ์

1 3 5 7 9