Test: are there three sides of a triangle ?

Sunday, July 4, 2010

 // C08_Ex 9: Input 3 whole number.
// 3 This is an integer form three sides of a triangle or not ?
// If so, what is the triangle?

#include <stdio.h>

void main()
{
    int a, b, c;
    printf("Input 3 whole number: ");
    scanf("%d%d%d", &a, &b, &c);

    if (a + b > c && a + c > b && b + c > a)
    {
        // This is the 3 sides of a triangle
        if (a == b && b == c)
            printf("=> This is an equilateral triangle\n");
        else
        {
            if (a == b || b == c || c == a)
                printf("=> This is an isosceles triangle\n");
            else
            {
                if (a*a + b*b == c*c || b*b + c*c == a*a || c*c + a*a == b*b)
                    printf("=> This is a right triangle\n");
                else
                    printf("=> This is a regular triangle\n");
            }
        }
    }
    else
        printf("=> This is not the three sides of a triangle\n");
}

Read more »

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 »

Checkout cost of taxi

Tuesday, June 29, 2010

// C08_Ex 7: Checkout cost of taxi from kilometer input. Known:
// First km: 5$
// 2 -> 5 km price: 4$
// 6 km -> price:  3$
// If > 120km , will sale 10%

#include <stdio.h>
#define G1 5
#define G2 4
#define G3 3

void main()
{
    int n;
    float cost;
    printf("Input numbers of km (>0): ");
    scanf("%d", &n);

    if (n == 1)
        cost = G1;
    else
    {
        if (n >= 2 && n <= 5)
            cost = G1 + (n - 1)*G2;
        else
            cost = G1 + 4*G2 + (n - 1 - 4)*G3;
    }

    if (n > 120)
        cost = cost * 0.9;

    printf("=> Total cost: %.2f $\n", cost);
}

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