For adding two numbers in c++,we have to declare variables.
We can accept numbers from user also using "cin" function or we can do it by declaring numbers to variables.
we will see both the methods.
#include<conio.h>
#include<iostream.h>
void main()
{
int x=2,y=2,z;
/*here variables are of integer type which stores two bytes ,you can take it as float (decimal values) */
z=x+y;
cout<<"\nX:"<<x<<"\nY:"<<y<<"Answer: "<<z;
/*accepting from user*/
cout<<"Enter two numbers: ";
cin>>x>>y;
z=x+y;
cout<<"\nX:"<<x<<"\nY:"<<y<<"Answer: "<<z;
getch();
}
#include<iostream.h>
void main()
{
int x=2,y=2,z;
/*here variables are of integer type which stores two bytes ,you can take it as float (decimal values) */
z=x+y;
cout<<"\nX:"<<x<<"\nY:"<<y<<"Answer: "<<z;
/*accepting from user*/
cout<<"Enter two numbers: ";
cin>>x>>y;
z=x+y;
cout<<"\nX:"<<x<<"\nY:"<<y<<"Answer: "<<z;
getch();
}
Output:

0 comments:
Post a Comment