Monday, January 5, 2015

C using Geometrical Figures and Surface Area of cylinder and cos pro



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
 
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

No comments:

Post a Comment