void main()
{
int i = 0;
int sum = 0;
while (i <= 100) {//求1到100累加和
sum = sum + i;
i++;
}printf("%d\n", sum);
}
void main() {
int i = 0;
int sum = 0;
while (i <= 100) {//求1到100偶数累加和
printf("%d\n", i);
sum = sum + i;
i += 2;
}printf("%d\n", sum);
}
void main() {
int i = 1;
int sum = 0;
while (i <= 100) {//求1到100奇数累加和
printf("%d\n", i);
sum = sum + i;
i += 2;
}printf("%d\n", sum);
}
void main()
{
int i = 1;
int sum = 0;
int sign = 1;
while (i <= 100) {//1-2+3-4+......-100
printf("%u\n", i);
sum = sum + sign * i;
i++;
sign = -sign;
}printf("%d\n", sum);
}
void main() {
int i = 1;
float sum = 0;
while (i <= 100) {//1+1/2+1/3+....+1/100
sum = sum + 1.0/i;
i++;
}printf("sum=%f\n", sum);
}
void main() {
char i = 'A';
while (i <= 'Z') {
printf("%c\n", i);
i++;
}
}
void main() {
int n, f, i;
printf("请输入n:");
scanf_s("%d", &n);
for (i = 1, f = 1; i <= n; i++)
f = f * i;
printf("%d!=%d\n", n, f);
}
void main(){
int x,d;
scanf_s("%d", &x);
printf("%d的倒序是:", x);
while (x != 0) {
d = x % 10;
x = x / 10;
printf("%d",d);
}
}
void main() {
int i, j;
for (i = 1; i <= 9; i++) {
for (j = 1; j <= i; j++) {
printf("%d*%d=%-4d", i, j, i * j);
}printf("\n");
}
}
void main() {
int i, j;
for (i = 1; i <= 4; i++) {
for (j = 1; j <= i; j++) {
printf("*");
}printf("\n");
}
}
void main() {
int i, j;
for (i = 1; i <= 4; i++) {
for (j = 1; j <= 5 - i; j++) {
printf("*");
}printf("\n");
}
}
void main() {
int i, j;
for (i = 1; i <= 4; i++) {
for (j = 1; j <= 2 * i - 1; j++) {
printf("*");
}printf("\n");
}
}
void main() {
int i, j;
for (i = 1; i <= 4; i++) {
for (j = 1; j <= 5- i; j++) {
printf(" ");
}
for (j = 1; j <= 2 * i - 1; j++) {
printf("*");
}printf("\n");
}
}
void main() {
int i, j;
for (i = 1; i <= 4; i++) {
for (j = 1; j <= 4 - i; j++) {
printf(" ");
}
for (j = 1; j <= 2 * i - 1; j++) {
printf("*");
}printf("\n");
}
for(i=3;i>=1;i--){
for (j = 1; j <= 4 - i; j++) {
printf(" ");
}
for (j = 1; j <= 2 * i - 1; j++) {
printf("*");
}printf("\n");
}
}
void main() {
int i = 0;
int sum = 0;
while (2) {
if (i > 100)break;
sum = sum + i;
i++;
}printf("%d", sum);
}
void main() {
char c;
while (1) {
c = getchar();
if (c >= 'a' && c <= 'z')
putchar(c - 32);
else
break;
}
}
void main() {
char c;
scanf_s("%c", &c);
while (1) {
if (c >= 'a' && c <= 'z') {
c = c - 32;
putchar(c);
}
else
break;
}
}
void main() {
int i;
for (i = 100; i <= 200; i++) {
if (i % 3 != 0)
printf("%4d", i);
}
}
void main() {
int i;
for (i = 100; i <= 200; i++) {
if (i % 3 == 0)
continue;
printf("%4d", i);
}
}
9.求Pi的值,根据文章来源地址https://www.toymoban.com/news/detail-404982.html
void main() {
int sign = 1;
double pi = 0, n = 1, term = 1;
while (fabs(term) >= 1e-6) {
pi = pi + term;
n = n + 2;
sign = -sign;
term = sign / n;
}pi = pi * 4;
printf("%10.8f", pi);
}
void main() {
int f1 = 1;
int f2 = 1;
int i;
int k;
scanf_s("%d", &k);
for (i = 1; i <= k; i++) {
printf("%12ld%12ld", f1, f2);
if (i % 2 == 0)printf("\n");
f1 = f1 + f2;
f2 = f2 + f1;
}
}
void main() {
int i, n, k;
scanf_s("%d", &k);
long a, b, next;
a = b = 1;
printf("%20ld%20ld", a, b);
n = 2;
for (i = 3; i <= k; i++) {
next = a + b;
printf(" % 20ld", next);
n++;
if (n % 4 == 0)printf("\n");
a = b;
b = next;
}
}
void main() {
int i;
int n;
scanf_s("%d", &n);
for (i = 2; i <= n - 1; i++)
if (n % i == 0)break;
if (i >= n)
printf("%d是素数", n);
else
printf("%d不是素数",n);
}
#include <stdio.h>
#include<math.h>
#define _CRT_SECURE_NO_WARNINGS
void main() {
int i;
int n;
scanf_s("%d", &n);
int temp = (int)(sqrt(n));
for (i = 2; i <=temp; i++)
if (n % i == 0)break;
if (i >= temp)
printf("%d是素数", n);
else
printf("%d不是素数", n);
}
void main() {
int i;
int n;
for (n = 3; n <=99; n++){
for(i=4;i<=n-1;i++)
if (n % i == 0)break;
if (i >= n)
printf("%d是素数\n", n);
}
}
void main() {
int i = 0;
float sum = 0;
float x;
for (i = 1; i <= 1000; i++) {
scanf_s("%f", &x);
sum = sum + x;
if (sum >= M) {
break;
}
}
if (i <= 1000) {
printf("%d\n%f", i, sum);
}
else
printf("%d\n%f", i - 1, sum);
}
void main() {
int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
printf("请输入一行字符:");
char c;
while ((c = getchar()) != '\n') {
if (c >= 'a' && c <= 'z'|| c >= 'A' && c <= 'Z')
c1++;
else if (c >= '0' && c <= '9')
c2++;
else if (c == ' ')
c3++;
else
c4++;
}printf("字母数:%d\n数字数:%d\n空格数:%d\n其他符号数:%d\n", c1, c2, c3, c4);
}
void main() {
int r, m, n, t;
scanf_s("%d\n%d", &m, &n);
if (m < n) {
t = m;
m = n;
n = t;
}
while (2) {
r = m % n;
if (r == 0) {
printf("%d", n);
break;
}
m = n;
n = r;
}
}
void main() {
int n, i;
float fac, sum = 0;
for (n = 1; n <= 20; n++) {
i = 1; fac = 1;
do {
fac = fac * i;
i++;
} while (i <= n);
sum = sum + fac;
}printf("sum=%e\n", sum);
}
void main() {
int n;
float fac = 1, sum = 0;
for (n = 1; n <= 20; n++) {
fac = fac * n;
sum = sum + fac;
}printf("sum=%e\n", sum);
}
void main() {
int n;
float fac = 1, sum = 0;
for (n = 1; n <= 20; n++) {
fac = fac * 2;
sum = sum + fac;
}printf("sum=%e\n", sum);
}
void main() {
int x, y, z;
for (x = 0; x <= 100; x++) {
for (y = 0; y <= 100; y++) {
z = 100 - x - y;
if (15 * x + 9 * y + z==300) {
printf("公鸡:%8d\t母鸡:%8d\t小鸡:%8d\n", x, y, z);
}
}
}
}
void main() {
int i;
int b0, b1, b2;
for (i = 100; i <= 999; i++) {
b0 = i % 10;
b1 = i / 10 % 10;
b2 = i / 100;
if (b0 * b0 * b0 + b1 * b1 * b1 + b2 * b2 * b2 == i)
printf("水仙花数:%d\n", i);
}
}
void main() {
char c;
c = getchar();
while (c != '\n') {
if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') {
if (c >= 'w' && c <= 'z' || c >= 'W' && c <= 'Z') {
c = c - 22;
}else c = c + 4;
}printf("%c", c);
}
}
void main() {
int i,k,s;
for (i = 1; i < 1000; i++) {
s = 0;
for (k = 1; k < i; k++)
if (i % k == 0)
s = s + k;
if (s == i)
printf("%d\n", i);
}
}
文章来源:https://www.toymoban.com/news/detail-404982.html
到了这里,关于C语言循环结构一些重要的练习题(较为基础的)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!