用C语言编程:从键盘上输入两个正整数n和a, 求Sn=a+aa+aaa+ …a…aa的值(共有n项,最后一项有n个a)

2024-11-16 11:30:46
推荐回答(1个)
回答1:

我以前写过一个一样的程序,你可以看一下。
/*this
program
can
calculate
a+aa+aaa+...
...+aaa...aaa(n
a).*/
#include
int
main(void)
{
int
sum
=
0,s
=
0,a,digit,power
=
1,i;
printf("please
input
the
value
of
number
and
digits:");
scanf("%d%*c%d",&a,&digit);
//digit
is
"n"
in
the
expression.
for(i
=
0;i
<
digit;i++)
//loop
"digit"
times.
{
s
+=
power*a;
//s
is
one
item
of
the
expression.
sum
+=
s;
power
*=
10;
}
printf("the
summary
is:%d\n",sum);
return
0;
}