answersLogoWhite

0


Best Answer

#include <iostream>

int main()

{

double num1 = 0.0;

std::cout << "Enter first number: ";

std::cin >> num1;

double num2 = 0.0;

std::cout << "Enter second number: ";

std::cin >> num2;

std::cout << num1 << " + " << num2 << " = " << (num1 + num2);

return 0;

}

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

#include<iostream>

#include<vector>

#include<algorithm>

int main()

{

using namespace std;

vector<int> v { 9, 1, 4, 3, 7, 6, 5, 2, 8 };

cout << "Before sorting:\t";

for (unsigned i=0; i<v.size(); ++i) std::cout << v[i] << '\t';

cout << "\nAfter sorting:\t";

sort(v.begin(), v.end());

for (unsigned i=0; i<v.size(); ++i) std::cout << v[i] << '\t';

cout << endl;

}

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

Constructors cannot return values so the only paractical way of achieving this would be to supply an output parameter to the constructor. But what exectly are you constructing and for what reason? You can find the sum of the digits for any value with a simple function.

unsigned sum_of_digits(unsigned num)

{

unsigned sum = 0;

do

{

sum += num % 10;

} while (num/=10);

return sum;

}

To achieve the same thing via a constructor you'd need the following:

class sum_of_digits

{

public:

sum_of_digits(unsigned num, unsigned& sum)

{

sum = 0;

do

{

sum += num % 10;

} while (num/=10);

return sum;

}

};

This class does absolutely nothing useful. Even though the class is empty (no member data), you must still instantiate an object of the class in order to find the sum. And an empty object consumes memory like any other object.

int main()

{

unsigned sum;

sum_of_digits f (42, sum);

// Now you've got two objects occupying memory (sum and sum_of_digits).

sum = sum_of_digits (42);

// With the function call you'd have just one object: the sum itself.

}

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

Use the following two template functions to find the sum and average of any array. The elements in the array must support the compound plus/assignment operator (+=) for both functions, while the average function requires the elements also support the division operator (/). All integral types support these operators by default, thus to sum or average twenty numbers, place all the numbers in an array of the appropriate type and call the appropriate template function, passing the array (by reference) and its size. Note that the average function makes use of the sum function.

template<typename T>

T sum(T A[], const size_t size)

{

T total=0;

for(size_t index=0; index<size; ++index)

total+=A[index];

return(total);

}

template<typename T>

T average(T A[], const size_t size)

{

return(sum(A,size)/size);

}

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

#include<iostream>

int main()

{

int x=0, y=1;

std::cout<<x<<" ";

std::cout<<y<<" ";

while( y<1000000 )

{

std::cout<<(y+=x)<<" ";

x=y-x;

}

std::cout<<std::endl;

return(0);

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

#include

#include

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: C plus plus program for sum of n numbers using class?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Program to generate Fibonacci series using storage class in c plus plus?

i dn't know. haha


A program c plus plus on automorphic numbers or not?

how to write a program that counts automorphic number from 1 to 999


How do you write a C plus plus program that will display the first 10 positive prime numbers?

By learning how to program on C+.


Write c plus plus program for new and delete operator using class?

#include&lt;iostream&gt; class foo{ int m_data; }; int main() { foo* p=new foo; delete( foo), foo=NULL; return(0); }


C plus plus program to find all even numbers between 100 and 150 using for loop?

#includeint main(){int i;for(i=2;i


C plus plus program using a stacks converting a postfix-infix?

Yes


Write a c plus plus program to find the largest among three numbers using ternary operators?

R = (A &gt; B &amp;&amp; A &gt; C) ? A : (B &gt; C) ? B : C; // parentheses not necessary - for clarity only


How to Write a c plus plus program to print number from 1 to 500?

#include using std::cout;using std::endl;int main(viod){cout


Can you use c in c plus plus without using class and object?

Sure.


Explain instantiation of objects in c plus plus?

Instantiation of a class literally means creating an instance of a class. This is the process of allocating memory for an object that you can use in your program.


Program to get a system time using c plus plus?

time in hours second minute


How do you alternate the input numbers using for loop in c plus plus?

Input a variable.