0% found this document useful (0 votes)
662 views

C Program To Find HCF and LCM Using Recursion

This C program takes in a decimal number as input from the user and converts it to its binary equivalent. It uses a for loop to right shift the decimal number by each bit position from 31 to 0. It then performs a bitwise AND with 1 to check if that bit position is set or not. If the result is 1, it prints 1, otherwise it prints 0 to output the binary number.

Uploaded by

Sri Hari
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
662 views

C Program To Find HCF and LCM Using Recursion

This C program takes in a decimal number as input from the user and converts it to its binary equivalent. It uses a for loop to right shift the decimal number by each bit position from 31 to 0. It then performs a bitwise AND with 1 to check if that bit position is set or not. If the result is 1, it prints 1, otherwise it prints 0 to output the binary number.

Uploaded by

Sri Hari
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

C program to find hcf and lcm using recursion

#include <stdio.h> long gcd(long, long); int main() { long x, y, hcf, lcm; printf("Enter two integers\n"); scanf("%ld%ld", &x, &y); hcf = gcd(x, y); lcm = (x*y)/hcf; printf("Greatest common divisor of %ld and %ld = %ld\n", x, y, hcf); printf("Least common multiple of %ld and %ld = %ld\n", x, y, lcm); return 0; } long gcd(long a, long b) { if (b == 0) { return a; } else { return gcd(b, a % b); } }

C program to convert decimal to binary:


C language code to convert an integer from decimal number system(base-10) to binary number system(base-2). Size of integer is assumed to be 32 bits. We use bitwise operators to perform the desired task. We right shift the original number by 31, 30, 29, ..., 1, 0 bits using a loop and bitwise AND the number obtained with 1(one), if the result is 1 then that bit is 1 otherwise it is 0(zero).
#include <stdio.h> int main() { int n, c, k; printf("Enter an integer in decimal number system\n"); scanf("%d", &n); printf("%d in binary number system is:\n", n);

for (c = 31; c >= 0; c--) { k = n >> c; if (k & 1) printf("1"); else printf("0"); } printf("\n"); return 0; }

You might also like