C Program to find GCD and LCM of a number

GCD and LCM of a number

This is a c program to find gcd and lcm of two numbers

Program:

#include <stdio.h>
#include <conio.h>
 /*c program to find gcd and lcm of two numbers by Slashmycode.com */
void main()
{
  int n1, n2, lcm, gcd;
  clrsce();
  printf("Enter the two numbers: \n");
  scanf("%d%d",&n1, &n2);
  if(n1>n2)
  {
    lcm=n1;
    gcd=n2;
   }
  else
  {
   lcm=n2;
   gcd=n1;
  }
                 while(lcm%n1!=0 || lcm%n2!=0)
                {
                       lcm++;
                 }
        while(gcd%n1!=0 || gcd%n2!=0)
        {
                gcd--;
         }
  printf("LCM=%d \nGCD=%d",lcm, gcd);
 /*c program to find gcd and lcm of two numbers by Slashmycode.com */
 getch();

}


Output:


Algorithm:
  • Step 1: START
  • Step 2: PRINT"Enter the two numbers:"
  • Step 3: accept n1,n2.
  • Step 4: IF n1<n2 THEN gcd=n1 ELSE gcd=n2.
  • Step 5: IF n1<n2 THEN lcm=n1 ELSE lcm=n2.
  • Step 6: IF (lcm Mode n1 !=0 AND lcm Mode n2 != 0),THEN Go to Step 7. Else Step 10
  • Step 7: lcm=lcm+1.
  • Step 8: IF (gcd Mode n1 != 0 AND gcd Mode n2 != 0),THEN Go to Step 9. Else Step 10
  • Step 9: gcd=gcd-1.
  • Step 10: print "LCM \n GCD"
  • Step 14: STOP
Flowchart:
Flowchart of GCD and LCM of a number



No comments:

Post a Comment

Feel free to ask us any question regarding this post