square the array by foreach loop in C++ by R4R Team


program:

#include < iostream>
#include < algorithm>
using namespace std;
int func(int i)
{
cout<< i*i<< endl;
}
int main()
{
int a[5]={1,2,3,4,5};
for_each(a,a+5,func);
return 0;
}


output-

1
4
9
16
25

-In this program, we just pass each and every element of array in the function and print the square of every element.
-for_each is the special kind of loop in C++ which deal with the each element of the array.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!