一:
求和:1+(1+4)+(1+4+9)+(1+4+9+16)+…+(1+4+9+16+…+n2)=? 其中n的值由键盘输入;
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>int main()
{int sum = 0;int n = 0;printf("请输入一个整数");scanf("%d", &n);for (int i = 0; i < n; i++){for (int j = 1; j <= n - i; j++){sum += (j * j);}}printf("结果:");printf("%d", sum);return 0;
}
二:
不断输入整数,统计大于0的个数以及小于0的个数,当输入为0的时候结束输入。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>int main()
{int input = 0;int positive = 0;int negative = 0;printf("请输入(输入0结束输入): ");do{scanf("%d", &input);if (input > 0){positive++;}else if (input < 0){negative++;}} while (input != 0);printf("正数:%d个;负数:%d个\n", positive, negative);return 0;
}
三 :
有一分数序列:1/3,3/5,5/7,7/9...,输出该数列前10项的和。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>int main()
{double sum = 0;double member = 0;for (int i = 1; i <= 10; i++){member = (1.0) * (2 * i - 1) / (2 * i + 1);sum += member;}printf("%lf", sum);return 0;
}
四:
将100-200之间能被3整除且不能被5整除的数及个数输出。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>int main()
{int num = 0;for (int i = 100; i <= 200; i++){if (i % 3 == 0 && i % 5 != 0){num++;printf("%d ", i);}}printf("\n一共%d个", num);return 0;
}
五:
输入一个正整数,判断它是几位数。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>int main()
{printf("请输入一个整数:");int input = 0;int digit = 0;scanf("%d", &input);while (input != 0){digit++;input /= 10;}printf("共几位:%d\n", digit);return 0;
}
六:
打印三角形,三角形的行数n由键盘输入。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>int main()
{int n = 0;printf("请输入打印几行\n");scanf("%d", &n);for (int i = 1; i <= n; i++){for (int j = 1; j <= n - i; j++){printf(" ");}//先打印空格for (int j = 1; j <= 2 * i - 1; j++){printf("*");}//再打印*printf("\n");}return 0;
}