Friday, 26 September 2014

Finding co-prime numbers

Write a C program that given an integer ‘n’, prints the number of integers that are less than or equal to ‘n’ and co-prime to ‘n’

Two integers a and b are said to be relatively prime or co-prime if the only positive integer that evenly divides both of them is 1. That is, the only common positive factor of the two numbers is 1. This is equivalent to their greatest common divisor being 1.
Input Format:
One line containing the value of 'n' , where 1<=n<=10,000

Output Format:
One line containing the number of integers that are co-prime to n and less than or equal to 'n'.





#include<stdio.h>

int main()
{int n,j,i,k,no=0;
scanf("%d",&n
);
for(i=1;i<=n;i++)
{k=0;
for(j=1;j<=i;j++)
{
if(i%j==0&&n%j==0)
k++;
}
if(k==1)
no++;
}
printf("%d",no);
return 0;}

1 comment: