1. Question:Sum of digits in c using recursion 

    Answer
    #include<stdio.h>
    int main(){
      int num,x;
      clrscr();
      printf("\nEnter a number: ");
      scanf("%d",&num);
      x=findsum(num);
      printf("Sum of the digits of %d is: %d",num,x);
      return 0;
    }
    
    int r,s;
    int findsum(int n){
    if(n){
             r=n%10;
             s=s+r;
             findsum(n/10);
         }
         else
           return s;
    }

    1. Report
  2. Question:Find power of a number using recursion using c program 

    Answer
    #include<stdio.h>
    int main(){
      int pow,num;
      long int res;
      long int power(int,int);
      printf("\nEnter a number: ");
      scanf("%d",&num);
      printf("\nEnter power: ");
      scanf("%d",&pow);
      res=power(num,pow);
      printf("\n%d to the power %d is: %ld",num,pow,res);
      return 0;
    }
      int i=1;
      long int sum=1;
      long int power(int num,int pow){
          if(i<=pow){
               sum=sum*num;
              power(num,pow-1);
          }
          else
          return sum;
      }

    1. Report
  3. Question:How to find size of integer data type without using sizeof operator in c programming language 

    Answer
    #include<stdio.h>
    int main(){
      int *ptr = 0;
      ptr++;
      printf("Size of int data type:  %d",ptr);
      return 0;
    }

    1. Report
  4. Question:How to find size of double data type without using sizeof operator in c programming language 

    Answer
    #include<stdio.h>
    int main(){
      double *ptr = 0;
      ptr++;
      printf("Size of double data type:  %d",ptr);
      return 0;
    }

    1. Report
  5. Question:How to find size of structure data type without using sizeof operator in c programming language 

    Answer
    #include<stdio.h>
    struct student{
        int roll;
        char name[100];
        float marks;
    };
    
    int main(){
      struct student *ptr = 0;
      ptr++;
      printf("Size of the structure student:  %d",ptr);
      return 0;
    }

    1. Report
  6. Question:How to find size of union data type without using sizeof operator in c programming language 

    Answer
    #include<stdio.h>
    union student{
        int roll;
        char name[100];
        float marks;
    };
    
    int main(){
      union student *ptr = 0;
      ptr++;
      printf("Size of the union student:  %d",ptr);
      return 0;
    }

    1. Report
  7. Question:Concatenation of two strings using pointer in c programming language 

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

    1. Report
  8. Question:Write a simple code for linear search in c programming language 

    Answer
    #include<stdio.h>
    int main(){
    
        int a[10],i,n,m,c=0;
    
        printf("Enter the size of an array: ");
        scanf("%d",&n);
    
        printf("Enter the elements of the array: ");
        for(i=0;i<=n-1;i++){
             scanf("%d",&a[i]);
        }
    
        printf("Enter the number to be search: ");
        scanf("%d",&m);
        for(i=0;i<=n-1;i++){
             if(a[i]==m){
                 c=1;
                 break;
             }
        }
        if(c==0)
             printf("The number is not in the list");
        else
             printf("The number is found");
    
        return 0;
    }
    Sample output: Enter the size of an array: 5 Enter the elements of the array: 4 6 8 0 3 Enter the number to be search: 0 The number is found

    1. Report
  9. Question:Write a simple code for binary search in c programming language 

    Answer
    #include<stdio.h>
    int main(){
    
        int a[10],i,n,m,c=0,l,u,mid;
    
        printf("Enter the size of an array: ");
        scanf("%d",&n);
    
        printf("Enter the elements in ascending order: ");
        for(i=0;i<n;i++){
             scanf("%d",&a[i]);
        }
    
        printf("Enter the number to be search: ");
        scanf("%d",&m);
    
        l=0,u=n-1;
        while(l<=u){
             mid=(l+u)/2;
             if(m==a[mid]){
                 c=1;
                 break;
             }
             else if(m<a[mid]){
                 u=mid-1;
             }
             else
                 l=mid+1;
        }
        if(c==0)
             printf("The number is not found.");
        else
             printf("The number is found.");
    
        return 0;
    }
    Sample output: Enter the size of an array: 5 Enter the elements in ascending order: 4 7 8 11 21 Enter the number to be search: 11 The number is found.

    1. Report
  10. Question:Write a simple code for binary search using function recursion in c programming language 

    Answer
    #include<stdio.h>
    int main(){
    
        int a[10],i,n,m,c,l,u;
    
        printf("Enter the size of an array: ");
        scanf("%d",&n);
    
        printf("Enter the elements of the array: " );
        for(i=0;i<n;i++){
             scanf("%d",&a[i]);
        }
    
        printf("Enter the number to be search: ");
        scanf("%d",&m);
    
        l=0,u=n-1;
        c=binary(a,n,m,l,u);
        if(c==0)
             printf("Number is not found.");
        else
             printf("Number is found.");
    
        return 0;
     }
    
    int binary(int a[],int n,int m,int l,int u){
    
         int mid,c=0;
    
         if(l<=u){
              mid=(l+u)/2;
              if(m==a[mid]){
                  c=1;
              }
              else if(m<a[mid]){
                  return binary(a,n,m,l,mid-1);
              }
              else
                  return binary(a,n,m,mid+1,u);
         }
         else
           return c;
    }
    Sample output: Enter the size of an array: 5 Enter the elements of the array: 8 9 10 11 12 Enter the number to be search: 8 Number is found.

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