Wednesday, November 21, 2018

C code review: calculates perimeter, area & volume of rectangle

This C code shows how to calculates perimeter, area & volume of rectangle, using language supplied operators. And uses if else constructors to prepare menu.

code:

//Tested OK on Code::Blocks IDE 17.12 & Cygwin + GCC tools.
//Calculates Perimeter, Area & Volume of Rectangle.

#include <stdio.h>

void inputoutput ()
{
    int comp,ans;
//   clrscr ();
    printf ("\nchoose please: 1=perimeter,2=area,3=volume] ?: ");
    scanf ("%d",&comp);
    if (comp==1)
    {
        int le, wi;
        printf ("Enter the length: ");
        scanf ("%d",&le);
        printf ("Enter the width: ");
        scanf ("%d",&wi);
        printf ("P=%d",perimeter(le,wi));
    }
    else if (comp==2)
    {
        int le, wi;
        printf ("Enter the length: ");
        scanf ("%d",&le);
        printf ("Enter the width: ");
        scanf ("%d",&wi);
        printf ("A=%d", area(le,wi));
    }
    else if (comp==3)
    {
        int length,width,height;
        printf ("Enter length: ");
        scanf ("%d",&length);
        printf ("Enter width: ");
        scanf ("%d",&width);
        printf ("Enter height: ");
        scanf ("%d",&height);
        printf ("V=%d",volume (length,width,height));
    }

    else
        inputoutput ();
    printf ("\nDo you want to continue? [Yes=1/No=0]: ");
    scanf ("%d",&ans);
    if (ans==1)
        inputoutput ();
    else
        printf ("GOOD BYE");
}
int perimeter (int l, int w)
{
    int per;
    per=(l*2)+(w*2);
    return (per);

}
int area (int le, int wi)
{
    int are;
    are=le*wi;
    return (are);
}
int volume (int length, int width, int height)
{
    int vol;

    vol=(length*width*height);
    return (vol);
}

main ()
{
//    clrscr ();
    inputoutput ();
    getchar ();
}

//end of program

To see such C code reviews refer blog archieve at right side or click CCode lable for till posted posts list.

...till next post, bye-bye and take care.

No comments:

Post a Comment