C语言练习题,求s = a+aa+aaa+... ...+aaa...a的值,其中a是一个数字,如2+22+222+2222,a的值和加数个数n,均从键盘获取。要求a属于[1,9],n小于10

实验4-2(do...while循环):
求s = a+aa+aaa+... ...+aaa...a的值,其中a是一个数字,如2+22+222+2222,a的值和加数个数n,均从键盘获取。要求a属于[1,9],n小于10(如果不满足此条件,就重输入)。
输入:输入a值和n值
输入提示信息:"Please input a :"
输入格式:"%d"
输入提示信息:"请输入n:"
输入格式:"%d"
如:
请输入a:8
请输入n:9
输出:算式及和
输出提示信息:
"Sum=算式“,输出格式”%ld“ 
”Sum=和,输出格式”%ld“
如:
Sum=8+88+888+8888+88888+888888+88888888+888888888 
Sum=987654312

重点题!!!!!!
Find the value of s= a + aa + aaa +... + aaa... a, where a is a number, such as 2 + 22 + 222 + 2222. The value of a and the number of addends n are obtained from the keyboard. It is required that a belongs to [1,9] and N is less than 10 (if this condition is not met, re-enter).
Input: enter a value and n value
Enter the prompt: "please input a:"
Input format: '% d'
Enter the prompt: "please input n:"
Input format: '% d'
For example:
Please input a:8
Please input n:9
Output: formula and sum
Output prompt:
"Sum = expression", output format "% LD"“
”Sum = sum, output format "% LD"“
For example:
Sum=8+88+888+8888+88888+888888+8888888+88888888+888888888
Sum=987654312
 
 

代码如下:

#include <stdio.h>



int main()
{	    	 		 	   
    long  a = 0;
    long  b = 0;
    int n = 0;
    int i;
    long sum = 0;
    do
    {	    	 		 	   
        printf("Please input a:");
        scanf("%d", &a);//1
        printf("Please input n:");
        scanf("%d", &n);//1

    }
    while (a > 9 || a < 0 || n > 11 || n < 0);//2
    printf ("Sum=");
    for (i = 0; i < n; i++)
    {	    	 		 	   
        b = a + b;//1
        if (i == 0)
        {	    	 		 	   
            printf ("%ld", b);
        }
        else
        {	    	 		 	   
            printf ("+%ld", b);
        }

        sum += b;//2
        a = 10 * a;//2
    }
    printf ("\nSum=%ld\n", sum);
}	    	 	
运行如下:

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杪商柒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值