Thursday 6 June 2019

Add Two Numbers in C++

Method#1
#include<iostream>
using namespace std;
void main()
{
int a=5. b=4,s=0; //Initializing Method#1
#include<iostream>
using namespace std;
void main()
{
int a=5. b=4,s=0; //Initializing variables
s=a+b; //Adding numbers and storing result in variable 's'
cout<<"Sum: "<<s<<endl; //Displaying the Result
return 0;
}



Method#2
#include<iostream>
using namespace std;

void main()
{

int a. b,s=0;  //Initializing variables
cout<<"Enter 1st Number: "<<endl; //Prompt a message to user
cin>>a; //Taking input the in variable 'a'
cout<<"Enter 2nd Number: "<<endl; //Prompt a message to user
cin>>b; //Taking input the in variable 'b'
s=a+b;  //Adding numbers and storing result in variable 's'
cout<<"Sum: "<<s<<endl;  //Displaying the Result
return 0;

}

s=a+b; //Adding numbers and storing result in variable 's'
cout<<"Sum: "<<s<<endl; //Displaying the Result
return 0;
}



Method#2
#include<iostream>
using namespace std;

void main()
{

int a. b,s=0;  //Initializing variables
cout<<"Enter 1st Number: "<<endl; //Prompt a message to user
cin>>a; //Taking input the in variable 'a'
cout<<"Enter 2nd Number: "<<endl; //Prompt a message to user
cin>>b; //Taking input the in variable 'b'
s=a+b;  //Adding numbers and storing result in variable 's'
cout<<"Sum: "<<s<<endl;  //Displaying the Result
return 0;

}

No comments:

Post a Comment