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
  6. Question:Write a c program for swapping of two arrays 

    Answer
    #include<stdio.h>
    int main(){
      int a[10],b[10],c[10],i;
      printf("Enter First array->");
      for(i=0;i<10;i++)
      scanf("%d",&a[i]);
      printf("\nEnter Second array->");
      for(i=0;i<10;i++)
                scanf("%d",&b[i]);
      printf("Arrays before swapping");
      printf("\nFirst array->");
      for(i=0;i<10;i++){
                printf("%d",a[i]);
      }
      printf("\nSecond array->");
      for(i=0;i<10;i++){
                printf("%d",b[i]);
      }
      for(i=0;i<10;i++){
                //write any swapping technique
                c[i]=a[i];
                a[i]=b[i];
                b[i]=c[i];
      }
      printf("\nArrays after swapping");
      printf("\nFirst array->");
      for(i=0;i<10;i++){
                printf("%d",a[i]);
      }
      printf("\nSecond array->");
      for(i=0;i<10;i++){
                printf("%d",b[i]);
      }
      return 0;
    }

    1. Report
  7. Question:Swapping of strings using c programming language 

    Answer
    #include<stdio.h>
    int main(){
      int i=0,j=0,k=0;
      char str1[20],str2[20],temp[20];
      puts("Enter first string");
      gets(str1);
      puts("Enter second string");
      gets(str2);
      printf("Before swaping the strings are\n");
      puts(str1);
      puts(str2);
      while(str1[i]!='\0'){
                 temp[j++]=str1[i++];
      }
      temp[j]='\0';
      i=0,j=0;
      while(str2[i]!='\0'){
                  str1[j++]=str2[i++];
      }
      str1[j]='\0';
      i=0,j=0;
      while(temp[i]!='\0'){
                  str2[j++]=temp[i++];
      }
      str2[j]='\0';
      printf("After swaping the strings are\n");
      puts(str1);
      puts(str2);
      return 0;
    }

    1. Report
  8. Question:Decimal to binary conversion in c programming language. 

    Answer
    #include<stdio.h>
    
    int main(){
    
        long int decimalNumber,remainder,quotient;
    
        int binaryNumber[100],i=1,j;
    
        printf("Enter any decimal number: ");
    
        scanf("%ld",&decimalNumber);
    
        quotient = decimalNumber;
    
        while(quotient!=0){
    
             binaryNumber[i++]= quotient % 2;
    
             quotient = quotient / 2;
        }
    
        printf("Equivalent binary value of decimal number %d: ",decimalNumber);
    
        for(j = i -1 ;j> 0;j--)
    
             printf("%d",binaryNumber[j]);
    
        return 0;
    }
    Sample output: Enter any decimal number: 50 Equivalent binary value of decimal number 50: 110010

    1. Report
  9. Question:C code for decimal to octal converter 

    Answer
    #include<stdio.h>
    
    int main(){
    
      long int decimalNumber,remainder,quotient;
      int octalNumber[100],i=1,j;
    
      printf("Enter any decimal number: ");
      scanf("%ld",&decimalNumber);
    
      quotient = decimalNumber;
    
      while(quotient!=0){
          octalNumber[i++]= quotient % 8;
          quotient = quotient / 8;
      }
    
      printf("Equivalent octal value of decimal number %d: ",decimalNumber);
      for(j = i -1 ;j> 0;j--)
          printf("%d",octalNumber[j]);
    
      return 0;
    }
    Sample output: Enter any decimal number: 50 Equivalent octal value of decimal number 50: 62

    1. Report
  10. Question:Easy way to convert decimal number to octal number in c 

    Answer
    #include<stdio.h>
    int main(){
    
      long int decimalNumber;
    
      printf("Enter any decimal number : ");
      scanf("%d",&decimalNumber);
    
      printf("Equivalent octal number is: %o",decimalNumber);
    
      return 0;
    }
    Sample output: Enter any decimal number: 25 Equivalent octal number is: 31

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