Website For Developers

Array with loops C++




Example of array with loops c++

//Program to find Square of integer values using arrays
#include<iostream>
#include<conio.h>
using namespace std;
int main() {
  int array[5];
  for(int i=0; i<5; i++) {
  cout<<""Enter value: ";
  cin>>array[i];
  }
  for(int i=0; i<5; i++) {
  cout<<"Square of "<<array[i]<<" is "<<array[i]*array[i];
  }
  getch();
  return 0;
}

Output

Enter value: 5
Enter value: 6
Enter value: 9
Enter value: 11
Enter value: 25

Square of 5 is 25
Square of 6 is 36
Square of 9 is 81
Square of 11 is 121
Square of 25 is 625



2# Example of array with loops c++

//Program to find Sum of float values using arrays
#include<iostream>
#include<conio.h>
using namespace std;
int main() {
  float array[5];
  float total = 0;
  for(int i = 0; i<5 ; i++) {
    cout<<"Enter float value: ";
    cin>>array[i];
    total = total + array[i];
  }
  cout<<"Total is "<<total;
  getch();
  return 0;
}

Output

Enter float value: 1.1
Enter float value: 2.2
Enter float value: 3.9
Enter float value: 43.6
Enter float value: 67.4
Total is 118.2



Example of reverse order of array

//Program to find Sum of float values using arrays
#include<iostream>
#include<conio.h>
using namespace std;
int main() {
  int array[5];
  for(int i=1; i<=5; i++) {
    cout<<"Enter an integer value:";
    cin>>array[i];
  }
  cout<<endl"Actual order"endl;
  for (int i=1; i<=5; i++) {
    cout<<array[i]<<" ";
  }
  cout<<endl"Reverse order"<<endl;
  for (int i=5; i>=1; i--) {
    cout<<array[i]<<" ";
  }
  getch();
  return 0;
}

Output

Enter an integer value: 1
Enter an integer value: 2
Enter an integer value: 3
Enter an integer value: 4
Enter an integer value: 5

Actual order
1 2 3 4 5
Reverse order
5 4 3 2 1