About Me

My photo
Vijayapur, Karnataka, India
Let us learn together!

Thursday, 6 August 2026

PC Lab Expt 3:

 

Program 3: Calculate Fibonacci Numbers Using OpenMP Tasks

Objective

To compute the Nth Fibonacci number using OpenMP tasks, where recursive function calls are executed as parallel tasks.


Prerequisites

  1. Recursion

    • Fibonacci numbers are calculated recursively using:
      [
      F(n) = F(n-1) + F(n-2)
      ]
      where (F(0)=0) and (F(1)=1).

  2. OpenMP Tasks

    • A task is an independent unit of work that can be executed by any available thread.

    • Tasks are created using:

      #pragma omp task
      
  3. Task Synchronization

    • #pragma omp taskwait ensures that all child tasks finish before the parent task continues.

  4. Single Directive

    • #pragma omp single ensures that only one thread creates the initial Fibonacci task.


Algorithm

  1. Start.

  2. Read the value of n.

  3. Create a parallel region.

  4. Use single directive to allow one thread to start the recursive Fibonacci computation.

  5. If n < 2, return n.

  6. Create one task to compute fib(n−1).

  7. Create another task to compute fib(n−2).

  8. Wait until both tasks complete using taskwait.

  9. Return the sum of the two results.

  10. Display the Fibonacci value.

  11. Stop.


Program

#include <stdio.h>
#include <omp.h>

int fib(int n) {
    if (n < 2)
        return n;

    int x, y;

    #pragma omp task shared(x)
    x = fib(n - 1);

    #pragma omp task shared(y)
    y = fib(n - 2);

    #pragma omp taskwait

    return x + y;
}

int main() {
    int n, result;

    printf("Enter value of n: ");
    scanf("%d", &n);

    #pragma omp parallel
    {
        #pragma omp single
        result = fib(n);
    }

    printf("Fibonacci(%d) = %d\n", n, result);

    return 0;
}

Sample Input

Enter value of n: 10

Sample Output

Fibonacci(10) = 55

Other Possible Outputs

Input: 5

Enter value of n: 5
Fibonacci(5) = 5

Input: 6

Enter value of n: 6
Fibonacci(6) = 8

Input: 8

Enter value of n: 8
Fibonacci(8) = 21

Input: 12

Enter value of n: 12
Fibonacci(12) = 144

Result

The program successfully computes the Nth Fibonacci number using OpenMP tasks. Independent recursive calls are executed as separate tasks, and taskwait synchronizes them before combining their results, demonstrating task-based parallelism in OpenMP.


Explanation of OpenMP Task Directives (Connected to the Problem Statement)

Problem Statement:
Write an OpenMP program to calculate the Nth Fibonacci number using tasks.

The Fibonacci sequence is defined as:

[
F(n) = F(n-1) + F(n-2)
]

To compute F(n), the program must calculate F(n−1) and F(n−2). These two calculations are independent, so they can be performed simultaneously using OpenMP tasks.


1. #pragma omp task shared(x)

#pragma omp task shared(x)
x = fib(n - 1);

Explanation:

  • Creates a new task to compute the Fibonacci value of n−1.

  • Any available thread in the OpenMP thread team can execute this task.

  • The computed result is stored in the shared variable x.

Connection to the problem:

  • While calculating F(n), one independent subproblem is F(n−1).

  • Instead of waiting for it to finish sequentially, OpenMP executes it as a separate task.


2. #pragma omp task shared(y)

#pragma omp task shared(y)
y = fib(n - 2);

Explanation:

  • Creates another independent task to compute F(n−2).

  • This task can execute concurrently with the task computing F(n−1).

  • The result is stored in the shared variable y.

Connection to the problem:

  • The second independent subproblem, F(n−2), is also executed in parallel.

  • Since F(n−1) and F(n−2) do not depend on each other, they are ideal candidates for task-based parallelism.


3. #pragma omp taskwait

#pragma omp taskwait

Explanation:

  • Suspends the current task until all child tasks created by it have completed.

  • Ensures that both x and y contain valid results before they are added.

Connection to the problem:

  • The Fibonacci formula is:
    [
    F(n) = F(n-1) + F(n-2)
    ]

  • The program cannot compute x + y until both values are available.

  • taskwait guarantees that the calculations of F(n−1) and F(n−2) are finished before returning the final Fibonacci value.


