Question #226790

Write a C++ program that creates a one dimensional array with of length 5 elements. Your
program should read an integer from the keyboard and test if it is prime. If the integer is
prime, the program should insert the integer into the 3rd index of the array.

Expert's answer



#include <iostream>


using namespace std;


int main()
{
    int arr[5];
    int n, i, m=0, flag=0;  
    cout << "Enter the Number: ";  
    cin >> n;  
    m=n/2;  
    for(i = 2; i <= m; i++)  
    {  
      if(n % i == 0)  
      {  
          flag=1;  
          break;  
      }  
    }  
    if (flag==0)  
        arr[3]=n;
        
    cout<<"\nThe element in the 3rd index of the array is: "<<arr[3];
    return 0;
}

LATEST TUTORIALS
APPROVED BY CLIENTS