1. Question:How to get division of two very large numbers larger or beyond than long int in c programming language. 

    Answer
    #include<stdio.h>
    #include<string.h>
    #define MAX 10000
    
    int validate(char []);
    char * division(char[],long);
    long int remainder;
    
    int main(){
    
        char dividend[MAX],*quotient;
        long int divisor;
    
        printf("Enter dividend: ");
        scanf("%s",dividend);
        if(validate(dividend))
             return 0;
    
        printf("Enter divisor: ");
        scanf("%ld",&divisor);
    
        quotient = division(dividend,divisor);
    
        while(*quotient)
             if(*quotient ==48)
                 quotient++;
             else
                 break;
    
        printf("Quotient: %s / %ld  =  %s",dividend,divisor,quotient);
        printf ("\nRemainder: %ld",remainder);
        return 0;
    }
    
    int validate(char num[]){
        int i=0;
    
        while(num[i]){
             if(num[i] < 48 || num[i]> 57){
                 printf("Invalid positive integer: %s",num);
                 return 1;
             }
             i++;
        }
    
        return 0;
    }
    
    char * division(char dividend[],long divisor){
       
        static char quotient[MAX];
        long temp=0;
        int i=0,j=0;
    
        while(dividend[i]){
            
             temp = temp*10 + (dividend[i] -48);
             if(temp<divisor){
                
                 quotient[j++] = 48;
                
             }
             else{
                 quotient[j++] = (temp / divisor) + 48;
                 temp = temp % divisor;
    
             }
             i++;
        }
    
        quotient[j] = '\0';
        remainder = temp;
        return quotient;
    }
    Sample output: Enter dividend: 543109237823560187 Enter divisor: 456 Quotient: 543109237823560187 / 456 = 1191029030314824 Remainder: 443Alternate program (source code):
    #include<stdio.h>
    #include<math.h>
    #include<stdlib.h>
    #include<string.h>
    #define MAX 10000
    
    char * division(char [],unsigned long);
    int main(){
        char a[MAX];
        unsigned long b;
        char *c;
        printf("Enter the divdend : ");
        scanf("%s",a);
        printf("Enter the divisor : ");
        scanf("%lu",&b);
        c = division(a,b);
        printf("\nQuotient of the division : ");
        printf("%s",c);
        return 0;
    }
    
    char * division(char a[],unsigned long b){
        static char c[MAX];
        int la;
        int i,k=0,flag=0;
        unsigned long temp=1,reminder;
        la=strlen(a);
    
        for(i=0;i<=la;i++){
             a[i] = a[i] - 48;
        }
    
        temp = a[0];
        reminder = a[0];
        for(i=1;i<=la;i++){
             if(b<=temp){
                 c[k++] = temp/b;
                 temp = temp % b;
                 reminder = temp;
                 temp =temp*10 + a[i];
                 flag=1;
    
             }
             else{
                 reminder = temp;
                 temp =temp*10 + a[i];
                 if(flag==1)
                     c[k++] = 0;
             }
        }
    
        for(i=0;i<k;i++){
             c[i]=c[i]+48;
        }
        c[i]= '\0';
    
        printf("Reminder of division:  %lu  ",reminder);
        return c;
    }
    Sample output: Enter the divdend: 55555555555555555555555555555555555555555 Enter the divisor: 5 Reminder of division: 0 Quotient of the division: 11111111111111111111111111111111111111111

    1. Report
  2. Question:How to get modular division of two very large numbers larger or beyond than long int in c programming language 

    Answer
    #include<stdio.h>
    #include<string.h>
    #define MAX 10000
    
    int validate(char []);
    int modulerDivision(char[],unsigned long);
    
    
    int main(){
    
        char dividend[MAX];
        unsigned long int divisor,remainder;
    
        printf("Enter dividend: ");
        scanf("%s",dividend);
        if(validate(dividend))
             return 0;
    
        printf("Enter divisor: ");
        scanf("%lu",&divisor);
    
        remainder = modulerDivision(dividend,divisor);
    
        printf("Modular division: %s %% %lu  =  %lu",dividend,divisor,remainder);
    
        return 0;
    }
    
    int validate(char num[]){
        int i=0;
    
        while(num[i]){
             if(num[i] < 48 || num[i]> 57){
                 printf("Invalid positive integer: %s",num);
                 return 1;
             }
             i++;
        }
    
        return 0;
    }
    
    int modulerDivision(char dividend[],unsigned long divisor){
       
        unsigned long temp=0;
        int i=0;
    
        while(dividend[i]){
            
             temp = temp*10 + (dividend[i] -48);
             if(temp>=divisor){
                 temp = temp % divisor;
             }
       
             i++;
        }
    
        return temp;
    }
    Sample output: Enter dividend: 123456789 Enter divisor: 56 Modular division: 123456789 % 56 = 29

    1. Report
  3. Question:How to get power of two very large numbers larger or beyond than long int in c programming language 

    Answer
    #include<stdio.h>
    #include<string.h>
    #define MAX 10000
    
    char * multiply(char [],char[]);
    int main(){
        char a[MAX];
        char *c;
        int i,n;
        printf("Enter the base number: ");
        scanf("%s",a);
        printf("Enter the power: ");
        scanf("%d",&n);
    
        printf("Power of the %s^%d: ",a,n);
    
        c = multiply(a,"1");
        for(i=0;i<n-1;i++)
             c = multiply(a,c);
    
        while(*c)
        if(*c =='0')
             c++;
        else
             break;
    
        printf("%s",c);
        return 0;
    }
    
    char * multiply(char num1[],char num2[]){
        static char mul[MAX];
        char a[MAX];
        char b[MAX];
        char c[MAX];
        char temp[MAX];
        int la,lb;
        int i=0,j,k=0,x=0,y;
        long int r=0;
        long sum = 0;
       
    
        while(num1[i]){
             a[i] = num1[i];
             i++;
        }
        a[i]= '\0';
    
        i=0;
        while(num2[i]){
             b[i] = num2[i];
             i++;
        }
        b[i]= '\0';
    
        la=strlen(a)-1;
            lb=strlen(b)-1;
      
            for(i=0;i<=la;i++){
                    a[i] = a[i] - 48;
            }
    
            for(i=0;i<=lb;i++){
                    b[i] = b[i] - 48;
            }
    
        for(i=lb;i>=0;i--){
             r=0;
             for(j=la;j>=0;j--){
                 temp[k++] = (b[i]*a[j] + r)%10;
                 r = (b[i]*a[j]+r)/10;
             }
             temp[k++] = r;
             x++;
             for(y = 0;y<x;y++){
                 temp[k++] = 0;
             }
        }
      
        k=0;
        r=0;
        for(i=0;i<la+lb+2;i++){
             sum =0;
             y=0;
             for(j=1;j<=lb+1;j++){
                 if(i <= la+j){
                     sum = sum + temp[y+i];
                 }
                 y += j + la + 1;
             }
             c[k++] = (sum+r) %10;
             r = (sum+r)/10;
        }
        c[k] = r;
        j=0;
        for(i=k-1;i>=0;i--){
             mul[j++]=c[i] + 48;
        }
        mul[j]='\0';
        return mul;
    }
    Sample output: Enter the base number: 5 Enter the power: 100 Power of the 5^100: 78886090522101180541172856528278622 96732064351090230047702789306640625

    1. Report
  4. Question:C program for atm machine 

    Answer
    #include<stdio.h>
    
    int totalThousand =1000;
    int totalFiveFundred =1000;
    int totalOneHundred =1000;
    
    int main(){
    
        unsigned long withdrawAmount;
        unsigned long totalMoney;
    
        int thousand=0,fiveHundred=0,oneHundred=0;
    
        printf("Enter the amount in multiple of 100: ");
        scanf("%lu",&withdrawAmount);
    
        if(withdrawAmount %100 != 0){
             printf("Invalid amount;");
             return 0;
        }
    
        totalMoney = totalThousand * 1000 + totalFiveFundred* 500 +  totalOneHundred*100;
    
        if(withdrawAmount > totalMoney){
             printf("Sorry,Insufficient money");
             return 0;
        }
    
        thousand = withdrawAmount / 1000;
        if(thousand > totalThousand)
             thousand = totalThousand;
        withdrawAmount = withdrawAmount - thousand * 1000;
    
        if (withdrawAmount > 0){
             fiveHundred = withdrawAmount / 500;
             if(fiveHundred > totalFiveFundred)
                 fiveHundred = totalFiveFundred;
             withdrawAmount = withdrawAmount - fiveHundred * 500;
        }
    
        if (withdrawAmount > 0)
             oneHundred = withdrawAmount / 100;
    
        printf("Total 1000 note: %d\n",thousand);
        printf("Total  500 note: %d\n",fiveHundred);
        printf("Total  100 note: %d\n",oneHundred);
    
        return 0;
    }
    Sample output: Enter the amount in multiple of 100: 7800 Total 1000 note: 7 Total 500 note: 1 Total 100 note: 3

    1. Report
  5. Question:How to pass one dimensional array to function in c 

    Answer
    #include <stdio.h>
    #define N 5
    void fstore1D(int a[], int a_size);
    void fretrieve1D(int a[], int a_size);
    void fedit1D(int a[], int a_size);
    int main(){
    int a[N];
    printf("Input data into the matrix:\n");
    fstore1D(a, N);
    fretrieve1D(a, N);
    fedit1D(a, N);
    fretrieve1D(a, N);
    return 0;
    }
    
    void fstore1D(int a[], int n){
    int i;
    for ( i = 0; i < n; ++i )
    scanf("%d", &a[i]);
    }
    
    void fretrieve1D(int a[], int n){
    int i;
    for ( i = 0; i < n; ++i )
    printf("%6d ", a[i]);
    printf("\n");
    }
    
    void fedit1D(int a[], int n){
    int i, q;
    for ( i = 0; i < n; ++i ){
    printf("Prev. data: %d\nEnter 1 to edit 0 to skip.", a[i]);
    scanf("%d", &q);
    if ( q == 1 ){
    printf("Enter new value: ");
    scanf("%d", &a[i]);
    }
    }
    }

    1. Report
  6. Question:How to pass two dimensional array to a function in c 

    Answer
    #include <stdio.h>
    #define M 3
    #define N 5
    void fstore2D(int a[][N]);
    void fretrieve2D(int a[][N]);
    int main(){
      int a[M][N];
      printf("Input data in matrix (%d X %d)\n", M, N);
      fstore2D(a);
      fretrieve2D(a);
      return 0;
    }
    void fstore2D(int a[][N]){
        int i, j;
        for (i = 0; i < M; ++i){
        for (j = 0; j < N; ++j)
             scanf("%d", &a[i][j]);
        }
    }
    void fretrieve2D(int a[][N]){
       int i, j;
       for ( i = 0; i < M; ++i ){
           for ( j = 0; j < N; ++j)
                printf("%6d ", a[i][j]);
           printf("\n");
       }
    }

    1. Report
  7. Question:Write a c program which takes password from users 

    Answer
    #include<stdio.h>
    #define MAX 500
    
    int main(){
    
        char password[MAX];
        char p;
        int i=0;
    
        printf("Enter the password:");
       
        while((p=getch())!= 13){
             password[i++] = p;
             printf("*");
        }
    
        password[i] = '\0';
        if(i<6)
             printf("\nWeak password");
        printf("\nYou have entered: %s",password);
        return 0;
    }
    Sample output: Enter the password:******* You have entered: fgt67m,

    1. Report
  8. Question:Write a scanf function in c programming language which accept sentence from user 

    Answer
    #include<stdio.h>
    #define MAX 500
    
    int main(){
    
        char arr[MAX];
    
        printf("Enter any sentence which can include spaces.\n");
        printf("To exit press enter key.\n");
        scanf("%[^\n]s",arr);
    
        printf("You had entered: \n");
        printf("%s",arr);
    
        return 0;
    }
    Sample output: Enter any sentence which can include spaces. To exit press enter key. May I help you? You had entered: May I help you?

    1. Report
  9. Question:Write a scanf function in c programming language which accept paragraph from users 

    Answer
    #include<stdio.h>
    #define MAX 500
    
    int main(){
    
        char arr[MAX];
    
        printf("Enter any paragraph which can include spaces or new line.\n");
        printf("To exit press the tab key.\n");
        scanf("%[^\t]s",arr);
    
        printf("You had entered: \n");
        printf("%s",arr);
    
        return 0;
    }
    Sample output: Enter any paragraph which can include spaces or new line. To exit, press the tab key. C is powerful language. I am learning c from cquestionbank.blogspot.com You had entered: C is powerful language. I am learning c from cquestionbank.blogspot.com

    1. Report
  10. Question:Print prime numbers between 1-300 using break and continue in c 

    Answer
    #include <math.h>
    #include <stdio.h>
    main(){
      int i, j;
      i = 2;
      while ( i < 300 ){
         j = 2;
         while ( j < sqrt(i) ){
             if ( i % j == 0 )
                break;
             else{
                ++j;
                continue;
             }
          }
          if ( j > sqrt(i) )
                printf("%d\t", i);
          ++i;
      }
      return 0;
    }

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