Working Example (n = 5)

fib(5)
├── Task 1 → fib(4)
└── Task 2 → fib(3)
       ↓
   taskwait
       ↓
Return fib(4) + fib(3)
  • Task 1: Computes fib(4) and stores the result in x.

  • Task 2: Computes fib(3) and stores the result in y.

  • taskwait: Waits until both tasks finish.

  • Final Result: Returns x + y = 3 + 2 = 5.


Summary

OpenMP DirectivePurposeRole in Fibonacci Problem
#pragma omp task shared(x)Creates a task to compute fib(n-1)Computes the first recursive subproblem in parallel
#pragma omp task shared(y)Creates a task to compute fib(n-2)Computes the second recursive subproblem in parallel
#pragma omp taskwaitWaits for all child tasks to completeEnsures both results are available before calculating fib(n) = x + y

Key Idea: The Fibonacci problem naturally breaks into two independent recursive computations (fib(n−1) and fib(n−2)). OpenMP tasks execute these computations concurrently, and taskwait synchronizes them before combining their results.

PC Lab Expt2

 

Prerequisites (Conceptual)

Before understanding this program, students should know the following OpenMP concepts:

  1. Static Scheduling

    • In static scheduling, loop iterations are divided into fixed chunks before execution begins.

    • These chunks are assigned to threads in a round-robin manner.

    • The assignment remains fixed throughout the execution, resulting in low scheduling overhead.

  2. Chunk Size

    • A chunk is a group of consecutive loop iterations assigned to a thread.

    • In schedule(static, 2), each chunk contains 2 iterations.

    • Example (8 iterations):

      • Chunk 1 → Iterations 0, 1

      • Chunk 2 → Iterations 2, 3

      • Chunk 3 → Iterations 4, 5

      • Chunk 4 → Iterations 6, 7

  3. Thread Assignment

    • Each thread executes the chunk assigned to it.

    • If there are more chunks than threads, chunks are assigned cyclically (round-robin).

  4. Parallel for Loop

    • The #pragma omp parallel for directive divides the loop iterations among multiple threads so they execute simultaneously.


Example: schedule(static,2)

For 8 iterations and 2 threads:

ChunkIterationsAssigned Thread
10, 1Thread 0
22, 3Thread 1
34, 5Thread 0
46, 7Thread 1

For 9 iterations and 2 threads:

ChunkIterationsAssigned Thread
10, 1Thread 0
22, 3Thread 1
34, 5Thread 0
46, 7Thread 1
58Thread 0

Key Point: In static scheduling, the chunks are assigned before execution starts, and the assignment does not change during program execution. The output order may vary due to concurrent execution, but the thread-to-iteration assignment remains fixed.



Program 2: OpenMP Static Scheduling with Chunk Size = 2

Objective

To demonstrate static scheduling in OpenMP with a chunk size of 2, where loop iterations are divided into fixed chunks of two consecutive iterations and assigned to threads before execution.


Algorithm

  1. Start.

  2. Read the number of iterations n from the user.

  3. Create a parallel region using OpenMP.

  4. Apply #pragma omp parallel for schedule(static,2) to the loop.

  5. Divide the loop iterations into chunks of 2.

  6. Assign each chunk statically to available threads in a round-robin manner.

  7. Each thread prints its thread ID and the iteration it executes.

  8. Stop.


Program

#include <stdio.h>
#include <omp.h>

int main() {
    int n;

    printf("Enter number of iterations: ");
    scanf("%d", &n);

    #pragma omp parallel for schedule(static, 2)
    for (int i = 0; i < n; i++) {
        printf("Thread %d executes iteration %d\n",
               omp_get_thread_num(), i);
    }

    return 0;
}

Sample Input

Enter number of iterations: 4

Sample Output (2 Threads)

Thread 0 executes iteration 0
Thread 0 executes iteration 1
Thread 1 executes iteration 2
Thread 1 executes iteration 3

Note: The exact order of printed lines may vary because threads execute concurrently. However, with schedule(static,2), iterations are assigned in fixed chunks of two. For example, with two threads:

  • Thread 0: Iterations 0–1

  • Thread 1: Iterations 2–3

  • If more iterations exist, the next chunks are assigned in a round-robin manner (e.g., Thread 0 gets 4–5, Thread 1 gets 6–7, and so on).


