Program Discussion :: 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 ::
Language:
Rafiquar
28 Jan, 2018 11:12 PM
#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;
}
}