A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 唐阿丽 中级黑马   /  2015-11-12 11:24  /  981 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

14、C语言检查是否是闰年

源代码:
/* C program to check whether a year is leap year or not using if else statement.*/

#include <stdio.h>
int main(){
      int year;
      printf("Enter a year: ");
      scanf("%d",&year);
      if(year%4 == 0)
      {
          if( year%100 == 0) /* Checking for a century year */
          {
              if ( year%400 == 0)
                 printf("%d is a leap year.", year);
              else
                 printf("%d is not a leap year.", year);
          }
          else
             printf("%d is a leap year.", year );
      }
      else
         printf("%d is not a leap year.", year);
      return 0;
}
输出1:
Enter year: 2000
2000 is a leap year.
输出2:
Enter year: 1900
1900 is not a leap year.
输出3:
Enter year: 2012
2012 is a leap year.
15、C语言检查一个数是正数、负数还是零

源代码:
#include <stdio.h>
int main()
{
    float num;
    printf("Enter a number: ");
    scanf("%f",&num);
    if (num<=0)
    {
        if (num==0)
          printf("You entered zero.");
        else
          printf("%.2f is negative.",num);
    }
    else
      printf("%.2f is positive.",num);
    return 0;
}
也可以使用if-else语句
/* C programming code to check whether a number is negative or positive or zero using nested if...else statement. */

#include <stdio.h>
int main()
{
    float num;
    printf("Enter a number: ");
    scanf("%f",&num);
    if (num<0)      /* Checking whether num is less than 0*/
      printf("%.2f is negative.",num);
    else if (num>0)   /* Checking whether num is greater than zero*/
      printf("%.2f is positive.",num);
    else
      printf("You entered zero.");
    return 0;
}
输出1:
Enter a number: 12.3
12.30 is positive.
输出2:
Enter a number: -12.3
-12.30 is negative.
输出3:
Enter a number: 0
You entered zero.
16、C语言检查某字符串是不是字母

源代码:
/* C programming code to check whether a character is alphabet or not.*/

#include <stdio.h>
int main()
{
    char c;
    printf("Enter a character: ");
    scanf("%c",&c);
    if( (c>='a'&& c<='z') || (c>='A' && c<='Z'))
       printf("%c is an alphabet.",c);
    else
       printf("%c is not an alphabet.",c);
    return 0;
}
输出1:
Enter a character: *
* is not an alphabet
输出2:
Enter a character: K
K is an alphabet
17、C语言计算自然数的和

源代码:
/* This program is solved using while loop. */

#include <stdio.h>
int main()
{
    int n, count, sum=0;
    printf("Enter an integer: ");
    scanf("%d",&n);
    count=1;
    while(count<=n)       /* while loop terminates if count>n */
    {
        sum+=count;       /* sum=sum+count */
        ++count;
    }
    printf("Sum = %d",sum);
    return 0;
}
也可以使用for循环语句
/* This program is solve using for loop. */

#include <stdio.h>
int main()
{
    int n, count, sum=0;
    printf("Enter an integer: ");
    scanf("%d",&n);
    for(count=1;count<=n;++count)  /* for loop terminates if count>n */
    {
        sum+=count;                /* sum=sum+count */
    }
    printf("Sum = %d",sum);
    return 0;
}
输出:
Enter an integer: 100
Sum = 5050
也可以通过if-else语句
/* This program displays error message when user enters negative number or 0 and displays the sum of natural numbers if user enters positive number. */

#include <stdio.h>
int main()
{
    int n, count, sum=0;
    printf("Enter an integer: ");
    scanf("%d",&n);
    if ( n<= 0)
        printf("Error!!!!");
    else
    {
       for(count=1;count<=n;++count)  /* for loop terminates if count>n */
       {
          sum+=count;                    /* sum=sum+count */
       }
       printf("Sum = %d",sum);
    }
    return 0;
}
18、C语言计算阶乘

对于任意正数n,阶乘指的是:
factorial = 1*2*3*4....n
如果数值是负数,那么阶乘就不存在。并且我们规定,0的阶乘就是1。

源代码:
/* C program to display factorial of an integer if user enters non-negative integer. */

#include <stdio.h>
int main()
{
    int n, count;
    unsigned long long int factorial=1;         
    printf("Enter an integer: ");
    scanf("%d",&n);
    if ( n< 0)
        printf("Error!!! Factorial of negative number doesn't exist.");
    else
    {
       for(count=1;count<=n;++count)    /* for loop terminates if count>n */
       {
          factorial*=count;              /* factorial=factorial*count */
       }
    printf("Factorial = %lu",factorial);
    }
    return 0;
}
输出1:
Enter an integer: -5
Error!!! Factorial of negative number doesn't exist.
输出2:
Enter an integer: 10
Factorial = 3628800

4 个回复

倒序浏览
举的例子好多,值得学习ing!
回复 使用道具 举报
大神,求带,顶礼膜拜
回复 使用道具 举报
学习一下
回复 使用道具 举报
hei军 来自手机 中级黑马 2015-11-15 11:01:24
报纸
有中文注释吗?
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马