Result

The program successfully demonstrates OpenMP static scheduling with chunk size = 2, where consecutive groups of two iterations are statically distributed among the available threads before execution.


Since OpenMP executes threads concurrently, the assignment of iterations to threads remains fixed with schedule(static,2), but the order in which the printf() statements appear can vary.

Assume:

  • Number of iterations = 8

  • Number of threads = 2

Possible Output 1

Thread 0 executes iteration 0
Thread 0 executes iteration 1
Thread 1 executes iteration 2
Thread 1 executes iteration 3
Thread 0 executes iteration 4
Thread 0 executes iteration 5
Thread 1 executes iteration 6
Thread 1 executes iteration 7

Possible Output 2

Thread 1 executes iteration 2
Thread 1 executes iteration 3
Thread 0 executes iteration 0
Thread 0 executes iteration 1
Thread 0 executes iteration 4
Thread 0 executes iteration 5
Thread 1 executes iteration 6
Thread 1 executes iteration 7

Possible Output 3

Thread 0 executes iteration 0
Thread 1 executes iteration 2
Thread 0 executes iteration 1
Thread 1 executes iteration 3
Thread 0 executes iteration 4
Thread 1 executes iteration 6
Thread 0 executes iteration 5
Thread 1 executes iteration 7

If 4 Threads are Used (n = 8)

Each thread initially gets one chunk of 2 iterations.

Possible Output 1

Thread 0 executes iteration 0
Thread 0 executes iteration 1
Thread 1 executes iteration 2
Thread 1 executes iteration 3
Thread 2 executes iteration 4
Thread 2 executes iteration 5
Thread 3 executes iteration 6
Thread 3 executes iteration 7

Possible Output 2

Thread 2 executes iteration 4
Thread 2 executes iteration 5
Thread 0 executes iteration 0
Thread 0 executes iteration 1
Thread 3 executes iteration 6
Thread 3 executes iteration 7
Thread 1 executes iteration 2
Thread 1 executes iteration 3

If n = 10 and 2 Threads

The chunks are:

  • Chunk 1 → Iterations 0–1 → Thread 0

  • Chunk 2 → Iterations 2–3 → Thread 1

  • Chunk 3 → Iterations 4–5 → Thread 0

  • Chunk 4 → Iterations 6–7 → Thread 1

  • Chunk 5 → Iterations 8–9 → Thread 0

One possible output is:

Thread 0 executes iteration 0
Thread 0 executes iteration 1
Thread 1 executes iteration 2
Thread 1 executes iteration 3
Thread 0 executes iteration 4
Thread 0 executes iteration 5
Thread 1 executes iteration 6
Thread 1 executes iteration 7
Thread 0 executes iteration 8
Thread 0 executes iteration 9

Important Note

With schedule(static,2):

  • The iteration-to-thread assignment is deterministic (fixed before execution).

  • Only the order of the printed output may vary because threads execute simultaneously. The thread assigned to a particular iteration will not change for a given number of threads and scheduling policy.


Yes. schedule(static,2) works for both even and odd numbers of iterations. The iterations are divided into chunks of 2, and if the total number of iterations is odd, the last chunk contains only one iteration.


Case 1: Even Number of Iterations (n = 8)

Chunks:

  • Chunk 1 → 0, 1

  • Chunk 2 → 2, 3

  • Chunk 3 → 4, 5

  • Chunk 4 → 6, 7

With 2 Threads

ThreadIterations
Thread 00, 1, 4, 5
Thread 12, 3, 6, 7

Possible Output

Thread 0 executes iteration 0
Thread 0 executes iteration 1
Thread 1 executes iteration 2
Thread 1 executes iteration 3
Thread 0 executes iteration 4
Thread 0 executes iteration 5
Thread 1 executes iteration 6
Thread 1 executes iteration 7

Case 2: Odd Number of Iterations (n = 9)

Chunks:

  • Chunk 1 → 0, 1

  • Chunk 2 → 2, 3

  • Chunk 3 → 4, 5

  • Chunk 4 → 6, 7

  • Chunk 5 → 8 (only one iteration)

With 2 Threads

