Monday, October 27, 2014

Find the Areas of Different Geometrical Figures

Find the Areas of Different Geometrical Figures
#include 
#include 
void main()
{
    int fig_code;
    float side, base, length, breadth, height, area, radius;
    clrscr();
    printf("-------------------------\n");
    printf(" 1 --> Circle\n");
    printf(" 2 --> Rectangle\n");
    printf(" 3 --> Triangle\n");
    printf(" 4 --> Square\n");
    printf("-------------------------\n");
    printf("Enter the Figure code\n");
    scanf("%d", &fig_code);
    switch(fig_code)
    {
    case 1:
        printf("Enter the radius\n");
        scanf("%f", &radius);
        area = 3.142 * radius * radius;
        printf("Area of a circle = %f\n", area);
        break;
    case 2:
        printf("Enter the breadth and length\n");
        scanf("%f %f", &breadth, &length);
        area = breadth * length;
        printf("Area of a Reactangle = %f\n", area);
        break;
    case 3:
        printf("Enter the base and height\n");
        scanf("%f %f", &base, &height);
        area = 0.5 * base * height;
        printf("Area of a Triangle = %f\n", area);
        break;
    case 4:
        printf("Enter the side\n");
        scanf("%f", &side);
        area = side * side;
        printf("Area of a Square=%f\n", area);
        break;
    default:
        printf("Error in figure code\n");
        break;
    }
    getch();
}

Output
 
-------------------------
 1 --> Circle
 2 --> Rectangle
 3 --> Triangle
 4 --> Square
-------------------------
Enter the Figure code
1
Enter the radius
30
Area of a circle = 2827.800049
 
-------------------------
 1 --> Circle
 2 --> Rectangle
 3 --> Triangle
 4 --> Square
-------------------------
Enter the Figure code
2
Enter the breadth and length
20 30
Area of a Reactangle = 600.000000
 
-------------------------
 1 --> Circle
 2 --> Rectangle
 3 --> Triangle
 4 --> Square
-------------------------
Enter the Figure code
3
Enter the base and height
45 80
Area of a Triangle = 1800.000000
 
-------------------------
 1 --> Circle
 2 --> Rectangle
 3 --> Triangle
 4 --> Square
-------------------------
Enter the Figure code
4
Enter the side
100
Area of a Square=10000.000000
Calculate the Sum of cos(x) Series
#include 
#include 
#include 
 
void main()
{
    int n, x1, i, j;
    float x, sign, cosx, fact;
    clrscr();
    printf("Enter the number of the terms in a series\n");
    scanf("%d", &n);
    printf("Enter the value of x(in degrees)\n");
    scanf("%f", &x);
    x1 = x;
            
    x = x * (3.142 / 180.0);
    cosx = 1;
    sign = -1;
    for (i = 2; i <= n; i = i + 2)
    {
        fact = 1;
        for (j = 1; j <= i; j++)
        {
            fact = fact * j;
        }
        cosx = cosx + (pow(x, i) / fact) * sign;
        sign = sign * (-1);
    }
    printf("Sum of the cosine series = %7.2f\n", cosx);
    printf("The value of cos(%d) using library function = %f\n", x1, cos(x));
    getch();
    getch();
}

Output
 
Enter the number of the terms in a series
3
Enter the value of x(in degrees)
90
Sum of the cosine series =   -0.23
The value of cos(90) using library function = -0.000204
Find the Volume and Surface Area of cylinder
#include 
#include 
#include 
 
int main()
{
 
    float radius, height;
    float surface_area, volume;
    clrscr();
    printf("Enter  value for  radius and height of a cylinder : \n");
    scanf("%f%f", &radius, &height);
    surface_area = 2 * (22 / 7) * radius * (radius + height);
    volume = (22 / 7) * radius * radius * height;
    printf("Surface area of cylinder is: %.3f", surface_area);
    printf("\n Volume of cylinder is : %.3f", volume);
    return 0;
    getch();
}

Output
 
Enter  value for  radius and height of a cylinder :
15 17
Surface area of cylinder is: 2880.000
Volume of cylinder is : 11475.000
Find the Nth Fibonacci Number using Recursion
#include 
#include 
int fibo(int);
 
int main()
{
    int num;
    int result;
    clrscr();
    printf("Enter the nth number in fibonacci series: ");
    scanf("%d", &num);
    if (num < 0)
    {
        printf("Fibonacci of negative number is not possible.\n");
    }
    else
    {
        result = fibo(num);
        printf("The %d number in fibonacci series is %d\n", num, result);
    }
    return 0;
}
int fibo(int num)
{
    if (num == 0)
    {
        return 0;
    }
    else if (num == 1)
    {
        return 1;
    }
    else
    {
        return(fibo(num - 1) + fibo(num - 2));
    } 
    getch();
}

Output
 
Enter the nth number in fibonacci series: 8
The 8 number in fibonacci series is 21
 
Enter the nth number in fibonacci series: 12
The 12 number in fibonacci series is 144
Find HCF of a given Number using Recursion
#include 
#include 
int hcf(int, int);
 
int main()
{
    int a, b, result;
    clrscr();
    printf("Enter the two numbers to find their HCF: ");
    scanf("%d%d", &a, &b);
    result = hcf(a, b);
    printf("The HCF of %d and %d is %d.\n", a, b, result);
}
 
int hcf(int a, int b)
{
    while (a != b)
    {
        if (a > b)
        {
            return hcf(a - b, b);
        }
        else
        {
            return hcf(a, b - a);
        }
    }
    return a;
    getch();
}
Output
 
Enter the two numbers to find their HCF: 24 36

The HCF of 24 and 36 is 12.

No comments:

Post a Comment