Printing Prime Numbers and calculating HCF

Problem Statement
1.In main() read two distinct positive integers s and l, s < l. Do not expect that the user
   provides them in any specific order.
2.Compute the sum of the integers ≤ s and not relatively prime to l. Print the sum and the count
   of these integers.
   s l sum count numbers
   10 18 42 7 {2, 3, 4, 6, 8, 9, 10}
    27 55 108 7 {5, 10, 11, 15, 20, 22, 25}
2. Write a recursive function int isPrime(int n, int fp) to test whether the first parameter n, a positive int data, is prime. The function returns 1 if n is a prime, otherwise it
returns 0. When isPrime() is called for the first time, two (the first prime number) is pas sed
as the second parameter. [Your function should not contain any loop construct. You get 50%
credit if you write an iterative function.]
3. Write a non-recursive function int printPrime(int s, int l) that prints all primes
p, so that s ≤ p ≤ l. It returns the count of primes in this range. This function calls the function
isPrime() to test primality. [You get 50% credit if you write a recursive function.]
4. Call printPrime() from main() with s and l as actual parameters. Also print the prime
count in the range s ≤ p ≤ l from main().
s l primes count
10 18 {11, 13, 17} 3
27 55 {29, 31, 37, 41, 43, 47, 53} 7


#include<stdio.h>
int printprime(int s,int l);
int hcf(int i,int l);
int isprime(int s,int i);
int main()
{
  int i,s,l,u,count,sum,temp,y;
  printf("enter the number s and l repectively \n");
  scanf("%d %d",&s,&l);
  if(s>l)            /*here i inteerchange if number are entered in revarse order */
    {
      temp=s;
      s=l;
      l=temp;
    }
  printf("now i print the numbers which are not relative prime to l \n");
  sum=count=0;
  for(i=2;i<=s;i++)             /* calculating the hcf and printing the values */
    {
      u=hcf(i,l);
      if(u!=1)
{
 printf("%d, ",i);
 count=count+1;
 sum=sum+i;
}
    }
  printf("\n the number of such number is %d and sum of such number is %d",count,sum);
  printf("now i print the number which are prime in between s and l \n");
  y= printprime(s,l);
  printf("\n the number of such numbers is i.e.count =  %d \n",y);
  printf("the prograam ends here \n");
  return 0;
}
int isprime(int s,int i)
{
  int res;
  if(s==i)
    {
      return 1;
    }
  else
    {
      if(s%i==0)
{
 res=0;
 return res;
}
      else
{
 i=i+1;
 res= isprime(s,i);
}
   
    }


  return res;
}


int printprime(int s,int l)
{
  int count,i,y;
  count=0;
  for(i=s;i<l+1;i++)
    {
      y=isprime(i,2);
      if(y==1)
{
 printf("%d, ",i);
 count=count+1;
}
    }
  return count;
}
int hcf(int i,int l)
{
  int temp;
  while(l%i!=0)
    {
      temp=l%i;
      l=i;
      i=temp;
    }
  return i;
}

No comments:

Post a Comment