Monday, May 14, 2012

Test Programming with C [Problem 1]

/* Problem 1: Write a C Program to find area of a triangle (Hint : A = ½ *h*b). The height and base will be given as input. */


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

float tri_area(float x,float y);

int main()
{
    float h, b, ansarea;
   
    loop1:
    printf("Please enter the height of the triangle:\n");
    scanf("%f", &h);
   
    if(h < 0) {
         printf("Height can not be negative.\n");
         goto loop1;    
         }
   
    loop2:
    printf("\nPlease enter the base of the triangle:\n");
    scanf("%f", &b);
   
    if(b < 0) {
         printf("Base can not be negative.\n");
         goto loop2;    
         }
        
    ansarea = tri_area(h,b);
   
    printf("The area of the triangle is: %f", ansarea);
   
    getch();
    return 0;
}

float tri_area(float x,float y)
{
      float result;
     
      result = (0.5*x*y);
     
      return result;     
}

No comments:

Post a Comment

No Spamming Please.