Saturday, March 24, 2018

C++ Program To Input Base, Height of Triangle, Calculate Area

Easyway How To Write a Program in C++ for Calculating Area of Triangle When base and height is given

Program Statement: Write a C++ program to input base and height of a triangle and calculates and prints the area and triangle.

Input base and height, compute area of triangle c++ program
Input base and height, compute area of triangle c++ program

What are the Formulas for Area of Triangle when base and height are given


These are as follows:
  1. area of triangle = 1 / 2 x base x height

How Formula of  Area of Triangle is Transformed into C++ Arithmetic Expressions

In C++ programming, we write the above formula as follows

area = 1.0/2.0 * base * height

C++ Source Code for Calculating Area of Triangle When Base and Height are Given

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>

void main()
{

   float area, base, height;
   clrscr();
    cout<<"Enter base  = ";
    cin>>base;
    cout<<"Enter height  = ";
    cin>>height;
     
    area = 1.0 / 2.0  * base * height;

   
    cout<<"Area of  Triangle = "<<setprecision(2)<<area<<endl;
    
    getch();
}

Sample Run Output Window: Area of Triangle in C Plus Plus Program 




How This Program works : C++ Source Code for Calculating Area of Triangle When Base and Height are Given

#include<iostream.h>   //include for cin, cout
#include<conio.h>     //include for clrscr()
#include<iomanip.h>  //include for setprecision()

void main()
{

   float area, base, height;   //declare variables
   clrscr();
    cout<<"Enter base  = ";
    cin>>base;                          //input base
    cout<<"Enter height  = ";
    cin>>height;      //input height
   
    area = 1.0 / 2.0  * base * height;  //compute area

   
    cout<<"Area of  Triangle = "<<setprecision(2)<<area<<endl; // show result
    
    getch();
}


C++ Program Compute Sum of Cubes of Digits of User Supplied Number Easyway

Q: Write a Program in C++ Programming language that inputs an integer number and computes the sum of cubes of digits of  this user supplied number at run time.

      For example, if given number is 123 then answer will be Sum of cubes of  digits = 36
      because 13+23+33 = 1+8+27 = 36.
C++ Program to compute sum of cubes of digits of user supplied number
C++ Program to compute sum of cubes of digits of user supplied number


C++ Source Code of Sum of cubes of Digits of a given number Program


/*
Program to input a number, then display sum of cubes of its digits.
(c) Www.EasyCppProgramming.Blogspot.Com
*/
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
     long num, temp;
     int, sum=0, r, q;
     clrscr();
     cout<<"Enter a number to add cubes of its Digits=";
     cin>>num;
     temp = num;
     while ( temp>0 )
     {
       q = temp/10;
       r = temp % 10;
       sum = sum +  ( r * r * r);

       temp = q;
     }
     cout<<"\n The sum of cubes of digits of given number "<<num<<"  =  "<<sum;
     getch();
     }
Calculate-cubes-of-digits-of-user-given number
Calculate-cubes-of-digits-of-user-given number


The Working of  C++ Code of Add Digits of a given number Program

This program is of reverse number logic.

1. We will divide 123 by 10 and get quotient=12, r = 3
2. add r*r*r=3*3*3  into sum, so sum=27

3. Now put q =12 into dividend number
4. so divide 12 by 10, quotient=1 and remainder r=2.
5. Add r*r*r=2*2*2 into sum, so sum will become 27+8 =35.
6. now put q=1 into dividend number

7. Now divide 1 by 10, quotient=0 and r=1.
8. so add r*r*r=1*1*1 into sum, and sum becomes 35+1= 36
9. now put quotient=0 into dividend number called temp variable
10. so that while loop test condition while (temp>0) becomes false and loop terminates.

11. Therefore display the answer sum of cubes of digits of number 123 = 36

C++ Program Calculate Sum of Squares of Digits of Given Number

Q: Write a C++ Program to input an integer number. Calculate the sum of squares of digits of          this number.

      For example, if given number is 123 then answer will be Sum squares of  digits = 14
      because 12+22+32 = 1+4+9 = 14.
C++ Program to Calculate sum of squares of digits of given number
C++ Program to Calculate sum of squares of digits of given number


C++ Code of Sum of Squares of Digits of a given number Program

/*
Program to input a number, then display sum of squares of its digits.
(c) Www.EasyCppProgramming.Blogspot.Com
*/
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
     int num, temp, sum=0, r, q;
     clrscr();
     cout<<"Enter a number to add square of its Digits=";
     cin>>num;
     temp = num;
     while ( temp>0 )
     {
       q = temp/10;
       r = temp % 10;
       sum = sum +  ( r * r);

       temp = q;
     }
     cout<<"\n The sum of squares of digits of given number "<<num<<"  =  "<<sum;
     getch();
     }

The Working of  C++ Code of Add Digits of a given number Program

This program is of reverse number logic.

1. We will divide 123 by 10 and get quotient=12, r = 3
2. add r*r=3*3  into sum, so sum=9

3. Now put q =12 into dividend number
4. so divide 12 by 10, quotient=1 and remainder r=2.
5. Add r*r=2*2 into sum, so sum will become 9+4 =13.
6. now put q=1 into dividend number

7. Now divide 1 by 10, quotient=0 and r=1.
8. so add r*r=1*1 into sum, and sum becomes 13+1= 14
9. now put quotient=0 into dividend number called temp variable
10. so that while loop test condition while (temp>0) becomes false and loop terminates.

11. Therefore display the answer sum of squares of digits of number 123 = 14
C++ Program to Calculate sum of squares of digits of given number output of execution
C++ Program to Calculate sum of squares of digits of given number execution

/*
Program to input a number, then display sum squares of its digits.
(c) Www.EasyCppProgramming.Blogspot.Com
*/
#include<iostream.h>     // include header files
#include<conio.h>

void main()
{
     int num, temp, sum=0, r, q;
     clrscr();
     cout<<"Enter a number to add its Digits=";
     cin>>num;                 // input number
     temp = num;             // copy number into temp variable
     while ( temp>0 )      // loop while temp is greater than zero
     {
       q = temp/10;      // calculate quotient = temp number / 10, if num is 123 then q=12
       r = temp % 10;  //  calculate remainder r = 123 % 10, r = 3
       sum = sum + ( r * r );  // sum = 0 + 3x3 =0+9
                    // so square of last digit 3 of 123 is added in sum  variable
                               
       temp = q;      // set next time dividend     temp=12
     }
     cout<<"\n The sum of digits of given number "<<num<<"  =  "<<sum; //sum=14
     getch();
     }

Search C++ Programs and Tutorials