Showing posts with label Read number. Show all posts
Showing posts with label Read number. Show all posts

Get number of days in month

Friday, July 2, 2010

 // C08_Ex 8: Input month,year
// Output dates of this month

#include <stdio.h>

void main()
{
    int month, yr, result;
  
    printf("Input month,year: ");
    scanf("%d%d", &month, &yr);
  
    if (month>=1 && month<=12)
    {
        switch (month)
        {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12: result = 31; break;
        case 4:
        case 6:
        case 9:
        case 11: result = 30; break;
        case 2:
            if (yr % 400 == 0 || (yr % 4 == 0 && yr % 100 != 0))    //  leap year
                result = 29;
            else
                result = 28;
        }

        printf("=> month %d/%d has %d days\n", month, yr, result);
    }
    else
        printf("=> Invalid month!\n");
}

Read more »

Read a whole number from 1 to 9

Monday, June 14, 2010

C08_Ex 1: Input a  whole number.Reading this whole number with value from 1 to 9.Default, Output:  "Don't know how to read".

// Input a  whole number.
// Reading this whole number with value from 1 to 9.
// Default, output Don't know how to read.
// Use Visual Studio 2005

#include <stdio.h>

void main()
{
    int n;
    printf("Input a  whole number: ");
    scanf("%d", &n);
   
    switch (n)
    {
        case 1: printf("One"); break;
        case 2: printf("Two"); break;
        case 3: printf("Three"); break;
        case 4: printf("Four"); break;
        case 5: printf("Five"); break;
        case 6: printf("Six"); break;
        case 7: printf("Seven"); break;
        case 8: printf("Eight"); break;
        case 9: printf("Nine"); break;
        default: printf("Don't know how to read!");
    }

    printf("\n");
}

Read more »

 
 
 
 
Copyright © 2010 C Source Code - C++ Source Code. All Rights Reserved. Using Xclear Theme | Bloggerized by Themescook Developed by Helios | Powered by Blogger