1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. Question:C code to convert decimal to hexadecimal 

    Answer
    #include<stdio.h>
    int main(){
        long int decimalNumber,remainder,quotient;
        int i=1,j,temp;
        char hexadecimalNumber[100];
    
        printf("Enter any decimal number: ");
        scanf("%ld",&decimalNumber);
    
        quotient = decimalNumber;
    
        while(quotient!=0){
             temp = quotient % 16;
    
          //To convert integer into character
          if( temp < 10)
               temp =temp + 48;
          else
             temp = temp + 55;
    
          hexadecimalNumber[i++]= temp;
          quotient = quotient / 16;
      }
    
        printf("Equivalent hexadecimal value of decimal number %d: ",decimalNumber);
        for(j = i -1 ;j> 0;j--)
          printf("%c",hexadecimalNumber[j]);
    
        return 0;
    }
    Sample output: Enter any decimal number: 45 Equivalent hexadecimal value of decimal number 45: 2D

    1. Report
  10. Question:Easy way to convert decimal number to hexadecimal number 

    Answer
    #include<stdio.h>
    int main(){
    
      long int decimalNumber;
    
      printf("Enter any decimal number: ");
      scanf("%d",&decimalNumber);
    
      printf("Equivalent hexadecimal number is: %X",decimalNumber);
    
      return 0;
    }
    Sample output: Enter any decimal number: 45 Equivalent hexadecimal number is: 2D

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