C语言输入两个正数a和n求a+aa+aaa+aaaa+…+aa…a(n个a)之和调用fn(a,n)函数
int fn( int a, int n){int i,sum=0,t=0;for( i=1;i<=n;i++){ //1、多条语句要括起来{}t=t*10+a;//a=t;这句 不能要!!不能改变asum += t ;}return sum;}
给你一个代码看哈:
#include
int main()
{
int i,number,n;
int sum=1,total=0;
printf("请输入2个数
");
scanf("%d %d",&number,&n);
for(i=1;i<=n;i++)
{
sum=sum*number;
total+=sum;
}
printf("%d",total);
return 0;
}
#include <stdio.h>
int fn(int a, int n)
{
int sum = a, tem = a;
printf("%d", tem);
for (int i = 2; i <= n; i++)
{
tem = tem * 10 + a;
printf(" + %d", tem);
sum += tem;
}
return sum;
}
int main()
{
int a, n;
printf("input a and n:");
scanf("%d %d", &a, &n);
printf(" = %d
", fn(2, 5));
return 0;
}
楼主你好!
根据你的要求,我实现代码如下!
#include<stdio.h>
int fun(int a,int n){
int sum1=0,sum2=0;
for(int i=0;i<n;i++){
sum1=sum1*10+a;
sum2+=sum1;
}
return sum2;
}
int main(){
int a,n;
printf("Enter the a and n: ");
scanf("%d%d",&a,&n);
printf("result: %d",fun(a,n));
}
希望我的回答对你有帮助!望采纳!
#include <stdio.h>
int fn( int, int );
int main()
{
int a, n;
scanf( "%d %d", &a, &n );
printf( "%d\n", fn( a, n ) );
return 0;
}
int fn( int a, int n )
{
int i, result = 0;
for ( i = 0; i < n; i++ )
{
result += a;
a = a * 10 + a % 10;
}
return result;
}
#include
#include
int fun(a,n)
{
int i,j,k,s=0;
for(i=1;i<=n;i++)
{
k=0;
for(j=1;j<=i;j++)
{
k=k+a*(int)pow(10,j);
}
s=s+k;
}
return s;
}
int main()
{
int a,n;
scanf("%d %d",&a,&n);
printf("%d\n",fun(a,n));
return 0;
}
fn(a,n)=f(a,n-1)*10+a
编程题:输入两个正整数a,n,求a+aa+aaa+aaaa+aa……a(n个a),要求调用函...
就是编写一个函数fn吧,VB的代码:Function fn(a As Integer, n As Integer) As LongDim i As Integer, tmp As Longtmp = 0For i = 1 To ntmp = tmp + a * (10 ^ i - 1) \/ 9Nextfn = tmpEnd Function
输入两个整数a和n,计算Sn=a+aa+aaa+aa...a(n个a),用c语言写个程序将这...
格式错误:scanf("%d,%d",a,n);应该是scanf("%d,%d",&a,&n);逻辑错误:a=a+a*10,a的值变了,不再是原先输入的a。修改方法:格式错误按C语言要求的格式修改。逻辑错误引入新的变量k解决。include <stdio.h>int main(){int a,n,k=0,sum=0,i;\/\/定义变量k,并赋初始值0scanf("%d,...
Python+1、从键盘输入两个正整数n和a,求a+++aa+++aaa+++…+a..a(此 ...
python代码和运行结果如图所示:输出符合题意,望采纳~附源码链接:求和
C语言编程:求Sn=a+aa+aaa+···+aa··a(n个a)的值,其中a和N键盘输入...
include <stdio.h> void main(){ int a,n,count=1;long int sn=0,tn=0;printf("please input a and n\\n");scanf("%d%d",&a,&n);printf("a=%d,n=%d\\n",a,n);while(count<=n){ tn=tn+a;sn=sn+tn;a=a*10;++count;} printf("a+aa+...=%ld\\n",sn);} ...
C语言 求Sn=a+aa+……+aaa……a(n个a),其中a和n是从键盘输入。怎么做啊...
include<stdio.h> int main(){ int a,n,i=1,sn=0,tn=0;printf("a,n=:");scanf("%d,%d",&a,&n);while(i<=n){ tn=tn+a;sn=sn+tn;a=a*10;++i;} printf("a+aa+aaa+...=%d\\n",sn);return 0;} 修改了一下,,现在可以了 ...
求s=a+aa+aaa+aaa…aaa(n个a)的值,a和n由键盘输入。
include<stdio.h> void main() { int s,a,n,i,t;scanf("%d%d",&a,&n); s=0; t=0;for ( i=0;i<n;i++ ) { t*=10; t+=a; s+=t; } printf("%d\\n",s);}
用递归函数求a+aa+aaa+……+aa…a(n个)
include <stdio.h> double fun(int a,int n){ if(n==1)return (double)a;else return (fun(a,n-1)*10.0 + a);} int main(){ int a,n,i;double s=0;printf("输入a和n:\\n");scanf("%d%d",&a,&n);if(!(a>0&&a<10&&n>0)){ printf("输入数据错误\\n");return 1;}...
C语言从键盘出入两个整数,求其和。输出时要求输出一个完整的等式
C语言从键盘出入两个整数,求其和,可以参考下面的代码:include<stdio.h> int main(){ int a1, a2;printf("请输入两个整数:");\/\/输入以空格间隔 回车结束 scanf_s("%d %d", &a1, &a2);printf("%d+%d=%d\\n", a1, a2, a1 + a2);return 0;} ...
用C语言编写程序,如何输入两个整数,并求和。
include <stdio.h> int main()printf("请输入两个整数:");scanf("%d %d",&a,&b);\/\/ sum=a+b;\/\/三、数据处理,把a、b的和赋值给sum;printf("这两个数的和是:%d\\n",sum);\/\/ return 0;\/\/返回;C语言,是一种通用的、过程式的编程语言,广泛用于系统与应用软件的开发。具有高效、...
C语言.编程计算a+aa+aaa+……+aa…a(n个a)的值,要求n和a由用户从键盘输...
a); main(){ int a , n ;printf("Input a,n:\\n");scanf("%d,%d",&n,&a);printf("sum=%ld\\n",sum(n,a));system("pause"); } int sum(int a,int n) {int i ;int A=a ;int s=0 ;for(i=1;i<=n;i++){s+=A;A=A+a*pow((double)10,i);}return s; } ...