ThreadIterations
Thread 00, 1, 4, 5, 8
Thread 12, 3, 6, 7

Possible Output

Thread 0 executes iteration 0
Thread 0 executes iteration 1
Thread 1 executes iteration 2
Thread 1 executes iteration 3
Thread 0 executes iteration 4
Thread 0 executes iteration 5
Thread 1 executes iteration 6
Thread 1 executes iteration 7
Thread 0 executes iteration 8

Another Odd Example (n = 7)

Chunks:

  • Chunk 1 → 0, 1

  • Chunk 2 → 2, 3

  • Chunk 3 → 4, 5

  • Chunk 4 → 6

With 2 Threads

ThreadIterations
Thread 00, 1, 4, 5
Thread 12, 3, 6

Possible Output

Thread 0 executes iteration 0
Thread 0 executes iteration 1
Thread 1 executes iteration 2
Thread 1 executes iteration 3
Thread 0 executes iteration 4
Thread 0 executes iteration 5
Thread 1 executes iteration 6

Conclusion

  • Even number of iterations: Every chunk contains exactly 2 iterations.

  • Odd number of iterations: The last chunk contains only 1 iteration.

  • The chunk-to-thread assignment is fixed with schedule(static,2), while the order of the printed lines may vary because the threads execute concurrently.

 

Monday, 18 May 2026

AI Prompts to get source code for IDT projects

AI Prompt-1

Design and develop web application that read student roll number, name, USN and Department and store in Browsers memory.

Next contnued AI prompt: Explain about browsers memory storage

AI Prompt-2

Design and develop web application that read student roll number, name, USN and Department and store in Browsers memory.But store the inputs in excel file (if excel file is not there for first time then create it ; if exists, open and add the inputs)

Next contnued AI prompt: for evey click it needs to append with existing data

AI Prompt-3

Design and Develop web application for Calculator (use CSS for good apperance) to perform arithematical calculations.

Next contnued AI prompt: Heading: BLDEA's V. P. Dr. P. G. Halakatti College of Engineering and Technology., Vijayapur-586103. Two Days Workshop on Design and development of Web Applications 18th and 19th May 2026 and animated buttons

Next contnued AI prompt: Heading: BLDEA's V. P. Dr. P. G. Halakatti College of Engineering and Technology., Vijayapur-586103. Two Days Workshop on Design and development of Web Applications 18th and 19th May 2026 and animated buttons

Next contnued AI prompt:give me full code

AI Prompt-4

 I am engineering student. My name is _______________. Please explain me about "Innovative Design Thinking"

AI Prompt-5

Can you design and devlope an web application for "Anti proxy smart attendance system "

Next contnued AI prompt: Give me all files listed in 21. Suggested Folder Structure

-------------------------------------------------------

AI Prompt-6

Design and Dvelop web application for "Anti proxy smart attendance system " for beginner with single html file that includes css content that takes input and stores in excel file in the same folder. Very simple example. so that I can save all files in single folder and run it.

------------------------------------------------------

Next contnued AI prompt: Can you design only one webpage so that esuly i can run it

Next contnued AI prompt: where inputs are stored?

-----------------------------

AI Prompt-7 

Design and develop web bsed application with single html file (minimum number of files)."Lack of online complaint portal for college students "

AI Prompt-8

Design and develop web bsed application with single html file (minimum number of files)."Lack of online complaint portal for college students "

AI Prompt-9

Medicine inventory management .

Link-1: https://chatgpt.com/share/6a0bbc2e-a1f4-8320-8272-386ad45d9af0

Link-2:  https://chatgpt.com/share/6a0bbc13-48f0-8323-80fb-e7c8975b85cb




Monday, 8 September 2025

PARALLEL COMPUTING (BCS702) Program 6: Write a MPI program to demonstration of deadlock using point to point communication and avoidance of deadlock by altering the call sequence

 Department of Computer Science & Engineering, BLDEACET, Vijayapura

13 Lab Manual : PARALLEL COMPUTING (BCS702)

Program 6: Write a MPI program to demonstration of deadlock using point to point communication and avoidance of deadlock by altering the call sequence

Objective: Demonstrate deadlock and its avoidance using MPI.

Part A: Deadlock Example

Code (Deadlock-prone)

// mpi_deadlock.c

#include <stdio.h>

#include <mpi.h>

