You are on page 1of 9

LAB - ASSIGNMENT - 5

NAME : SHRISHAIL RAVI TERNI


REG_NO : 19BCE0445

1. Assume a 4 digit number and perform the following tasks using


Openmp and MPI programming approach.
- To find the sum of individual digits
- To find the sum of even and odd digits and count of even and
odd digits
- To check the number is prime or not
- To check the 4 digit number is palindrome or not

Code : -
#include<stdio.h>
#include<omp.h>
#include<stdlib.h>
#include<string.h>
void main(){
char a[] = {'1','2','3','4','\0'};
int odd_count =0, eve_count = 0,tid,sum1 = 0;
omp_set_num_threads(4);
#pragma omp parallel private(tid),private(a),private(sum1)
{
tid = omp_get_thread_num();
if(tid == 0)
{
int c,b;
sscanf(a,"%d",&b);
while(b!=0){
#pragma omp parallel shared(sum1,b,c)
{
c = b%10;
sum1 = sum1+c;
b=b/10;
}
}
printf("\nThe sum of the digits is %d",sum1);
}

else if(tid == 1)
{
#pragma omp parallel for
private(a),shared(odd_count,eve_count)

for(int i=0;i<4;i++){
if((int)(a[i]) ==0)
{
eve_count +=1;
}
else{
odd_count +=1;
}
}
printf("\n The odd count is %d and the even count is
%d",odd_count,eve_count);
}
else if(tid ==2){
int flag = 0;
int k;
sscanf(a,"%d",&k);
#pragma omp parallel for private(k)
for(int i=2;i<k/2;i++){
if(k%i==0){
flag+=1;
}
}
if(flag>0){
printf("\n 1234 is not a prime number");
}
else{
printf("\n 1234 is a prime number");
}
}
else if(tid == 3){
int flag = 0;
# pragma omp parallel for private(a)
for(int i=0;i<2;i++){
if(a[i]==a[3-i]){
flag+=1;
}
if(flag!=0){
printf("\n The number is a palindrome");
}
else{
printf("\n The number is not a palindrome");
}
}
}
}

Output :

MPI

Code :
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main(int argc, char** argv) {
printf("\n__Divyansh Jain___\n");
MPI_Init(NULL, NULL);
int no_of_process;
MPI_Comm_size(MPI_COMM_WORLD, &no_of_process);
int process_id;
MPI_Comm_rank(MPI_COMM_WORLD, &process_id);
int num = 1221;
int master_process = 0;
MPI_Bcast(&num, 1, MPI_INT, master_process,
MPI_COMM_WORLD);
if (process_id == 0) {
printf("\nProcess %d is master process broadcasting our
number which is %
d\n", process_id, num);
}
else if (process_id == 1) {
int temp = num;
int sum_digits = 0;
int rem = 0;
while (temp > 0) {
rem = temp % 10;
sum_digits += rem;
temp = temp / 10;
}
printf("\nSum of Digits is : %d and process used is %d\n",
sum_digits,process_id);
printf("\n....\n");
}
else if (process_id == 2) {
int temp = num;
int sum_digits = 0;
int rem = 0;
int odd_count = 0;
int even_count = 0;
int even_sum = 0;
int odd_sum = 0;
while (temp > 0) {
rem = temp % 10;
if (rem % 2 == 0) {
even_count++;
even_sum += rem;
}
else {
odd_count++;
odd_sum += rem;
}
temp = temp / 10;
}
printf("\nNumber of even Digits are : %d and process used is
%d\n",
even_count, process_id);
printf("\nSum of even Digits is : %d and process used is
%d\n", even_sum,
process_id);
printf("\n........\n");
printf("\nNumber of odd Digits are : %d and process used is
%d\n",odd_count,
process_id);
printf("\nSum of odd Digits is : %d and process used is %d\n",
odd_sum,
process_id);
printf("\n...........\n");
}
else if (process_id == 3) {
//Checking whether number is prime or not
int temp = num;
int sum_digits = 0;
int rem = 0;
bool prime = true;
if (temp == 0 || temp == 1) {
prime = false;
}
for (int i = 2; i < temp / 2; i++) {
if (temp % i == 0) {
prime = false;
break;
}
}
if (prime == true) {
printf("\n %d is a prime number and process used is %d\n",
num,process_id);
printf("\n...\n");
}
else {
printf("\n %d is not a prime number and process used is
%d\n",num,process_id);
printf("\n....\n");
}
}
else if (process_id == 4) {
int temp = num;
int sum_digits = 0;
int rem = 0;
int reverse_num = 0;
while (temp > 0) {
rem = temp % 10;
reverse_num = reverse_num * 10 + rem;
temp = temp / 10;
}
if (num == reverse_num) {
printf("\n%d is a palindrome number and process used is
%d\n",num,process_id);
printf("\n......\n");
}
else {
printf("\n%d is not a palindrome number and process used is
%d\n", num,
process_id);
printf("\n.......\n"
);
}
}
MPI_Finalize();
}

Output :

You might also like