Square the array in C++ by R4R Team

Array:
Array is the collection of different values having same data type.
-We have a integer Array that having all integer values.
-Character array is known as String.

Example-
Array is {3,2,4,1,5}
After square the array, {9,4,16,1,25}

program:

#include < iostream>
using namespace std;
class r4r
{
public:
//class method or function
int square(int *a,int n)
{
int i;
cout<<"Array after square each element"<< endl;
for(i=0;i< n;i++)
{
cout<< a[i]*a[i]<<" ";
}
}
};
int main()
{
int a[100],i,n;
r4r obj;
cout<<"Enter number of element in array?"<< endl;
cin>>n;
cout<<"Enter number in array"<< endl;
for(i=0;i< n;i++){
cin>>a[i];
}
//pass in function
obj.square(a,n);
return 0;
}


output-

Enter number of element in array
5
Enter number in array
1 2 3 4 5
Array after square each element
1 4 9 16 25




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!