Answer to Question #226790 in C++ for Kvngdanzy

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.
1
Expert's answer
2021-08-17T12:54:39-0400


#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS