Average of three integer numbers, double output – Computer Sir Ki Class


Lost your password?

Don't have an account ?
Register (It's FREE) ×


Solved Problem #CPP#2331 siteicon   siteicon   siteicon  

Problem Statement - Average of three integer numbers, double output

Write a C++ program to find average of three integer numbers given by the user. The output can have fractional double precision values as well.


TC++ #2331


Run Output

Enter three integer numbers :12 37 13
Average of given numbers :20.6667

int a,b,c; cout<<“Enter three integer numbers :”; cin>>a>>b>>c;
Here we are collecting three integer values from the user in variables a, b and c. We are using a cin chain for collecting the input. Multiple values for cin chain can be given by giving space or tab between values or we also give them by pressing enter after the each value.

double avg=double (a+b+c)/3;
Here we take care that on the right hand side denominator should be first made a double precision variable before finding the average. If we do not do so only integer portion of the output may be retained. for example 5/2 in integer form would be 2 while in double(5)/2 form would be 2.5 .

cout<<“Average of given numbers :”<<avg<<endl;
Output is printed here.


  • The typecasting of numerator can be done in two forms. double (a+b+c) or as (double) (a+b+c) . When complex form of typecasting is done the first style is preferred, while for simple expressions the second style may be okay. Clarity of precedence is important. For e.g. if I write (double) a+b+c , one can have the impression that only a is typecasted.

Common Errors

  • Error in typecasting and precedence of  operator has to be watched for. For e.g. watch for following expressions.(double) a+b+c/3  will give wrong result as it will first make division of 3 before adding to a+b.
    double ((a+b+c)/3) will also give wrong result in some cases as numerator calculation will first happen as integer and then division by integer will happen and then lastly double casting be done.


sunmitra| Created: 7-Jan-2018 | Updated: 14-Dec-2018|

Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues