- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print the kth common factor of two numbers
Given with two numbers x and y the output should contain their kth common factor.
Input: x=9 y=18 k=1 Output : k common factor = 2 Factors of 9 : 1, 3, 9 Factors of 18 : 1, 2, 3, 6, 9, 18 Greatest Common Factor : 9
Algorithm
START Step 1 -: take input as x and y lets say 3 and 21 and k as 1 Step 2 -: declare start variables as int i,num,count=1 Step 3 -: IF x<y Set num=x End IF Step 4 -: Else Set num=y End Else Step 5 -: Loop For i=2and i<=num and i++ IF x % i==0 and y % i == 0 Count =count +1 End If IF count=k Print i End IF Else Return -1 End Else Step 6 -: End Loop For STOP
Example
#include<stdio.h> int main() { int x = 3, y = 21, k = 1; // taking x and y as two number and k is limit for their common factor int i,num,count=1; if(x<y) //fetching smaller value in num[poi num=x; else num=y; for (i=2; i<=num; i++) { //loop from 2 till smaller value if (x % i==0 && y % i == 0) //if remainder is 0 increment count count++; if (count == k) printf("%d",i); else printf("no value as there are less factors than k between x and y "); break; } return 0; }
Output
if we run above program then it will generate following output.
their kth common factor is : 2
- Related Articles
- Finding two numbers given their sum and Highest Common Factor using JavaScript
- Program to find the kth factor of n using Python
- Program to find HCF (Highest Common Factor) of 2 Numbers in C++
- Python program to print all the common elements of two lists.
- C# program to print all the common elements of two lists
- C++ Program for the Common Divisors of Two Numbers?
- C++ Program for Common Divisors of Two Numbers?
- Python Program for Common Divisors of Two Numbers
- Java Program for Common Divisors of Two Numbers
- In rational numbers, do we have to reduce the answer with a common factor?
- Java program to print the Armstrong numbers between two numbers
- Print common characters of two Strings in alphabetical order in C++
- Count common prime factors of two numbers in C++
- Find the lowest common denominator or greatest common factor in Excel
- Python code to print common characters of two Strings in alphabetical order

Advertisements