Why won't this code run in C++? #include #include void main() { int A,B,i,sum=0; cout<>A>>B; if(A%2==1||B%2==1) // checking whether given numbers are even or not { cout<<"Invalid input: Enter only even numbers"<B) // checking whether A is greater than B or not { cout<<"Invalid input: First number A must be smaller than Second number B"<
Addition of Two Numbers
Adding two numbers in programming is essentially the same as adding two numbers in general arithmetic. A significant difference is that in programming, you need to pay attention to the data type of the variable that will hold the sum of two numbers.
C++
C++ is a general-purpose hybrid language, which supports both OOPs and procedural language designed and developed by Bjarne Stroustrup. It began in 1979 as “C with Classes” at Bell Labs and first appeared in the year 1985 as C++. It is the superset of C programming language, because it uses most of the C code syntax. Due to its hybrid functionality, it used to develop embedded systems, operating systems, web browser, GUI and video games.
Why won't this code run in C++?
#include<iostream>
#include<stdlib>
void main()
{
int A,B,i,sum=0;
cout<<endl<<"Enter any two even numbers: ";
cin>>A>>B;
if(A%2==1||B%2==1) // checking whether given numbers are even or not
{
cout<<"Invalid input: Enter only even numbers"<<endl;
exit(1);
}
else if(A>B) // checking whether A is greater than B or not
{
cout<<"Invalid input: First number A must be smaller than Second number B"<<endl;
exit(1);
}
else
{
for(i=A;i<=B;i++)
{
if(i%2==0) // checking for even number
{
sum=sum+(i*i);
}
}
}
cout<<"Sum of even numbers between "<<A<<" and "<<B<<" is "<<sum<<endl;
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images