1.Write C++ program to accept N number from the user and print the elements of the array in reverse order using a pointer.
2.Write C++ program that accepts N elements from the user and display the sum of array elements using pointers.
1..
#include <iostream>
using namespace std;
void reverse(int * arr,int N){
int arr2[N];
for(int i = 0; i < N; i++){
arr2[i] = arr[N-i-1];
}
cout << "Reversed Array\n";
for(int i = 0; i < N; i++){
cout << arr2[i] << " ";
}
}
int main()
{
int N=5;
int arr[N];
cout<<"Enter "<<N<<" numbers:\n";
for (int i=0;i<N;i++){
cin>>arr[i];
}
reverse(arr,N);
return 0;
}
2..
#include <iostream>
using namespace std;
void getSum(int * arr,int N){
int sum=0;
cout<<"Enter "<<N<<" numbers\n";
for(int i = 0; i < N; i++){
cin>>arr[i];
}
for(int i = 0; i < N; i++){
sum+=arr[i];
}
cout<<"\nSum = "<<sum;
}
int main()
{
int N=5;
int arr[N];
getSum(arr,N);
return 0;
}
Comments
Thanks
Leave a comment