int main(int argc, char* argv[]) {

int rank, size, data;

MPI_Init(&argc, &argv);

MPI_Comm_rank(MPI_COMM_WORLD, &rank);

if (rank == 0) {

int msg = 100;

MPI_Recv(&data, 1, MPI_INT, 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);

MPI_Send(&msg, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);

} else if (rank == 1) {

int msg = 200;

MPI_Recv(&data, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);

MPI_Send(&msg, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);

}

MPI_Finalize();

return 0;

}


Explanation:

#include <stdio.h>

#include <mpi.h>

int main(int argc, char* argv[]) {

    int rank, size, data;

    MPI_Init(&argc, &argv);                  // Start MPI environment

    MPI_Comm_rank(MPI_COMM_WORLD, &rank);    // Get process rank (0, 1, ...)

....
  • MPI_Init → Initializes MPI.

  • MPI_Comm_rank → Gives each process a unique ID (rank).

    • Example: If you run with 2 processes → one will have rank=0, the other rank=1.

Process 0 (rank = 0)

if (rank == 0) { int msg = 100; MPI_Recv(&data, 1, MPI_INT, 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); MPI_Send(&msg, 1, MPI_INT, 1, 0, MPI_COMM_WORLD); }
  • Creates an integer message msg = 100.

  • First action → MPI_Recv: process 0 waits to receive an integer from process 1.

  • Only after receiving, it will send its own message (100) to process 1.


👀 Process 1 (rank = 1)

else if (rank == 1) { int msg = 200; MPI_Recv(&data, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); MPI_Send(&msg, 1, MPI_INT, 0, 0, MPI_COMM_WORLD); }
  • Creates an integer message msg = 200.

  • First action → MPI_Recv: process 1 waits to receive an integer from process 0.

  • Only after receiving, it will send its own message (200) to process 0.


❌ The Problem (Deadlock)

  • Process 0 → waiting for data from Process 1 (via MPI_Recv).

  • Process 1 → waiting for data from Process 0 (via MPI_Recv).

👉 Both are stuck waiting forever.
Since neither sends before receiving, no data is sent, so both processes are blocked.
This situation is called a deadlock.

Conclusion:
Both processes wait for Recv first, which leads to a deadlock as neither can proceed to Send.

Sample Output (Deadlock)
$ mpirun -np 2 ./mpi_deadlock
# Program hangs indefinitely — no output is produced

Part B: Deadlock-Free Version
Code (Avoiding Deadlock by Call Order)
// mpi_no_deadlock.c
#include <stdio.h>
#include <mpi.h>
int main(int argc, char* argv[]) {
int rank, data;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 0) {
int msg = 100;
MPI_Send(&msg, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
MPI_Recv(&data, 1, MPI_INT, 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("Process 0 received %d from Process 1\n", data);
} else if (rank == 1) {
int msg = 200;
MPI_Send(&msg, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
MPI_Recv(&data, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("Process 1 received %d from Process 0\n", data);
}

Explanation:

 Code (Deadlock-Free)

#include <stdio.h> #include <mpi.h> int main(int argc, char* argv[]) { int rank, data; MPI_Init(&argc, &argv); // Step 1: Start MPI environment MPI_Comm_rank(MPI_COMM_WORLD, &rank); // Step 2: Get process rank (0 or 1)
  • MPI_Init → starts MPI.

  • MPI_Comm_rank → gives each process a unique rank (0 or 1 here).


👀 Process 0 (rank = 0)

if (rank == 0) { int msg = 100; MPI_Send(&msg, 1, MPI_INT, 1, 0, MPI_COMM_WORLD); // Step 3: Send first MPI_Recv(&data, 1, MPI_INT, 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); // Step 4: Receive later printf("Process 0 received %d from Process 1\n", data); }
  • Creates message msg = 100.

  • First action → MPI_Send: sends 100 to process 1.

  • Then it waits to receive an integer from process 1.

  • Finally prints:

    Process 0 received 200 from Process 1

👀 Process 1 (rank = 1)

else if (rank == 1) { int msg = 200; MPI_Send(&msg, 1, MPI_INT, 0, 0, MPI_COMM_WORLD); // Step 3: Send first MPI_Recv(&data, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); // Step 4: Receive later printf("Process 1 received %d from Process 0\n", data); }
  • Creates message msg = 200.

  • First action → MPI_Send: sends 200 to process 0.

  • Then it waits to receive an integer from process 0.

  • Finally prints:

    Process 1 received 100 from Process 0

✅ Why This Code Does NOT Deadlock

  • In Part A, both processes did MPI_Recv first → they blocked forever.

  • In Part B, both processes do MPI_Send first → message is sent immediately and stored in MPI’s buffer.

  • Then when they call MPI_Recv, the matching message is already available → they succeed.

Thus, no process gets stuck. 🎯


🔑 Key Takeaway

  • Ordering matters in MPI.

  • If you do Recv first on both sides → ❌ deadlock.

  • If you do Send first → ✅ works fine (because MPI buffers the outgoing message until the other side receives it).


Tuesday, 4 March 2025

Design and implement C/C++ Program to sort a given set of n integer elements using Merge Sort method and compute its time complexity. Run the program for varied values of n> 5000, and record the time taken to sort. Plot a graph of the time taken versus n.

 #include <stdio.h>

#include <stdlib.h>

#include <time.h>


// Function to merge two subarrays

void merge(int arr[], int left, int mid, int right) {

    int i, j, k;

    int n1 = mid - left + 1;

    int n2 = right - mid;


    int L[n1], R[n2];


    for (i = 0; i < n1; i++)

        L[i] = arr[left + i];

    for (j = 0; j < n2; j++)

        R[j] = arr[mid + 1 + j];


    i = 0;

    j = 0;

    k = left;

    while (i < n1 && j < n2) {

        if (L[i] <= R[j]) {

            arr[k] = L[i];

            i++;

        } else {

            arr[k] = R[j];

            j++;

        }

        k++;

    }


    while (i < n1) {

        arr[k] = L[i];

        i++;

        k++;

    }


    while (j < n2) {

        arr[k] = R[j];

        j++;

        k++;

    }

}


// Merge Sort function

void mergeSort(int arr[], int left, int right) {

    if (left < right) {

        int mid = left + (right - left) / 2;

        mergeSort(arr, left, mid);

        mergeSort(arr, mid + 1, right);

        merge(arr, left, mid, right);

    }

}


int main() {

    int n;

    printf("Enter number of elements (n > 5000): ");

    scanf("%d", &n);


    if (n <= 5000) {

        printf("Please enter n greater than 5000.\n");

        return 1;

    }


    int *arr = (int *)malloc(n * sizeof(int));

    if (arr == NULL) {

        printf("Memory allocation failed.\n");

        return 1;

    }


    // Generating random numbers

    srand(time(0));

    for (int i = 0; i < n; i++) {

        arr[i] = rand() % 100000; // Random numbers between 0 and 99999

    }


    clock_t start, end;

    double cpu_time_used;


    start = clock();

    mergeSort(arr, 0, n - 1);

    end = clock();


    cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;

    printf("Time taken to sort %d elements: %f seconds\n", n, cpu_time_used);


    free(arr);

    return 0;

}


Tuesday, 23 April 2024

GCD of two numbers and its application...

The greatest common divisor (gcd) of two numbers is the largest positive integer that divides both numbers without leaving a remainder. The gcd can be found using the Euclidean algorithm


GCD is useful in cases when you want different amounts of things to be arranged in the same number of order


For example there are 32 soldiers and 48 bandsman and during the parade you want them to march in the same number of rows


So , you calculate the HCF which is 8 and thus you can make 8 rows each for each group.




Write C++ program to find GCD two numbers a and b using  user defined function. i.e. GCD(a,b) using Euclids algorithm.


 #include <iostream>

using namespace std;

int gcd(int a, int b) {

   if (b == 0)

   return a;

   return gcd(b, a % b);

}

int main() {

   int a , b;

   cout<<"Enter the values of a and b: "<<endl;

   cin>>a>>b;

   cout<<"GCD of "<< a <<" and "<< b <<" is "<< gcd(a, b);

   return 0;

}

Output:

Enter the values of a and b: 

10

20

GCD of 10 and 20 is 10

PC Lab Expt 3:

  Program 3: Calculate Fibonacci Numbers Using OpenMP Tasks Objective To compute the Nth Fibonacci number using OpenMP tasks , where recurs...