当前位置:网站首页>Notes on writing questions in C language -- find s=a+aa+aaa+aaaa+aa Value of a

Notes on writing questions in C language -- find s=a+aa+aaa+aaaa+aa Value of a

2022-06-21 19:06:00 Fanyi

 Insert picture description here

subject

Calculation s = a + aa + aaa + aaaa +…+ aa…a Value .

among a It's a number .

for example 2+22+222+2222+22222( At this time, there is 5 Add up the numbers ), The sum of several numbers is controlled by the keyboard .

Ideas

Calculate and add the values of each item by using the circular statement .

Answer key

#include <stdio.h>

int main()
{
    
    int a,n,count=1;
    long int sn=0,tn=0;

    printf(" Please enter  a  and  n:");
    scanf("%d %d",&a,&n);

    while(count<=n)
    {
    
        tn=tn+a;
        sn=sn+tn;
        a=a*10;
        ++count;
    }

    printf("s=%ld\n",sn);
}

Sample output

 Insert picture description here
 Insert picture description here

 Insert picture description here

原网站

版权声明
本文为[Fanyi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211728148660.html