In: Computer Science
If the current number is divisible by 2, then print CSU
If the current number is divisible by 5, then print SB
If the current number is divisible by both 2 and 5, then print CSUSB
If the number is neither divisible by 2 nor 5, then print the number
Example: 1 CSU 3 CSU SB CSU 7 CSU 9 CSUSB 11 CSU 13 CSU SB CSU 17 CSU 19 CSUSB …
Submit your code for both parts as separate files (C, Java or CPP) and submit your reasoning on your results (MS word document) that should include brief descriptions of both parts. For multithreaded part, please include your reasoning on the code portion where synchronization is implemented and why. For help, please refer to the lectures and codes where we implemented Dining philosophers, and readers and writers problems using semaphores and pthread_cond_wait variables. You are given the choice between semaphores and pthread conditional wait variables whichever seems easier or convenient to you.
Here is the code for the above problem with the output snippet
#include <iostream>
using namespace std;
int main()
{
int arr[50];
int n;
// ask the user to enter the size of the array
cout<<"Enter the size of the array\n";
// size of the array as input
cin>>n;
// loop to get the elements
for(int i=0; i<n; i++)
{
cout<<"Enter element "<<i+1<<endl;
cin>>arr[i];
}
// for loop to check for the conditions
for(int i=0; i<n; i++)
{
// if the number is divisible by 2 check if it is also
// divisible by 5
if(arr[i]%2 == 0)
{
if(arr[i] %5 ==0)
cout<<"CSUSB"<<endl;
else
cout<<"CSU"<<endl;
}
// ceck if the number is divible by 5
else if(arr[i]%5 == 0)
cout<<"SB"<<endl;
// else statement if the number is not divisible by any of them
else
cout<<arr[i]<<endl;
}
return 0;
}
Here is the output snippet
I have given the solution for pnly first part as you have highlighted that only
Tell in the comments if you need solution for second part also