3 Replies - 12654 Views - Last Post: 16 March 2009 - 07:00 AM Rate Topic: -----

#1 goldengirl.divya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 16-March 09

sum of 2 numbers in c++

Posted 16 March 2009 - 04:28 AM

hi,
this is divya. i have just started c++. i completed the hello world program in c++. when i run it it doesnt work but when i quit and it asks whether it should be saved or not and i click yes, a black screen appears and the 2 words "hello world" appear and the screen immediately vanishes. however that is not my problem or question. i want to know how to do a program to find the sum of 2 numbers in c++. i want it to be suitable for beginners.

Is This A Good Question/Topic? 0
  • +

Replies To: sum of 2 numbers in c++

#2 satya.c  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-March 09

Re: sum of 2 numbers in c++

Posted 16 March 2009 - 06:46 AM

View Postgoldengirl.divya, on 16 Mar, 2009 - 03:28 AM, said:

hi,
this is divya. i have just started c++. i completed the hello world program in c++. when i run it it doesnt work but when i quit and it asks whether it should be saved or not and i click yes, a black screen appears and the 2 words "hello world" appear and the screen immediately vanishes. however that is not my problem or question. i want to know how to do a program to find the sum of 2 numbers in c++. i want it to be suitable for beginners.

Was This Post Helpful? 0
  • +
  • -

#3 satya.c  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-March 09

Re: sum of 2 numbers in c++

Posted 16 March 2009 - 06:58 AM

View Postgoldengirl.divya, on 16 Mar, 2009 - 03:28 AM, said:

hi,
this is divya. i have just started c++. i completed the hello world program in c++. when i run it it doesnt work but when i quit and it asks whether it should be saved or not and i click yes, a black screen appears and the 2 words "hello world" appear and the screen immediately vanishes. however that is not my problem or question. i want to know how to do a program to find the sum of 2 numbers in c++. i want it to be suitable for beginners.


hi divya, i am giving the solution below for your question


#include <iostream>
using namespace std;

class Add
{
int x, y;

public:

int add_two_nos(int x, int y);
};

int Add::add_two_nos(int n1, int n2)
{
int result;
result = n1 + n2;

return(result);
}

int main()
{

Add obj; // Class variable
int n1, n2, result;

cout << " Enter First Number " << endl;
cin >> n1;

cout << " Enter Second Number " << endl;
cin >> n2;

result = obj.add_two_no(n1, n2);

cout << " Sum of two Numbers : " << result << endl;

return(0);
}
Was This Post Helpful? 0
  • +
  • -

#4 Plus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 41
  • View blog
  • Posts: 414
  • Joined: 24-November 08

Re: sum of 2 numbers in c++

Posted 16 March 2009 - 07:00 AM

int = integer
cout = C++ output method
cin = C++ input method
system("pause") = keep DOS window open
return 0 = no errors

#include<iostream>
using namespace std;

int main(){
  int a,b;
  cout << "enter first number: ";
  cin >>a;
  cout << "enter second number: ";
  cin >>b;
  cout << a <<" + "<<b<<" = "<< a+b << "\n\n ";

  system("pause");
  return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1