1. Question:How to convert string to int without using library functions in c programming language 

    Answer
    #include<stdio.h>
    
    int stringToInt(char[] );
    int main(){
      
        char str[10];
        int intValue;
    
        printf("Enter any integer as a string: ");
        scanf("%s",str);
    
       intValue = stringToInt(str);
    
        printf("Equivalent integer value: %d",intValue);
    
        return 0;
    }
    
    int stringToInt(char str[]){
        int i=0,sum=0;
    
        while(str[i]!='\0'){
             if(str[i]< 48 || str[i] > 57){
                 printf("Unable to convert it into integer.\n");
                 return 0;
             }
             else{
                 sum = sum*10 + (str[i] - 48);
                 i++;
             }
        }
        return sum;
    }
    Sample output: Enter any integer as a string: 123 Equivalent integer value: 123

    1. Report
  2. Question:Program in c to print 1 to 100 without using loop 

    Answer
    #include<stdio.h>
    
    int main(){
        int num = 1;
    
        print(num);
    
        return 0;
    }
    int print(num){
        if(num<=100){
             printf("%d ",num);
             print(num+1);
        }
    }
    Sample output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

    1. Report
  3. Question:Code for swapping in c 

    Answer
    #include<stdio.h>
    int main(){
        int a,b,temp;
       
        printf("Enter any two integers: ");
        scanf("%d%d",&a,&b);
        printf("Before swapping: a = %d, b=%d",a,b);
    
        temp = a;
        a = b;
        b = temp;
        printf("\nAfter swapping: a = %d, b=%d",a,b);
    
        return 0;
    }

    1. Report
  4. Question:C program for swapping of two numbers using pointers 

    Answer
    #include<stdio.h>
    
    int main(){
    
        int a,b;
        int *ptra,*ptrb;
        int *temp;
    
        printf("Enter any two integers: ");
        scanf("%d%d",&a,&b);
    
        printf("Before swapping: a = %d, b=%d",a,b);
    
        ptra = &a;
        ptrb = &b;
    
         temp = ptra;
        *ptra = *ptrb;
        *ptrb = *temp;
    
        printf("\nAfter swapping: a = %d, b=%d",a,b);
        return 0;
    }
    Sample output: Enter any two integers: 5 10 Before swapping: a = 5, b=10 After swapping: a = 10, b=10

    1. Report
  5. Question:Swapping program in c using function 

    Answer
    #include<stdio.h>
    
    void swap(int *,int *);
    int main(){
    
        int a,b;
       
        printf("Enter any two integers: ");
        scanf("%d%d",&a,&b);
    
        printf("Before swapping: a = %d, b=%d",a,b);
    
        swap(&a,&b);
    
        printf("\nAfter swapping: a = %d, b=%d",a,b);
        return 0;
    }
    
    void swap(int *a,int *b){
        int *temp;
        temp = a;
        *a=*b;
        *b=*temp;
    }
    Sample output: Enter any two integers: 3 6 Before swapping: a = 3, b=6 After swapping: a = 6, b=6

    1. Report
  6. Question:Simple program of c find the largest number 

    Answer
    #include<stdio.h>
    int main(){
      int n,num,i;
      int big;
      
      printf("Enter the values of n: ");
      scanf("%d",&n);
     
      printf("Number %d",1);
      scanf("%d",&big);
    
      for(i=2;i<=n;i++){
        printf("Number %d: ",i);
        scanf("%d",&num);
    
        if(big<num)
          big=num;
      }
      
      printf("Largest number is: %d",big);
    
      return 0;
    }
    Sample Output: Enter the values of n: Number 1: 12 Number 2: 32 Number 3: 35 Largest number is: 35

    1. Report
  7. Question:Extract digits from integer in c language 

    Answer
    #include<stdio.h>
    int main(){
      int num,temp,factor=1;
    
      printf("Enter a number: ");
      scanf("%d",&num);
    
      temp=num;
      while(temp){
          temp=temp/10;
          factor = factor*10;
      }
    
      printf("Each digits of given number are: ");
      while(factor>1){
          factor = factor/10;
          printf("%d ",num/factor);
          num = num % factor;
      }
    
      return 0;
    }
    Sample output: Enter a number: 123 Each digits of given number are: 1 2 3

    1. Report
  8. Question:Count the number of digits in c programming language 

    Answer
    #include<stdio.h>
    int main(){
      int num,count=0;
    
      printf("Enter a number: ");
      scanf("%d",&num);
    
      while(num){
          num=num/10;
          count++;
      }
      printf("Total digits is:  %d",count);
      return 0;
    }
    Sample output: Enter a number: 23 Total digits is: 2

    1. Report
  9. Question:C code to count the total number of digit using for loop 

    Answer
    #include<stdio.h>
    int main(){
      int num,count=0;
    
      printf("Enter a number: ");
      scanf("%d",&num);
    
      for(;num!=0;num=num/10)
          count++;
    
      printf("Total digits is:  %d",count);
    
      return 0;
    }
    Sample output: Enter a number: 456 Total digits is: 3

    1. Report
  10. Question:Count the digits of a given number in c language using recursion 

    Answer
    #include<stdio.h>
    
    int countDigits(num);
    int main(){
      int num,count;
    
      printf("Enter a number: ");
      scanf("%d",&num);
    
      count = countDigits(num);
    
      printf("Total digits is:  %d",count);
      return 0;
    }
    
    int countDigits(int num){
        static int count=0;
    
         if(num!=0){
              count++;
             countDigits(num/10);
        }
    
        return count;
    }
    Sample output: Enter a number: 1234567 Total digits is: 7

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