Create a free account on Computer Sir Ki Class and unlock access to valuable learning resources.
Exam Paper Downloads: After registering and logging in, you can download exam question papers in PDF format to practice and prepare for your exams.
Student Support: Get assistance with your doubts, queries, and questions related to your studies. Our support is here to help you understand topics and resolve your learning difficulties.
Register today and make your exam preparation easier and more effective!
Find errors in the following program and write the program with correct syntax.
TC++ #2997
Solution
int a,b; //Correct cout<<“Enter two numbers :”; //Correct cin>>a<<b;
cin should always have stream extraction operator (>>)
const int c;
A constant declaration should necessarily be initialised.
c=a+b;
A constant is a read only variable so it can not have any assignment other than while declaration.
cout<<“Sum of given numbers :”<<c<<endl; //correct
The corrected program can be written as follows –
#include <iostream>
using namespace std;
int main()
{
int a,b;
cout<<"Enter two numbers :";
cin>>a>>b;const int c=a+b;
cout<<"Sum of given numbers :"<<c<<endl;
return 0 ;
}
This program will run well but it is generally not advisable the constant declaration of such things which are likely to change. So even better program for above would be without constant declaration lie follows.
#include <iostream>
using namespace std;
int main()
{
int a,b;
cout<<"Enter two numbers :";
cin>>a>>b;int c=a+b;
cout<<"Sum of given numbers :"<<c<<endl;
return 0 ;
}
Notes
const declaration is useful for things like value of PI, value of Maximum Marks etc. examples are const double PI=3.1416; const int MaxMarks=100;