1.for循环
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i = 0;
int n = 0;
int a = 0;
int sum = 0;
printf("请输入一个数:");
scanf("%d", &n);
for (i = 0; i < 5; i++)
{
a = n + a * 10;
sum += a;
}
printf("sum=%d\n", sum);
system("pause");
return 0;
}
方法2.while循环
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i = 0;
int n = 0;
int a = 0;
int sum = 0;
printf("请输入一个数:");
scanf("%d", &n);
while (i < 5)
{
a = n + a * 10;
sum += a;
i++;
}
printf("sum=%d\n", sum);
system("pause");
return 0;
}
方法3.do..while循环
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i = 0;
int n = 0;
int a = 0;
int sum = 0;
printf("请输入一个数:");
scanf("%d", &n);
do
{
a = n + a * 10;
sum += a;
i++;
} while (i < 5);
printf("sum=%d\n", sum);
system("pause");
return 0;
}
扩展:数字a的前n项求和
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a = 0;
int n = 0;
int sum = 0;
int i = 0;
int k = 0;
scanf("%d%d", &a,&n);
for (i = 0; i < n; i++)
{
k = a + k * 10;
sum = sum + k;
}
printf("sum=%d\n", sum);
system("pause");
return 0;
}