A PHP Error was encountered

Severity: Warning

Message: fopen(/var/cpanel/php/sessions/ea-php56/PHPSESSID8pt8nq6bo3qmr3uto2d8ueth3naflr2e): failed to open stream: No space left on device

Filename: drivers/Session_files_driver.php

Line Number: 174

A PHP Error was encountered

Severity: Warning

Message: session_start(): Cannot send session cookie - headers already sent by (output started at /home/nerd/public_html/system/core/Exceptions.php:271)

Filename: Session/Session.php

Line Number: 144

A PHP Error was encountered

Severity: Warning

Message: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/nerd/public_html/system/core/Exceptions.php:271)

Filename: Session/Session.php

Line Number: 144

A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /home/nerd/public_html/system/core/Exceptions.php:271)

Filename: controllers/Questions.php

Line Number: 268

C program to find the HCF (Highest Common Factor) of given numbers using recursion
Q:

C program to find the HCF (Highest Common Factor) of given numbers using recursion

0

C program to find the HCF (Highest Common Factor) of given numbers using recursion

All Answers

A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /home/nerd/public_html/system/core/Exceptions.php:271)

Filename: questions/question_for_visitors.php

Line Number: 568

need an explanation for this answer? contact us directly to get an explanation for this answer

Read two integer numbers, and find the Highest Common Factor of given numbers.

Program:

The source code to find the HCF (Highest Common Factor) of a given number using recursion is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.

// C program to find the HCF of
// given numbers using recursion

#include <stdio.h>

int calculateHCF(int num1, int num2)
{
    while (num1 != num2) {
        if (num1 > num2)
            return calculateHCF(num1 - num2, num2);
        else
            return calculateHCF(num1, num2 - num1);
    }
    return num1;
}

int main()
{
    int num1 = 0;
    int num2 = 0;

    printf("Enter Number1: ");
    scanf("%d", &num1);

    printf("Enter Number2: ");
    scanf("%d", &num2);

    printf("The Highest Common Factor is: %d\n", calculateHCF(num1, num2));
}

Output:

RUN 1:
Enter Number1: 10
Enter Number2: 20
The Highest Common Factor is: 10

RUN 2:
Enter Number1: 15
Enter Number2: 225
The Highest Common Factor is: 15

RUN 3:
Enter Number1: 117
Enter Number2: 10
The Highest Common Factor is: 1

Explanation:

In the above program, we created two functions calculateHCF() and main(). The calculateHCF() function is a recursive function, which is used to find the Highest Common Factor of specified numbers.

In the main() function, we read two integer numbers num1 and num2 from the user and called the calculateHCF() function, and printed the HCF of given numbers on the console screen.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now

A PHP Error was encountered

Severity: Warning

Message: Unknown: Failed to write session data (user). Please verify that the current setting of session.save_path is correct (/var/cpanel/php/sessions/ea-php56)

Filename: Unknown

Line Number: 0