i want solution of a c prog using functions ie to find lcm and gcd using functions in same program but to find lcm we will be using formula lcm=(a*b)/gcd;
please help me out
this is my code if modifiction are to be done then please send me modified code

gcd(int,int);
lcm(int,int);
void main()
{
    int a,b,gcd1,lcm1;
    clrscr();
    printf("Enter the values for a and b");
    scanf("%d %d",&a,&b);
    gcd1=gcd(a,b);
    lcm1=lcm(a,b);
    printf("\n gcd is %d \n lcm is %d",gcd1,lcm1);
}
gcd(int c,int d)
{
    int r;
    do
    {
        r=c%d;
        c=d;
        d=r;
    }while(r!=0);
    return c;
}
lcm(int a,int b)
{
    int lc;
    lc=(a*b)/gcd1;
    return lc;
}
shoroq.salamh.3 commented: Matching Subscripts In Arrays and For Loop Issues +0
maria.clay.397 commented: declearing a function +0

Recommended Answers

All 5 Replies

Your GCD function will divide by zero when you enter 0 as its second argument. You can also supply negative numbers. You're lacking return types as well.. I assume that you don't feel the need to check your arguments as you have full control over them. You should still assert them in case it somehow does happen. Applying those things result in:

#include <stdio.h>
#include <assert.h>

int gcd(int,int);
int lcm(const int, const int);

int main(void)
{
    int a, b;

    printf("Enter the values for a and b:\n");
    scanf("%d %d", &a, &b);

    printf("gcd is %d.\nlcm is %d.\n", gcd(a,b), lcm(a,b));

    return 0;
}

int gcd (int c, int d)
{
    assert(c > 0 && d > 0);

    int r;

    do
    {
        r = c % d;
        c = d;
        d = r;
    }
    while(r != 0);

    return c;
}


int lcm (const int a, const int b)
{
    assert(a > 0 && b > 0);

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

please send me code asap

I deliberately went to get a coffee before posting on the reply button because of this while I don't even like coffee.

Finally, the GCD is one of those classic algorithms you generally come across when learning about recursive functions. Those functions use the algorithm of Euclides, you can read more of that here: http://en.wikipedia.org/wiki/Greatest_common_divisor#Using_Euclid.27s_algorithm

i want a simple code using simple c functions i'm just learning c so in simple code is it possible

you can use switch case to calculate lcm and gcd in one program

and this is the piece of code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int temp;
int lcm(int no1,int no2)
    {
    if(no1==no2)
        return(temp/no1);
    else if(no1>no2)
        lcm(no1-no2,no2);
    else if(no2>no1)
        lcm(no2,no1);
    }
int gcd(int no1,int no2)
{
    if(no1==no2)
    {
        return(no1);
    }
    else if(no1>no2)
    {
        gcd(no1-no2,no2);
    }
    else if(no2>no1)
    {
        gcd(no2,no1);
    }
}
void main(void)
    {
    int no,choice,i,k;
    int *num;
    int no1,no2,result;//these are for GCD
    while(1)
    {
        clrscr();
        printf("1.LCM\n2.GCD\n3.EXIT");
        printf("\nEnter your choice:");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:

                printf("\nhow many numbers do u want to enter?");
                scanf("%d",&no);
                num=(int *)malloc(sizeof(int)*no);
                for(i=0;i<no;i++)
                {
                    printf("\nenter number:");
                    scanf("%d",&num[i]);
                }
                for(k=1;k<no;k++)
                {
                    temp=num[k-1]*num[k];
                    num[k]=lcm(num[k-1],num[k]);
                }
                printf("The result is:%d",num[no-1]);
                break;
            case 2:
                printf("enter no1:");
                scanf("%d",&no1);
                printf("enter no2:");
                scanf("%d",&no2);
                result=gcd(no1,no2);
                printf("The gcd of %d and %d is:%d",no1,no2,result);
                break;
            case 3:
                exit(0);
            default:
                printf("Wrong Choice");
        }
    getch();
    }
}

i want a simple code using simple c functions i'm just learning c so in simple code is it possible

The code I posted is almost identical to the one you posted yourself. That should be simple for you, unless you copy pasted the code in your starting post and didn't have a clue about it in the first place.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.