I'm practicing so I wrote this simple program that suppose to add and divide two numbers. It does that but the result comes out with a 0 at the front and don't know why. Any idea? Thanks
#include<iostream>
using namespace std;
int main()
{
int a ,b;
int result;
a = 0;
b = 0;
//a = a + 90;
//result = 0;
result = a + b;
cout << "Enter a number for a and a number for b:" << endl;
cin >> a >> b;
cout <<"the answer is:" << result << (a + b)/2 <<endl;
result = a + b;
cout << "Enter a number for a and a number for b:" << endl;
cin >> a >> b;
You are summing a and b first and then taking a & b as input.
When a and b are being added, a = 0 & b = 0, so result is giving 0.
Solution:
#include<iostream>
using namespace std;
int main()
{
int a ,b;
int result;
a = 0;
b = 0;
//a = a + 90;
//result = 0;
cout << "Enter a number for a and a number for b:" << endl;
cin >> a >> b;
result = a + b;
cout <<"the answer is:" << result << (a + b)/2.0 <<endl;
The thing being printed is the result (10) and average(5) without any space or newline.
Replace your cout statememnt with:
cout <<"the answer is:" << result << endl;
cout << "Average is: " << (a + b)/2.0 <<endl; //or you can write cout << "Average is: " <<result/2.0 <<endl;
Side note: Remember result/2 and result/2.0 are two different things. Division of two integers give an integer where as division of an integer and a float gives float.