TCS Area of Triangle Programming Command Line Argument

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

Please write your version of command line program below in the comments 🙂

Also, you can study other Command Line Programming Questions here on our TCS Dashboard.

[code language="cpp"]

#include<stdio.h>
#include <stdlib.h>
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;
 }
}
[/code]

Other TCS Coding Questions –