使用 C 编程中的 for 循环生成 1 到 n 之间的偶数平方
偶数的平方是 - 22, 42, 62, 82,………
= 4, 16, 36, 64, 100, ………
算法
START Step 1: declare two variables a and n Step 2: read number n at runtime Step 3: use for loop to print square numbers For a=2; a*a<=n;a+=2 until the condition satisfy loop will continue and Print a*a STOP
程序 1
#include<stdio.h> int main(){ int a,n; printf("enter a number for n:"); scanf("%d",&n); for(a=2;a*a<=n;a+=2) //print even squares that are present in between 1 and n{ printf("%d
",a*a); } return 0; }
输出
enter a number for n:200 4 16 36 64 100 144 196
程序 2
以下是在 1 到 n 之间查找偶数的程序 -
#include<stdio.h> int main(){ int a,n; printf("enter a number for n:"); scanf("%d",&n); for(a=2;a*a*a<=n;a+=2){ printf("%d
",a*a*a); } return 0; }
输出
enter a number for n:300 8 64 216
广告