1.打印一个数的前5 项之和(如:2+22+222+2222+22222)
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
int n = 0;
int s = 0;
int t = 0;
int sum = 0;
int i = 0;
printf("请输入一个数:");
scanf("%d", &n);
for (i = 0; i < 5; ++i)
{
s = n * pow(10, i); //5 50 500 5000 50000
t += s;
sum += t;
}
printf("%d\n", sum);
system("pause");
return 0;
}
2.不用临时变量交换两数的值
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a = 0;
int b = 0;
printf("请输入两个数的值:");
scanf("%d %d", &a, &b);
/* 加减法:
a = a + b;
b = a - b;
a = a - b;
*/
// 按位与法:
a = a ^ b;
b = a ^ b;
a = a ^ b;
printf("a=%d,b=%d\n", a, b);
system("pause");
return 0;
}
3.求两个数的最大公约数
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
int main()
{
int m = 0;
int n = 0;
int tmp = 0;
printf("请输入两个数:");
scanf("%d %d", &m, &n);
while (tmp = m % n)
{
m = n;
n = tmp;
}
printf("最大公约数是:%d\n",n);
system("pause");
return 0;
}
4.三数排序
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a = 0;
int b = 0;
int c = 0;
int temp = 0;
printf("请输入三个数:");
scanf("%d %d %d", &a, &b, &c);
if (a < b)
{
temp = b;
b = a;
a = temp;
}// a大
if (a < c)
{
temp = c;
c = a;
a = temp;
}//a大
if (b < c)
{
temp = c;
c = b;
b = temp;
}//b大
printf("排序后,数字为:%d %d %d\n", a, b, c);
system("pause");
return 0;
}