First Problem


#include <stdio.h>

int gcd(int a, int b) {

   int q, res;

   q = a % b;

   if (q == 0)
      res = b;
   else
      res = gcd(b,q);

  return(res);
  }

int lcm(int a, int b) {

  return(a*b/gcd(a,b));

}
 
int main() {

   int n,m;

   printf("Enter two positive integers > ");
   scanf("%d %d",&n,&m);
   printf("The least common multiple is %d\n",lcm(n,m));

}

Second Problem


#include <stdio.h>

/* function test returns 1 if the number is prime and 0 if it is composite */

int test( int n){ 

  int p, res;

  if (n == 1) /* 1 is not prime */
    res = 0;
  else
    res = 1;

  p = 2;

  while(p*p <= n) {
    if (n % p == 0) {  /* if n is divisible by p, n is not prime */
      res = 0; 
      break;
    } 
 else
    p = p+1;
  }

  return(res);
}

/* this function counts primes between 1 and n */

int F( int n){
  
   int m, res;

     res = 0;
  
     for (m = 1; m <= n; m++)
       res = res + test(m);

   return(res);
}
  
int main() {
  int n;
    
     printf("Type a positive integer> ");
     scanf("%d",&n);

     printf("The number of primes less or equal to %d is %d\n",n,F(n));
     
}