Showing posts with label Begginer. Show all posts
Showing posts with label Begginer. Show all posts

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 »

Arranging numbers in ascending order in C

Monday, June 28, 2010

// C08_Ex 6: Input 4 whole number
// To arrange this numbers in order of in ascending

#include <stdio.h>

void main()
{
    int a, b, c, d, tempt;

    printf("Input a, b, c, d: ");
    scanf("%d%d%d%d", &a, &b, &c, &d);
  
    if (a > b) { tempt = a; a = b; b = tempt; }
    if (a > c) { tempt = a; a = c; c = tempt; }
    if (a > d) { tempt = a; a = d; d = tempt; }
    if (b > c) { tempt = b; b = c; c = tempt; }
    if (b > d) { tempt = b; b = d; d = tempt; }
    if (c > d) { tempt = c; c = d; d = tempt; }

    printf("=> After arranging in ascending: %d %d %d %d\n", a, b, c, d);
}

Arranging numbers in ascending order

Read more »

Find minimum number

Sunday, June 20, 2010

// C08_Ex 5:Input 4 whole number a, b, c, d
// Find minimum number

#include <stdio.h>

void main()
{
    int a, b, c, d, min, max;
    printf("Input a, b, c, d: ");
    scanf("%d%d%d%d", &a, &b, &c, &d);
  
    min = a;
    if (b < min) min = b;
    if (c < min) min = c;
    if (d < min) min = d;

    printf("=> Minimum number: %d\n", min);

Read more »

Solve quadratic equations in C

Friday, June 18, 2010

Today, I tried to translate the very basic "solve quadratic equations" program. I made it while i use C complier of Microsoft : Visual Studio 2005. I have used it since i was a first year student

// C08_Ex 4: To solve quadratic equation: ax^2 + bx + c = 0

#include <stdio.h>
#include <math.h>

void main()
{
    int a, b, c;
    printf("Input a, b, c: ");
    scanf("%d%d%d", &a, &b, &c);
  
    if (a == 0)
    {
        // To solve a simple equation: bx + c = 0
        if (b == 0)
        {
            if (c == 0)
                printf("=> Equation has multitudinous root\n");
            else
                printf("=> Equation has not root\n");
        }
        else
            printf("=> Equation only has a root x = %.2f\n", (float)-c/b);
    }
    else
    {
        double delta = b*b - 4*a*c;

        if (delta < 0)
            printf("=> Equation has not root\n");
        else
        {
            if (delta == 0)
                printf("=> Equation has : x1 = x2 = %.2f\n", (float)-b/(2*a));
            else
            {
                float x1 = (float)(-b - sqrt(delta))/(2*a);
                float x2 = (float)(-b + sqrt(delta))/(2*a);
                printf("=> Equation has 2 root x1 = %.2f, x2 = %.2f\n", x1, x2);
            }
        }
    }
}

solve quadratic equations in C
 solve quadratic equations in C
Read more »

To solve a simple equation in C

Wednesday, June 16, 2010

Whether you are in middle school, high school or even college, you will quickly learn that knowing how to solving equations is essential to any mathematics class. The majority of applications in mathematics will end up in an equation, and you will have to be able to solve that equation in order to answer the original problem. By going over the steps to solving two different linear equations, you will get a better understanding of how they are solved. The two different equations will be in forms ax + b = c and ax - b = c, where a, b and c will be replaced by numeric values and x represents the variable you will be solving for. For the purpose of solve this procedure, the following source code examples will be used: ax+b=0 .

// C08_Ex 3: To solve a simple equation: ax + b = 0

#include <stdio.h>
#include <conio.h>

void main()
{
    int a, b;
    printf("Ipnput a, b: ");
    scanf("%d%d", &a, &b);
  
    if (a == 0)
    {
        if (b == 0)
            printf("=> Equation has multitudinous root\n");
        else
            printf("=> Equation has not root\n");
    }
    else
        printf("=> Equation only has a root: x = %.2f\n", (float)-b/a);
}

Read more »

How to convert a lowercase/uppercase character in C ?

Monday, June 14, 2010

C08_Ex 2:  Enter one letter. If it is  uppercase character, changed to lowercase letters, changed back to uppercase.

// Input a character
// If It's a  upper-case character -> change into lower-case character
// If It's a  lower-case character -> change into upper-case character
// Use Visual Studio 2005

#include <stdio.h>

void main()
{
    char ch;
    printf("Input a character: ");
    scanf("%c", &ch);

    if (ch >= 'a' && ch <= 'z')
        ch = ch - 32;
    else
        if (ch >= 'A' && ch <= 'Z')
            ch = ch + 32;

    printf("=> Character after changing: %c\n", ch);
}

Read more »

Read a whole number from 1 to 9

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