Identify errors in the following code and rewrite.Attach output in screenshot form
#include
#include void sum()
; // declaring a function
int a=10
b=20
c;
void sum()
// defining function
{
c=a+b;
cout<<"Sum: "<
SOLUTION FOR THE ABOVE QUESTION
THE FOLLOWING ARE THE ERRORS IN THE GIVEN CODE
i)The input/output stream is not included i.e. #include <iostream>
ii)The namespace standard is not defined i.e. using namespace std;
iii)The function named sum is defined wrongly with '#include included in the declaration
iv)The variables b and c are not declared
v)The definition of the sum method has no ending curly brackets i.e. "}"
vi)The main method is not included in the program which is executed
vii)No semicolon used at the end of declaration of variable a
CORRECT CODE
#include <iostream>
using namespace std;
//define sum method receiving two parameters and returning the sum
int sum(int a, int b)
{
return a+b;
}
int main()
{
int a=10;
int b=20;
//call the sum method
int c = sum(a,b);
cout<<"Sum = "<<c<<endl;
}
SAMPLE PROGRAM OUTPUT
Comments
Leave a comment