[Updated] Goldman Sachs Aptitude Test Questions and Answers
Practice List of TCS Digital Coding Questions !!!
Take 50+ FREE!! Online Data Interpretation Mock test to crack any Exams.

Program Discussion :: Basics

Home > Programs > Basics

337 / 279

Write a C program to find the area of a triangle given the base and the corresponding height. The values base and height are both positive integers passed to the program as the first and second command line parameters respectively.

Note: Write the output to stdout formatted as a floating point number rounded to EXACTLY 2 decimal precision WITHOUT any other additional text. Scientific format(such as 1.00E+5) should NOT be used while printing the output. You may assume that the inputs will be such that the output will not exceed the largest possible real number that can be stored in a float type variable.

Show Full Paragraph

Answer:

#include
#include 
int main(int argc, char *argv[])

  if (argc < 3)// as number of arguments needed are 2 and 1 is default arg. 
  {
   printf(" Please provide values for both base and height \n");
   return 0;
  } 
else
 {
  int base = atoi(argv[1]);
  int height = atoi(argv[2]);
  float area = 0.5*base*height;
  printf("%.2f",area);
  return 0;
 }
}

Asked In :: TCS

Post Your Answer Here:

Language:

Post Your Reply Here: