1 / 4
Mar 2016

Average of two numbers not printing properly..
#include iostream
#include iomanip
using namespace std;
float avg(int a,int b)
{
float temp = static_cast((a+b)/2);
return temp;
}
int main() {
float m;
m = avg(1,2);
cout< return 0;
}

  • created

    Mar '16
  • last reply

    Mar '16
  • 3

    replies

  • 1.3k

    views

  • 4

    users

Sir, it is difficult (for me) to test your code. It does not compile with c++14 nor with c++5.1 after changing it to

#include <iostream>
#include <iomanip>
using namespace std;
float avg(int a,int b)
{
    float temp = static_cast((a+b)/2);
    return temp;
}
int main()
{
    float m;
    m = avg(1,2);
    cout<<m<<endl;
    return 0;
}

So I must have made an error. Would you mind to help me here?

include iostream.h

include iomanip.h

include conio.h

float avg(float a,float b)
{
float temp = ((a+b)/2);
return temp;
}
int main()
{
float m;
m = avg(1,2);
cout<< m;
getch();
return 0;
}

Sir, Please try this code to get average of two numbers very simply in floating point numbers.

#include <iostream>
using namespace std;
float avg(int a,int b)
{
float temp = (float)(a+b)/2;
return temp;
}
int main()
{
float m;
m = avg(1,2);
cout<<m<<endl;
return 0;
}

Suggested Topics

Topic Category Replies Views Activity
Off-topic 1 81 Apr 9

Want to read more? Browse other topics in Off-topic or view latest topics.