【精华篇】C语言入门级练习20题(一):

Practice 1:

输入一个正整数n,求1+2+3+......+n的和。

#include 
#include 

int main()
{
    int i,n,sum;
    printf("Enter n:");
    scanf("%d",&n);
    sum=0;
    for(i=1;i<=n;i++)
    {
        sum=sum+i;
    }

    printf("Sum of numbers from 1 to %d is %d\n",n,sum);
    return 0;
}


Practice 2:

输入一个正整数n,求1-1/3+1/5-1/7+......共n项之和。

#include 
#include 

int main(void)
{
    int denominator,flag,i,n;
    double item,sum;

    printf("Enter n:");
    scanf("%d",&n);
    flag=1;
    denominator=1;
    sum=0;

    for(i=1;i<=n;i++)
    {
        item=flag*1.0/denominator;
        sum=sum+item;
        flag=-flag;
        denominator=denominator+2;
    }

    printf("sum=%f\n",sum);
    return 0;
}



Practice 3:

从键盘输入一个正整数,计算n!。

#include 
#include 

int main(void)
{
    int sum=1,i,n;
    printf("Enter n:");
    scanf("%d",&n);
    for(i=n;i>=1;i--)
    {
        sum=sum*i;
    }
    printf("sum的值为%d",sum);
    return 0;
}



practice 4:

简单的猜数游戏。输入你所猜的整数(假定1~100内),与计算机产生的被猜数比较。若相等;显示猜中,显示与被猜数的大小关系。

#include 
#include 

int main(void)
{
    int mynumber=38;
    int yournumber;

    printf("Input your number:");
    scanf("%d",&yournumber);
    if(yournumber==mynumber)
    {
        printf("OK!you are right!\n");
    }
    else
        if(yournumber>mynumber)
            printf("Sorry!your number is bigger than my number!\n");
        else
            printf("Sorry!your number is smaller than my number!\n");

        return 0;
}



practice 5:

输入一个正整数n,再输入n个学生的成绩,计算平均分,并统计不及格成绩的学生人数。

#include 
#include 

int main()
{
    int count,i,n;
    double grade,total;

    printf("Enter n:");
    scanf("%d",&n);
    total=0;
    count=0;
    for(i=1;i<=n;i++){
        printf("Enter grade#%d:",i);
        scanf("%lf",&grade);
        total=total+grade;
        if(grade<60){
            count++;
        }
    }
    printf("Grade average=%.2f\n",total/n);
    printf("Number of failures=%d\n",count);
    return 0;
}


practice 6:

求解简单的四则运算表达式,输入一个形式如“操作数  运算符  操作数”的四则运算表达式,输出运算结果。

#include 
#include 

int main()
{
    double value1,value2;
    char op;
    printf("Type in an expression:");
    scanf("%lf%c%lf",&value1,&op,&value2);

    if(op=='+')
        printf("=%.2f\n",value1+value2);
    else if(op=='-')
        printf("=%.2f\n",value1-value2);
    else if(op=='*')
        printf("=%.2f\n",value1*value2);
    else if(op=='/')
        printf("=%.2f\n",value1/value2);
    else printf("Unknown operator\n");

    return 0;
}


practice:7

输入10个字符,统计其中英文字母、数字字符和其他字符的个数。

#include 
#include 

int main()
{
    int digit,letter,other;
    char ch;
    int i;
    digit=letter=other=0;
    printf("Enter 10 characters:");
    for(i=1;i<=10;i++)
    {
        ch=getchar();
        if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
            letter++;
        else if(ch>='0'&&ch<='9')
            digit++;
        else other++;
    }
    printf("letter=%d,digit=%d,other=%d\n",letter,digit,other);
    return 0;
}


practice 8:

求解简单表达式。输入一个形式如“操作数  运算符  操作数”的四则运算表达式,输出运算结果,要求使用switch语句编写。

#include 
#include 

int main()
{
    double value1,value2;
    char op;
    printf("Type in an expression:");
    scanf("%lf%c%lf",&value1,&op,&value2);
    switch(op){
    case '+':
        printf("=%.2f\n",value1+value2);
        break;
    case '-':
        printf("=%.2f\n",value1-value2);
        break;
    case '*':
        printf("=%.2f\n",value1*value2);
        break;
    case '/':
        printf("=%.2f\n",value1/value2);
        break;
    default:
        printf("Unknown operation\n");
        break;
    }

    return 0;
}


practice 9:




你可能感兴趣的:(【精华篇】C语言入门级练习20题(一):)