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 –
- TCS Coding Questions – 1
- TCS Coding Questions – 2
- Area of a Triangle
- TCS Command Line Arguments – Fibonacci Series
- TCS Command Line Program to Swap two numbers
- TCS String Reversal Using Command Line Programming
- Greatest of Two Numbers using CLP
- LCM of Two Number using CLP
- Average of Two Numbers
- Sum of Digits of a number
- Binary to Decimal
- Decimal to Binary
- Factorial of a Number
- Square Root of Prime Number
- Armstrong Number