Friday, February 25, 2022

Sum And Product Of Five Numbers In C++

 A simple program to solve the sum and product of five numbers using C++ programming language.

I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me in the following email address for further details.  If you want to advertise on my website kindly contact me also in my email address also. Thank you.


My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

Please subscribe to my channel  https://www.youtube.com/channel/UCOs-lpOoIeJoh6gpJthPoGg

Thank you very much for your support.




Program Listing

// BEGIN CODE


#include <iostream>

#include <algorithm>

#include <numeric>


const std::string ordinal_words[] =

{

     "first", "second", "third", "forth", "fifth"

};


void calculations(int numbers[], int size) {

     int product = 1;


     int sum = 0;


     for (int i = 0; i < size; ++i) {

         product *= numbers[i];

         sum += numbers[i];

     }

     std::cout << "The sum of 5 numbers: " << sum << "\n";


     std::cout << "The product of 5 numbers: " << product << "\n";

}


void four_numbers()

{

     const int NUM_ELEMS = 4;

     int numbers[NUM_ELEMS] = { 0 };


     for (int i = 0; i < NUM_ELEMS; ++i) {

         std::cout << "Enter " << ordinal_words[i] << " number: ";

         std::cin >> numbers[i];

     }

     bool allEqual = std::all_of(numbers + 1, numbers + NUM_ELEMS,

                     [&numbers](int num){return num == numbers[0];} );


     std::cout << (allEqual ?  "true" : "false") << "\n";

}


void five_numbers()

{

     const int NUM_ELEMS = 5;

     int numbers[NUM_ELEMS] = { 0 };


     for (int i = 0; i < NUM_ELEMS; ++i) {

         std::cout << "Enter " << ordinal_words[i] << " number: ";

         std::cin >> numbers[i];

     }

     bool allEqual = std::all_of(numbers + 1, numbers + NUM_ELEMS,

             [&numbers](int num) {

                 return (num == numbers[0]);

             });


     std::cout << (allEqual ? "true" : "false") << "\n";

     calculations(numbers, NUM_ELEMS);

}




int main()

{

     four_numbers();

     five_numbers();


}



No comments:

Post a Comment