add numbers in c++ code example
Example 1: c++ program for addition of two numbers using functions
#include <iostream>
using namespace std;
int addition(int a,int b);
int main()
{
int a,b;
int add;
cout<<"Enter first number: ";
cin>>a;
cout<<"Enter second number: ";
cin>>b;
add=addition(a,b);
cout<<"Addition is: "<<add<<endl;
return 0;
}
int addition(int a,int b)
{
return (a+b);
}
Example 2: sum of two numbers c++
#include<iostream>
using namespace std;
int main()
{
double number1,number2;
double sum0;
sum=number1+number2;
cout<<sum;
}
Example 3: how to add numbers in for loop c++
#include <iostream>
int main() {
int n = 0;
int sum = 0;
std::cout << "Please enter and integer: ";
std::cin >> n;
while (n < 0 ) {
std::cout <<"Please enter positive integer only.\n";
std::cout << "Please enter and integer: ";
std::cin >> n;
}
for (int i = 0; i < n ; i++) {
std::cout << i << "\n";
sum +=i
}
std::cout << "Sum of odd numbers is " << sum <<".\n";
}