In: Computer Science
a) Find a bug in the code snippet below. Assume that a, b, c and d are correctly declared and initialized integer variables.
a = b+c if (a=d) then
print *,”A equals to D” else
print *,”A does not equal D” end if
b) Find a bug in the code snippet below. Assume that a,b,and c are correctly declared and initialized integer variables.
if (a+b) > c) then print *,”Sum of A+B is greater than C”
end if
Answer a:
Bug in code a is inside the if statement, For checking the condition to be equal using if statement we should us "==" for comparison inside the if statement,
So, instead of if (a=d) the correct statement should be if(a==d).
C++ bug free code for question a is:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b,c,d;
cout<<"enter the value of b,c,d"<<endl;
cin>>b>>c>>d;
a = b+c;
if(a==d)
{
cout<<"A equals to D";
}
else
{
cout<<"A does not equal D";
}
return 0;
}
Output screen:
Answer b:
Bug in the code b is inside the if condition, their is opening bracket missing of the condition.
Instead of if(a+b)>c) the statement should be if((a+b)>c).
C++ bug free code for question b is:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b,c;
cout<<"enter the value of a,b,c"<<endl;
cin>>a>>b>>c;
if((a+b)>c)
{
cout<<"Sum of A+B is greater than C";
}
return 0;
}
Output screen:
Thank you!!!