1. Question:LCM program in c with two numbers 

    Answer
    #include<stdio.h>
    int main(){
      int n1,n2,x,y;
      printf("\nEnter two numbers:");
      scanf("%d %d",&n1,&n2);
      x=n1,y=n2;
      while(n1!=n2){
          if(n1>n2)
               n1=n1-n2;
          else
          n2=n2-n1;
      }
      printf("L.C.M=%d",x*y/n1);
      return 0;
    }
    Alternate Solution:
    #include<stdio.h>
    
    int lcm(int,int);
    
    int main(){
        int a,b,l;
    
        printf("Enter any two positive integers ");
        scanf("%d%d",&a,&b);
     
        if(a>b)
             l = lcm(a,b);
        else
             l = lcm(b,a);
       
        printf("LCM of two integers is %d",l);
    
        return 0;
    }
    
    int lcm(int a,int b){
      
        int temp = a;
    
        while(1){
             if(temp % b == 0 && temp % a == 0)
                 break;
             temp++;
        }
    
       return temp;
    }






    1. Report
  2. Question:LCM program in c with multiple numbers 

    Answer
    #include<stdio.h>
    
    int lcm(int,int);
    
    int main(){
    
       
        int a,b=1;
        printf("Enter positive integers. To quit press zero.");
    
        while(1){
             scanf("%d",&a);
             if(a<1)
                 break;
             else if(a>b)
                 b = lcm(a,b);
             else
                 b = lcm(b,a);
        }
    
        printf("LCM is %d",b);
    
        return 0;
    }
    
    int lcm(int a,int b){
     
        int temp = a;
    
        while(1){
             if(temp % b == 0 && temp % a == 0)
                 break;
             temp++;
        }
    
        return temp;
    }






    1. Report
  3. Question:Write a c program for finding gcd (greatest common divisor) of two given numbers 

    Answer
    #include<stdio.h>
     
    int main(){
    
        int x,y,m,i;
    
        printf("Insert any two number: ");
    
        scanf("%d%d",&x,&y);
        if(x>y)
             m=y;
        else
             m=x;
    
        for(i=m;i>=1;i--){
             if(x%i==0&&y%i==0){
                 printf("\nHCF of two number is : %d",i) ;
                 break;
             }
        }
        return 0;
    }
    Alternate Solution:
    #include<stdio.h>
    int main(){
    int n1,n2;
    printf("\nEnter two numbers:");
    scanf("%d %d",&n1,&n2);
    while(n1!=n2){
    if(n1>=n2-1)
    n1=n1-n2;
    else
    n2=n2-n1;
    }
    printf("\nGCD=%d",n1);
    return 0;
    }






    1. Report
  4. Question:HCF program with multiple numbers in c 

    Answer
    #include<stdio.h>
    int main(){
        int x,y=-1;
        printf("Insert numbers. To exit insert zero: ");
       
        while(1){
             scanf("%d",&x);
             if(x<1)
                 break;
             else if(y==-1)
                 y=x;
             else if (x<y)
                 y=gcd(x,y);
             else
                 y=gcd(y,x);
        }
    
        printf("GCD is %d",y);
       
        return 0;
    }
    
    int gcd(int x,int y){
        int i;
        for(i=x;i>=1;i--){
             if(x%i==0&&y%i==0){
                 break;
             }
        }
        return i;
    }






    1. Report
  5. Question:How to swap two numbers in c without using third variable 

    Answer
    #include<stdio.h>
    
    int main(){
    
        int a=5,b=10;
    
        //process one
        a=b+a;
        b=a-b;
        a=a-b;
        printf("a= %d  b=  %d",a,b);
    
        //process two
        a=5;b=10;
        a=a+b-(b=a);
        printf("\na= %d  b=  %d",a,b);
    
        //process three
        a=5;b=10;
        a=a^b;
        b=a^b;
        a=b^a;
        printf("\na= %d  b=  %d",a,b);
    
        //process four
        a=5;b=10;
        a=b-~a-1;
        b=a+~b+1;
        a=a+~b+1;
        printf("\na= %d  b=  %d",a,b);
    
        //process five
        a=5,b=10;
        a=b+a,b=a-b,a=a-b;
        printf("\na= %d  b=  %d",a,b);
    
        return 0;
    }






    1. Report
Copyright © 2025. Powered by Intellect Software Ltd