C++ Input Output Statements (cout/cin) Practice 2

Task 3:

Write a Program to find our area of triangle when three sides a,b,c are given.
The value of a, b and c is taken as input from user..
The formula to find area of triangle using it’s side is:
A = sqrt (s(s-a)(s-b)(s-c))
Where s=(a+b+c)/2

Code:

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	//variables required for this program
	float a,b,c,s;
	//taking a,b,c as input
	cout<<"\nEnter Value of a : ";
	cin>>a;
	cout<<"\nEnter Value of b : ";
	cin>>b;
	cout<<"\nEnter Value of c : ";
	cin>>c;
	//calcualting value of s
	s=(a+b+c)/2.0;
	//now to Area of triangle we need to take square root
	//for that purpose we will use header file math.h
	//ups a is already declared... capital A is saving the value of s*(s-a)*(s-b)*(s-c);
	//while small a is representing one of the side
	float A=s*(s-a)*(s-b)*(s-c);
	//function to find square root is sqrt(number)
	cout<<"\n\tArea of Triangle is : "<<sqrt(A);
}

Output:

Enter Value of a : 3

Enter Value of b : 5

Enter Value of c : 4

        Area of Triangle is : 6

Task 4:

Write a Program that takes 4 numbers as input from user and find sum, Average and Product of all numbers

Code:

#include<iostream>
using namespace std;
int main()
{
	//declare variables
	int n1,n2,n3,n4;
	//variables in which we have to store the result;
	int sum;//stores sum
	float avg;//stores average
	long int mul;//stores result of multiplication
	//take input
	cout<<"Enter 4 Numbers \n";
	cin>>n1>>n2>>n3>>n4;
	//finding sum,avg,product
	sum=n1+n2+n3+n4;
	avg=(n1+n2+n4+n4)/4.0;
	mul=n1*n2*n3*n4;
	//disply result
	cout<<"\nSum = "<<sum<<endl;
	cout<<"\nAverage = "<<avg<<endl;
	cout<<"\nProduct = "<<mul<<endl;
	return 0;
}

Output:

Enter 4 Numbers
5
34
28
35

Sum = 102

Average = 27.25

Product = 166600

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *