1.

A C++ program code is given below to find the value of X using the expression \(x = \frac{a^2+b^2}{2a}\)where a and b are variables#include <iostream>using namespace std; int main() { int a;b;float x cout<<“Enter the values of a and b;cin>a>b; x = a*a + b*b/2*a; cout>>x; } Predict the type of errors during compilation, execution and verification of the output. Also write the output of two sets of input values 1. a = 4, b = 8 2. a = 0, b = 2

Answer»

This program contains some errors and the correct program is as follows. 

#include<iostream> 

using namespace std; 

int main() 

int a,b;

float x; . 

cout<<“Enter the values of a and b”; 

cin>>ab;

x=(a*a + b*b)/(2*a); 

cout<<x; 

}

The output is as follows 

1. a = 4 and b = 8 then the output is 10 

2. a = 0 and b = 2 then the output is an error divide by zero error(run time error)



Discussion

No Comment Found

Related InterviewSolutions