本文主要是介绍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);//1printf("Please input n:");scanf("%d", &n);//1}while (a > 9 || a < 0 || n > 11 || n < 0);//2printf ("Sum=");for (i = 0; i < n; i++){ b = a + b;//1if (i == 0){ printf ("%ld", b);}else{ printf ("+%ld", b);}sum += b;//2a = 10 * a;//2}printf ("\nSum=%ld\n", sum);
}
运行如下:
这篇关于C语言练习题,求s = a+aa+aaa+... ...+aaa...a的值,其中a是一个数字,如2+22+222+2222,a的值和加数个数n,均从键盘获取。要求a属于[1,9],n小于10的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!