TCS-NQT-C-PROGRAM-Kth largest factor of a number
Kth largest factor of a number
A positive integer d is said to be a factor of another positive integer N if when N is divided by d, the remainder obtained is zero. For example, for number 12, there are 6 factors 1, 2, 3, 4, 6, 12. Every positive integer k has at least two factors, 1 and the number k itself. Given two positive integers N and k, write a program to print the kth largest factor of N.
C-Program:
#include <stdio.h>
int main()
{
int num,pos,i,c=0;
scanf("%d",&num);
scanf("%d",&pos);
for(i=num;i>=1;i--)
{
if((num%i)==0)
{
c++;
}
if(c==pos)
{
printf("%d",i);
break;
}
}
if(c<pos)
{
printf("1");
}
return 0;
}
INPUT : 12 3
OUTPUT : 4
Comments
Post a Comment