C语言程序设计50例(经典收藏)

2020-01-06 20:20:54王冬梅

1.程序分析:利用while语句,条件为输入的字符不为'n'.
      
2.程序源代码:
复制代码
#include "stdio.h"
#include "conio.h"
main()
{
  char c;
  int letters=0,space=0,digit=0,others=0;
  printf("please input some charactersn");
  while((c=getchar())!='n')
  {
    if(c>='a'&&c<='z'||c>='A'&&c<='Z')
      letters++;
      else if(c==' ')
        space++;
        else if(c>='0'&&c<='9')
          digit++;
        else
          others++;
  }
  printf("all in all:char=%d space=%d digit=%d others=%dn",letters,
  space,digit,others);
  getch();
}
==============================================================
【程序18】
题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时
   共有5个数相加),几个数相加有键盘控制。
1.程序分析:关键是计算出每一项的值。
2.程序源代码:
复制代码
#include "stdio.h"
#include "conio.h"
main()
{
  int a,n,count=1;
  long int sn=0,tn=0;
  printf("please input a and nn");
  scanf("%d,%d",&a,&n);
  printf("a=%d,n=%dn",a,n);
  while(count<=n)
  {
    tn=tn+a;
    sn=sn+tn;
    a=a*10;
    ++count;
  }