C language

GCD.C
/*  GCD : Greatest Common Divisor
Largest Integer that can divide both the numbers (wihout any remainder)
or with Zero(0) as remainder.
e.g.  30 , 40 : GCD = 10
*/
main()
{
 int n1, n2,  count=2, gcd=0, small=0, lcm=0;
 clrscr();
 printf(“\n Pl. input 2 numbers”);
 flushall();
 scanf(“%d %d”, &n1, &n2);
 small = (n1 < n2) ? n1 : n2;
 while ( count <= small)
{
if( n1 % count == 0 && n2 % count == 0 )
{ gcd = count; }
count++;
}
 // formula to calculate LCM based on GCD.
 lcm = ( n1 * n2 )  / gcd;
 printf(“\n GCD of %d and %d is %d”, n1, n2, gcd);
 printf(“\n LCM of %d and %d is %d”, n1, n2, lcm);
}
LCM.C
/* LCM  : Least Common Multiple
LCM of two integer numbers, is the smallest positive number that
 is perfectly divisible by both the number
 e.g.  LCM of 72 and 120 is 360
 */
main()
{
int n1, n2, max;
clrscr();
printf(“\n Pl. input 2 nos”);
flushall();
scanf(“%d %d”, &n1, &n2);
max =  ( n1 > n2 ) ? n1 : n2 ;
while( 1 )
{
  if  ( ( max % n1  == 0 ) && ( max % n2 == 0 ) )
    {
printf(“\n The LCM of %d and %d is %d “, n1, n2, max);
break;
    }
    max++;
}
}
PELIN.C
//Check if entered number is PELINDROME or not.
// e.g. no = 1221   in reverse = 1221 so it is Pelindrome
// e.g. no = 123 in reverse = 321 so it is not PELINDROME
main()
{
 int no=0, rev=0, rem=0, tmp=0;
 clrscr();
 printf(“\n pl. input a no”); flushall(); scanf(“%d”,&no);
 tmp=no;
 while( no > 0 )
{
rem=no%10;
no=no/10;
rev=rev*10+rem;
}
 if (tmp == rev)
{  printf(“\n %d is PELINDROME”, tmp);  }
 else
{ printf(“\n %d is not PELINDROME”, tmp); }
 getch();
}
REV.C
//Reverse a number
main()
{
 int no=0, rev=0, rem=0;
 clrscr();
 printf(“\n pl. input a no”); flushall(); scanf(“%d”,&no);
 while( no > 0 )
{
rem=no%10;
no=no/10;
rev=rev*10+rem;
}
 printf(“\n reverese = %d”, rev);
 getch();
}
TABLES.C
// input starting no and ending no and print TABLES of the range.
// 10 tables on one screen at a time.
main()
{
 int stno=0, eno=0,step=1, col=1, row=1;
 clrscr();
 col=1;
 printf(“\n pl. input starting number:”); flushall();
 scanf(“%d”, &stno);
 printf(“\n Pl. input ending number”); flushall();
 scanf(“%d”, &eno);
 clrscr();
  for(col=1; col<=79; col++)
 { gotoxy(col,1);  printf(“²”);
   gotoxy(col,25); printf(“²”);
 }
 col=1;
 for(; stno <= eno; stno++ )
{
// print table.
for(step=1; step <= 10; step++)
{
gotoxy(col,row+step);
printf(” %d * %d = %d”, stno, step, stno*step);
}
col+=15;
getch();
if(col > 70 && row==1)
{
col=1; row+=11;
}
else
if(col > 70 && row > 1)
{
gotoxy(1,23);    ;
cprintf(“Press any key to continue”); getch();
row=1; col=1;
clrscr();
}
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *