PRINTING ASCII VALUE USING C PROGRAM

PRINTING ASCII VALUE USING C PROGRAM



Solution:

#include<stdio.h>
void main(){
  int i;
  for(i=0;i<=255;i++){
            printf("%d -> %c ",i,i);
  }
  return 0;
}

More C Program :


  1. PRINTING ASCII VALUE USING C PROGRAM
  2. write a c program to print ascii value of all characters
  3. how to check ascii value in c
  4. c program to find ascii value of all character
  5. c program to find ascii value of a number
  6. write a c program to print ascii value of given character
  7. c program to read a character and print its ascii value
  8. c program to print ascii value of any character
  9. c program to print ascii value of alphabets

MULTIPLICATION OF TWO MATRICES USING C PROGRAM

MULTIPLICATION OF TWO MATRICES USING C PROGRAM


Solution:

#include<stdio.h>
int main(){
  int a[5][5],b[5][5],c[5][5],i,j,k,sum=0,m,n,o,p;
  printf("\nEnter the row and column of first matrix");
  scanf("%d %d",&m,&n);
  printf("\nEnter the row and column of second matrix");
  scanf("%d %d",&o,&p);
  if(n!=o){
      printf("Matrix mutiplication is not possible");
      printf("\nColumn of first matrix must be same as row of second matrix");
  }
  else{
      printf("\nEnter the First matrix->");
      for(i=0;i<m;i++)
      for(j=0;j<n;j++)
           scanf("%d",&a[i][j]);
      printf("\nEnter the Second matrix->");
      for(i=0;i<o;i++)
      for(j=0;j<p;j++)
           scanf("%d",&b[i][j]);
      printf("\nThe First matrix is\n");
      for(i=0;i<m;i++){
      printf("\n");
      for(j=0;j<n;j++){
           printf("%d\t",a[i][j]);
      }
      }
      printf("\nThe Second matrix is\n");
      for(i=0;i<o;i++){
      printf("\n");
      for(j=0;j<p;j++){
           printf("%d\t",b[i][j]);
      }      
      }
      for(i=0;i<m;i++)
      for(j=0;j<p;j++)
           c[i][j]=0;
      for(i=0;i<m;i++){ //row of first matrix
      for(j=0;j<p;j++){  //column of second matrix
           sum=0;
           for(k=0;k<n;k++)
               sum=sum+a[i][k]*b[k][j];
           c[i][j]=sum;
      }
      }
  }
  printf("\nThe multiplication of two matrix is\n");
  for(i=0;i<m;i++){
      printf("\n");
      for(j=0;j<p;j++){
           printf("%d\t",c[i][j]);
      }
  }
  return 0;
}

More C Program :


  1. MULTIPLICATION OF TWO MATRICES USING C PROGRAM
  2. program for matrix multiplication
  3. c program for multiplication of two matrices using arrays
  4. c program for multiplication of two matrices using pointers
  5. c program for multiplication of two matrices using functions
  6. c program for multiplication of two matrices using dynamic memory allocation
  7. c program to find multiplication of two matrices using arrays
  8. write a c program for multiplication of two matrices using dynamic memory allocation
  9. c program for multiplication of two matrices with output

FIND OUT SUM OF DIAGONAL ELEMENTS OF A MATRIX USING

FIND OUT SUM OF DIAGONAL ELEMENTS OF A MATRIX USING


Solution:


#include<stdio.h>
int main(){
  int a[10][10],i,j,sum=0,m,n;
  printf("\nEnter the row and column of matrix");
  scanf("%d %d",&m,&n);
  printf("\nEnter the First matrix->");
  for(i=0;i<m;i++)
      for(j=0;j<n;j++)
           scanf("%d",&a[i][j]);
  printf("\nThe matrix is\n");
  for(i=0;i<m;i++){
      printf("\n");
      for(j=0;j<m;j++){
      printf("%d\t",a[i][j]);
      }
 }
 for(i=0;i<m;i++){
     for(j=0;j<n;j++){
          if(i==j)
              sum=sum+a[i][j];
     }
 }
 printf("\n\nSum of the diagonal elements of a matrix is -> ");
 printf("%d",sum);
 return 0;
}

More C Program :


  1. FIND OUT SUM OF DIAGONAL ELEMENTS OF A MATRIX USING
  2. sum of both diagonal elements of a matrix in c
  3. sum of diagonal elements of a matrix in java
  4. sum of diagonal elements of a matrix in c++
  5. sum of right diagonal matrix in c
  6. program to calculate sum of diagonals of a matrix
  7. sum of diagonal elements of a matrix in matlab
  8. diagonal elements of a matrix python
  9. sum of diagonal elements of a square matrix

Write A C Program To Find Out Transport Of A Matrix

Write A C Program To Find Out Transport Of A Matrix



Solution:

#include<stdio.h>
int main(){
  int a[10][10],b[10][10],i,j,k=0,m,n;
  printf("\nEnter the row and column of matrix");
  scanf("%d %d",&m,&n);
  printf("\nEnter the First matrix->");
  for(i=0;i<m;i++)
      for(j=0;j<n;j++)
           scanf("%d",&a[i][j]);
  printf("\nThe matrix is\n");
  for(i=0;i<m;i++){
      printf("\n");
      for(j=0;j<m;j++){
           printf("%d\t",a[i][j]);
      }
  }
  for(i=0;i<m;i++)
      for(j=0;j<n;j++)
           b[i][j]=0;
  for(i=0;i<m;i++){
      for(j=0;j<n;j++){
           b[i][j]=a[j][i];
           printf("\n%d",b[i][j]);
      }
  }
  printf("\n\nTraspose of a matrix is -> ");
  for(i=0;i<m;i++){
      printf("\n");
      for(j=0;j<m;j++){
           printf("%d\t",b[i][j]);
      }
  }
  return 0;
}

More C Program :


  1. Write a c program to find out transport of a matrix
  2. c program to find transpose of a matrix without using another matrix
  3. write a program to find transpose of a matrix using function
  4. program to find transpose of a matrix in c++
  5. multiplication of a matrix in c
  6. transpose of a matrix in c without using second matrix
  7. transpose of a matrix in c using pointers
  8. write a program to find the inverse of a matrix
  9. transpose of a 3x3 matrix in c

Factorial of 100 in C Program

Factorial of 100 in C Program


Solution:


#include<stdio.h>
#define MAX 10000
void factorialof(int);
void multiply(int);
int length = 0;
int fact[MAX];

int main(){
    int num;
    int i;

    printf("Enter any integer number : ");
    scanf("%d",&num);
 
    fact[0]=1;

    factorialof(num);
 
    printf("Factorial is : ");
    for(i=length;i>=0;i--){
         printf("%d",fact[i]);
    }
    return 0;
}


More C Program :


  1. Factorial of 100 in c
  2. finding factorial of large numbers in c
  3. how to store 100 factorial in c
  4. algorithm to find factorial of large numbers
  5. factorial of 100 program in c
  6. number of zeros in 100 factorial
  7. factorial of 100 in java
  8. factorial of large numbers in c++
  9. extra long factorials in c

Multiplication of large numbers in c

Multiplication Of Large Numbers In C


Solution:

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#define MAX 10000

char * multiply(char [],char[]);
int main(){
    char a[MAX];
    char b[MAX];
    char *c;
    int la,lb;
    int i;
    printf("Enter the first number : ");
    scanf("%s",a);
    printf("Enter the second number : ");
    scanf("%s",b);
    printf("Multiplication of two numbers : ");
    c = multiply(a,b);
    printf("%s",c);
    return 0;
}

char * multiply(char a[],char b[]){
    static char mul[MAX];
    char c[MAX];
    char temp[MAX];
    int la,lb;
    int i,j,k=0,x=0,y;
    long int r=0;
    long sum = 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;
}


More C Program :


  1. Multiplication of large numbers in c
  2. how to multiply 2 very large numbers in c/c++
  3. c program for multiplication of large numbers
  4. multiplication of large numbers worksheet
  5. multiplication of large numbers tricks
  6. multiplication of large numbers using linked lists
  7. multiplication of large numbers algorithm
  8. multiplication of large numbers using vedic maths
  9. multiplication of large numbers divide and conquer

Division of Large Numbers in C Program

Division of Large Numbers in C Program


Solution:

#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("Remainder of division:  %lu ",reminder);
    return c;
}

More C Program :

Division of Large Numbers in C Program
factorial program in c for large numbers
division of large numbers tricks
division of large numbers shortcut
long division with large numbers
shortcut for division of large numbers pdf
how to do long division with large numbers
shortcut method for division of large numbers
prime numbers c program

BINARY SEARCH USING C PROGRAM

BINARY SEARCH USING C PROGRAM


Solution:

#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("\nEnter the elements of the array->");
  for(i=0;i<n;i++){
      scanf("%d",&a[i]);
  }
  printf("\nThe elements of an array are->");
  for(i=0;i<n;i++){
      printf(" %d",a[i]);
  }
  printf("\nEnter 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("\nThe number is not in the list");
  else
      printf("\nThe number is found");
  return 0;
}

More C Program :


  1. BINARY SEARCH USING C PROGRAM
  2. write a c program for binary search
  3. program to search an element using binary search
  4. linear search program using c
  5. binary search program in c language
  6. binary search algorithm
  7. c program for binary search using recursion
  8. c program for binary search using function
  9. c program for binary search using iteration

BINARY SEARCH THROUGH RECURSION USING C PROGRAM

BINARY SEARCH THROUGH RECURSION USING C PROGRAM



Solution:

#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("\nEnter the elements of the array->");
  for(i=0;i<n;i++){
    scanf("%d",&a[i]);
  }
  printf("\nThe elements of an array are->");
  for(i=0;i<n;i++){
    printf(" %d",a[i]);
  }
  printf("\nEnter the number to be search->");
  scanf("%d",&m);
  l=0,u=n-1;
  c=binary(a,n,m,l,u);
  if(c==0)
    printf("\nThe number is not in the list");
  else
     printf("\nThe 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;
  }

More C Program :


  1. BINARY SEARCH THROUGH RECURSION USING C PROGRAM
  2. write a c program for binary search using recursion
  3. c program to convert decimal to binary using recursion
  4. c program using recursion to search an element in array
  5. c program for merge sort using recursion
  6. c program using recursion for fibonacci
  7. c program using recursion factorial
  8. fibonacci series in c program using recursion
  9. linear search using recursion in c


FIND FACTORIAL OF A NUMBER USING RECURSION IN C PROGRAM

FIND FACTORIAL OF A NUMBER USING RECURSION IN C PROGRAM


Solution:


#include<stdio.h>
int main(){
  int num,f;
  printf("\nEnter a number: ");
  scanf("%d",&num);
  f=fact(num);
  printf("\nFactorial of %d is: %d",num,f);
  return 0;
}

int fact(int n){
   if(n==1)
       return 1;
   else
       return(n*fact(n-1));
 }

More C Program :


  1. FIND FACTORIAL OF A NUMBER USING RECURSION IN C PROGRAM
  2. write a c program to find factorial of a number using recursion
  3. factorial of n numbers using recursion in c
  4. factorial without recursion in c
  5. write a c program to find factorial using recursive function
  6. c program to find fibonacci series using recursion
  7. program to find factorial of a number using recursion in java
  8. program to find factorial of a number using recursion in python
  9. factorial using recursion in java

FIND GCD OF A NUMBER USING RECURSION IN C PROGRAM

FIND GCD OF A NUMBER USING RECURSION IN C PROGRAM



Solution:


#include<stdio.h>
int main(){
  int n1,n2,gcd;
  printf("\nEnter two numbers: ");
  scanf("%d %d",&n1,&n2);
  gcd=findgcd(n1,n2);
  printf("\nGCD of %d and %d is: %d",n1,n2,gcd);
  return 0;
}

int findgcd(int x,int y){
     while(x!=y){
          if(x>y)
              return findgcd(x-y,y);
          else
             return findgcd(x,y-x);
     }
     return x;
}

More C Program :


  1. FIND GCD OF A NUMBER USING RECURSION IN C PROGRAM
  2. write a c program to find gcd of two numbers using recursion
  3. gcd program in c without recursion
  4. gcd of two numbers using recursion in java
  5. gcd using recursion in c language
  6. c program to find factorial of a number using recursion
  7. fibonacci using recursion
  8. write a c program for finding gcd of two given numbers
  9. c program to find gcd of two numbers using functions

FIND SUM OF DIGITS OF A NUMBER USING RECURSION USING C PROGRAM

FIND SUM OF DIGITS OF A NUMBER USING RECURSION USING C PROGRAM


Solution:

#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;
}

More C Program :


  1. FIND SUM OF DIGITS OF A NUMBER USING RECURSION USING C PROGRAM
  2. sum of digits of a number using recursion in java
  3. c program to find sum of n numbers using recursion
  4. sum of digits using recursion in python
  5. c program to find sum of digits of a number using function
  6. c program to find sum of digits of a number using do while loop
  7. c program to find sum of digits of a number until a single digit is obtained
  8. sum of digits using recursion in java
  9. c program for reverse of a number using recursion

FIND POWER OF A NUMBER USING RECURSION USING C PROGRAM

FIND POWER OF A NUMBER USING RECURSION USING C PROGRAM



Solution:

#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;
}

More C Program :


  1. FIND POWER OF A NUMBER USING RECURSION USING C PROGRAM
  2. wap to find power of a number using recursion in c
  3. write a program to compute a^n (a power n) using recursion
  4. write a program to find the power (a b) using recursion
  5. write a c program to find the power of a number using recursion
  6. power function in c using recursion
  7. c program to find factorial of a number using recursion
  8. c program to find reverse of a number using recursion
  9. c program to find prime factors of a number using recursion

REVERSE A NUMBER USING RECURSION IN C PROGRAM

REVERSE A NUMBER USING RECURSION IN C PROGRAM


Solution :

#include<stdio.h>
int main(){
int num,rev;
printf("\nEnter a number :");
scanf("%d",&num);
rev=reverse(num);
printf("\nAfter reverse the no is :%d",rev);
return 0;
}

int sum=0,r;
reverse(int num){
if(num){
r=num%10;
sum=sum*10+r;
reverse(num/10);
}
else
return sum;
return sum;
}

More C Program :


  1. REVERSE A NUMBER USING RECURSION IN C PROGRAM
  2. write a c program to reverse a number using recursion
  3. c program to reverse a string
  4. reverse a number using recursion in java program
  5. c program to reverse a number using for loop
  6. reverse a number using recursion in java
  7. program to reverse a number in c
  8. palindrome of a number in c
  9. c program to reverse a number with explanation

SWAP TWO VARIABLES WITHOUT USING THIRD USING C PROGRAM VARIABLE

SWAP TWO VARIABLES WITHOUT USING THIRD USING C PROGRAM VARIABLE


#include<stdio.h>
int main(){
    int a,b;
    printf("\nEnter two numbers:");
    scanf("%d %d",&a,&b);
    printf("\nBefore swapping a=%d b=%d",a,b);
    a=a^b;
    b=b^a;
    a=a^b;
    printf("\nAfter swapping a=%d b=%d",a,b);
    return 0;
}

OR
Swapping of two number
#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);
    getch();

}


More C Program :


  1. SWAP TWO VARIABLES WITHOUT USING THIRD USING C PROGRAM VARIABLE
  2. c program to swap two numbers without using third variable
  3. c program to swap two numbers without using third variable using functions
  4. program to swap two numbers without using third variable in c language
  5. program to swap two numbers without using third variable in php
  6. program to swap two numbers without using third variable in java
  7. c program to swap two numbers without using temporary variable
  8. write a program to swap two variables without using a temporary variable
  9. c program to swap two numbers using third variable

Write A C Program For Swapping Of Two Arrays

Write a c program for swapping of two arrays


Solution :

#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;
}


More C Program :


  1. Write a c program for swapping of two arrays
  2. c program to swap two numbers using arrays
  3. swapping of two numbers using array in c
  4. write a c program to merge two arrays
  5. write a c program to merge two arrays into a sorted third array
  6. write a c program to swap two numbers without using the third variable
  7. write a c program to swap two numbers using pointers
  8. write a c program to swap two numbers using function
  9. write a c program to swap two numbers using call by value and call by reference

SWAPPING OF STRINGS USING C PROGRAM

SWAPPING OF STRINGS USING C PROGRAM


Solution:

#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;
}

More C Program :


  1. SWAPPING OF STRINGS USING C PROGRAM
  2. c program to swap two strings using pointers
  3. c program to swap two strings without using third variable
  4. c program to swap two strings without using string functions
  5. c program for swapping of two numbers using call by value
  6. c program for swapping of two numbers without using temporary variable
  7. c program for swapping of two numbers using call by reference
  8. c program to sort strings using pointers
  9. c program for swapping 2 numbers

CONVERSION FROM DECIMAL TO OCTAL USING C PROGRAM

CONVERSION FROM DECIMAL TO OCTAL USING C PROGRAM



Solution:

#include<stdio.h>
int main(){
  long int no,n=0,j=1,rem,no1;
  printf("Enter any number any binary form->");
  scanf("%ld",&no);
  no1=no;
  while(no!=0){
      rem=no%10;
      n=n+rem*j;
      j=j*2;
      no=no/10;
  }
  printf("\nThe value of binary no. %ld is ->%ld",no1,n);
  return 0;
}

More C Program :


  1. CONVERSION FROM DECIMAL TO OCTAL USING C PROGRAM
  2. c program to convert decimal to octal without using array
  3. c program to convert decimal to octal using while loop
  4. conversion of decimal to octal number system
  5. conversion of decimal to octal in java
  6. conversion of decimal to octal examples
  7. c program to convert decimal to octal and hexadecimal
  8. write a c program to convert decimal to octal
  9. conversion of binary to octal

CONVERSION FROM DECIMAL TO OCTAL USING C PROGRAM

CONVERSION FROM DECIMAL TO OCTAL USING C PROGRAM


Solution:

#include<stdio.h>
int main(){
  int i=0,j=0,rem=0,a[10],b[10];
  long int num;
  printf("\nEnter a number :");
  scanf("%ld",&num);
  while(num){
    if(num<8){
         a[j++]=num;
         break;
     }
     else{
         a[j++]=num%8;
         num=num/8;
     }
   }
   for(i=j-1;i>=0;i--)
   b[rem++]=a[i];
   printf("\nOctal equivalent :");
   for(j=0;j<rem;j++)
   printf("%d",b[j]);
   return 0;
}

More C Program :


  1. CONVERSION FROM DECIMAL TO OCTAL USING C PROGRAM
  2. c program to convert decimal to octal without using array
  3. c program to convert decimal to octal using while loop
  4. conversion of decimal to octal number system
  5. conversion of decimal to octal in java
  6. conversion of decimal to octal examples
  7. c program to convert decimal to octal and hexadecimal
  8. write a c program to convert decimal to octal
  9. conversion of binary to octal

CONVERSION OF DECIMAL TO BINARY USING C PROGRAM

CONVERSION OF DECIMAL TO BINARY USING C PROGRAM


Solution:


#include<stdio.h>
int main(){
  long int m,no=0,a=1;
  int n,rem;
  printf("Enter any decimal number->");
  scanf("%d",&n);
  m=n;
  while(n!=0){
      rem=n%2;
      no=no+rem*a;
      n=n/2;
     a=a*10;
  }
  printf("The value %ld in binary is->",m);
  printf("%ld",no);
  return 0;
}

More C Program:


  1. CONVERSION OF DECIMAL TO BINARY USING C PROGRAM
  2. c program for decimal to binary conversion without using array
  3. simple c program for decimal to binary conversion
  4. c program to convert decimal to binary without using array
  5. c program to convert decimal to binary using function
  6. c program to convert decimal to binary using stack
  7. c program to convert decimal to binary using bitwise operators
  8. c program to convert decimal to binary using while loop
  9. c program to convert decimal to binary using recursion

CONVERSION OF FAHRENHEIT TO CENTIGRADE USING C PROGRAM

CONVERSION OF FAHRENHEIT TO CENTIGRADE USING C PROGRAM


Solution:

#include<stdio.h>
int main(){
  long int no,n=0,j=1,rem,no1;
  printf("Enter any number any binary form->");
  scanf("%ld",&no);
  no1=no;
  while(no!=0){
      rem=no%10;
      n=n+rem*j;
      j=j*2;
      no=no/10;
  }
  printf("\nThe value of binary no. %ld is ->%ld",no1,n);
  return 0;
}

More C Program:


  1. CONVERSION OF FAHRENHEIT TO CENTIGRADE USING C PROGRAM
  2. conversion fahrenheit to centigrade formula
  3. conversion fahrenheit to centigrade table
  4. conversion of fahrenheit to centigrade temperature
  5. fahrenheit to centigrade conversion calculator
  6. fahrenheit to centigrade conversion chart
  7. c program to convert celsius to fahrenheit using functions
  8. formula for conversion of degree celsius to fahrenheit
  9. c program to convert celsius to fahrenheit and vice versa



C# The Multiplication Of Matrices Of Complex Numbers

C# The multiplication of matrices of complex numbers


Solution:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication14
{
    class complex
    {
        int x, y;
        public complex(int _x, int _y)
        {
            x = _x;
            y = _y;
        }
        public static complex operator*(complex a, complex b)
        {
            return new complex(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
        }
        public static complex operator+(complex a, complex b)
        {
            return new complex(a.x + b.x, a.y + b.y);
        }
        public override string ToString()
        {
            return String.Format("{0} : {1}", x, y);
        }
    }
    class comp_matrix
    {
        int h, w;
        complex[,] x;
        public comp_matrix(int _h, int _w)
        {
            x = new complex[_h, _w];
            h = _h;
            w = _w;
        }
        public complex this[int i, int j]
        {
            set { x[i, j] = value; }
            get { return x[i, j]; }
        }
        public int getH
        {
            set { h = value; }
            get { return h; }
        }
        public int getW
        {
            set { w = value; }
            get { return w; }
        }
        public static comp_matrix operator*(comp_matrix a, comp_matrix b)
        {
            comp_matrix d = new comp_matrix(a.getH, b.getW);
            for (int i = 0; i < a.getH; ++i)
                for (int j = 0; j < b.getW; ++j)
                {
                    d[i, j] = new complex(0, 0);
                    for (int k = 0; k < a.getW; k++)
                    {
                        d[i, j] = d[i, j] + a.x[i, k] * b.x[k, j];
                    }
                }
            return d;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            int a, b, c;
            string s = Console.ReadLine();
            string[] f = s.Split(' ');
            a = int.Parse(f[0]);
            b = int.Parse(f[1]);
            c = int.Parse(f[2]);
            comp_matrix m1 = new comp_matrix(a, b);
            comp_matrix m2 = new comp_matrix(b, c);
            string[] d = new string[2 * b];
            for (int i = 0; i < a; i++)
            {
                s = Console.ReadLine();
                d = s.Split(' ');
                for (int j = 0; j < b; j++)
                {
                    m1[i, j] = new complex(int.Parse(d[j]), int.Parse(d[j * 2 + 1]));
                }
             
            }
            d = new string[2 * c];
            for (int i = 0; i < b; i++)
            {
                s = Console.ReadLine();
                d = s.Split(' ');
                for (int j = 0; j < c; j++)
                {
                    m2[i, j] = new complex(int.Parse(d[j]), int.Parse(d[j * 2 + 1]));
                }
            }

            comp_matrix ans = m1 * m2;
            Console.WriteLine("***********************************************");
            for (int i = 0; i < ans.getH; i++)
            {
                for (int j = 0; j < ans.getW; j++)
                {
                    Console.Write("{0, 10} ", ans[i, j]);
                }
                Console.WriteLine();
            }
        }
    }
}

Tags:

  1. c# complex numbers
  2. c# system.numerics missing
  3. complex number program in c#
  4. c# matrix
  5. math.net numerics
  6. c# array

C or C++ Program To Find Bonus Amount

The company has the following bonus policy for the fiscal year:


The bonus provided will be 5% if the total sale is less than 1, 00,000 and if the sale exceeds that limit then an addition of 3% will be added on the extra sale amount. There will be a taxation of 15% on the total bonus provided. Write a program to compute the bonus amount according to the sale amount.

Solution :

#include<conio.h>
#include<stdio.h>
main()
{
      int sales,x;
      float bonus,bonus1;
      printf("Enter your Total sales\n");
      scanf("%d",&sales);
      if(sales>=100000)
      {x=(sales-100000);
      bonus=5000+(x*3)/100;
      bonus1=bonus-(bonus*15)/100;
      printf("Total Bonus = %.2f",bonus1);}
      else
      {bonus=(sales*5)/100;
      bonus1=bonus-(bonus*15)/100;
      printf("Total bonus= %.2f",bonus);}
      getch();
      }


Tags:


  1. C or C++ program to find bonus amount
  2. c program to check whether a number is perfect or not
  3. c program to check strong number
  4. write a c program to check given number is armstrong number or not.
  5. what is perfect number
  6. c program to find perfect square
  7. c program to find perfect number using function
  8. c program for perfect numbers between 1 and 1000
  9. perfect no program in java

Write own version of the " strlen( ) " library function which is named mystrlen( )

How To Write own version of the " strlen( ) "  library function which is named mystrlen( ) in C Programming Language ?


Solution For Write own version of the " strlen( ) "  library function which is named mystrlen( ):

#include<stdio.h>
int mystrlen(char a[]);
int main(void)
{
    char a[999999];
    gets(a);
    int x=mystrlen(a);
    printf("%d",x);
    return 0;
}
int mystrlen(char a[])
{
    int i,counter=0;
    for(i=0;a[i]!='\0';i++)
    {
        counter++;
    }
    return counter;
}

C Programme To Multiply Two Matrixes

How To Write a C Programme To Multiply Two Matrixes in C Programming Language ?


Solution For C Programme To Multiply Two Matrixes:
#include <stdio.h>
int main()
{
  int m, n, p, q, c, d, k, sum = 0;
  int first[10][10], second[10][10], multiply[10][10];
  printf("Enter the number of rows and columns of first matrix\n");
  scanf("%d%d", &m, &n);
  printf("Enter the elements of first matrix\n");
  for (  c = 0 ; c < m ; c++ )
    for ( d = 0 ; d < n ; d++ )
      scanf("%d", &first[c][d]);
  printf("Enter the number of rows and columns of second matrix\n");
  scanf("%d%d", &p, &q);
  if ( n != p )
    printf("Matrices with entered orders can't be multiplied with each other.\n");
  else
  {
    printf("Enter the elements of second matrix\n");
    for ( c = 0 ; c < p ; c++ )
      for ( d = 0 ; d < q ; d++ )
        scanf("%d", &second[c][d]);
    for ( c = 0 ; c < m ; c++ )
    {
      for ( d = 0 ; d < q ; d++ )
      {
        for ( k = 0 ; k < p ; k++ )
        {
          sum = sum + first[c][k]*second[k][d];
        }
        multiply[c][d] = sum;
        sum = 0;
      }
    }
    printf("Product of entered matrices:-\n");
    for ( c = 0 ; c < m ; c++ )
    {
      for ( d = 0 ; d < q ; d++ )
        printf("%d\t", multiply[c][d]);
      printf("\n");
    }
  }
  return 0;
}
Tags:

  1. C Programme To Multiply Two Matrices
  2. c program to multiply two matrices using function
  3. c program to multiply two matrices using pointers
  4. c program to multiply two matrices using recursion
  5. c program to multiply two matrices by passing matrix to function
  6. c program to multiply two matrices using array of pointers
  7. c program to multiply two matrices using dynamic memory allocation
  8. program to multiply two matrices in c++ using class
  9. write a c program to multiply two matrices using pointers

All Types Of Sorting in C Programming with Example

How To Write a C Program To MERGE SORT, HEAP SORT, QUICK SORT, INSERTION SORT SELECTION SORT and BUBBLE SORT Arrays in C Programming Language ?


Solution For C Program To MERGE SORT, HEAP SORT, QUICK SORT, INSERTION SORT SELECTION SORT and BUBBLE SORT Arrays:


MERGE SORT - Write a c program to merge two sorted arrays:
#include<stdio.h>
main()
{
      int a[10],m,n,b[10],p;
      static int i;
      clrscr();
      printf("enter the size of the first array\n");
      scanf("%d",&m);
      printf("enter the elements into thae array\n");
      for(i=0;i<m;i++)
 scanf("%d",&a[i]);
      printf("enter the size of the second array\n");
      scanf("%d",&n);
      printf("enter the elements into the array\n");
      for(i=0;i<n;i++)
  scanf("%d",&b[i]);
      merge_sort(a,b,m,n);
      getch();
}
merge_sort(int a[],int b[],int m,int n)
{
      int c[25],p;
      static int i,j,k;
      i=0;
      j=0;
      while(i<m && j<n)
      {
  if(a[i]<b[j])
  {
c[k]=a[i];
k++;
i++;
  }
  else if(a[i]>b[j])
  {
c[k]=b[j];
k++;
j++;
  }
  else
  {
c[k]=a[i];
i++;
k++;
  }
      }
      if(i<m)
      {
  for(p=i;p<m;p++)
  {
c[k]=a[i];
k++;
i++;
  }
      }
      else if(j<n)
      {
  for(p=j;p<n;p++)
  {
c[k]=b[j];
k++;
j++;
  }
      }
      printf("after sorting the elements are\n");
      for(i=0;i<k;i++)
  printf("%d ",c[i]);
}
Tags:  c program to merge two sorted arrays, c program to merge two sorted arrays into one sorted array, c program to merge two sorted arrays in ascending order, merge two sorted arrays, merge two sorted arrays in place, merge two sorted arrays algorithm, merge two sorted arrays without extra space, merge two sorted arrays java, merge two sorted arrays without duplicates.

HEAP SORT - C Program to Sort an Array based on Heap Sort Algorithm.

#include<stdio.h>
main()
{
     int a[25],n,i;
     system("clear");
     printf("enter the size of the array\n");
     scanf("%d",&n);
     printf("enter %d elements into the array\n",n);
     for(i=0;i<n;i++)
     {
scanf("%d",&a[i]);
heap(a,i,a[i]);
     }
     heapsort(a,n);
     printf("sorted elements are\n");
     for(i=0;i<n;i++)
     {
printf("%d ",a[i]);
     }
}
heap(int a[],int pos,int k)
{
     int f;
     f=(pos-1)/2;
     while(pos>0 && k>a[f])
     {
a[pos]=a[f];
pos=f;
f=(pos-1)/2;
     }
     a[pos]=k;
}
heapsort(int a[],int n)
{
     int k,key,i,j;
     for(i=n-1;i>0;i--)
     {
key=a[0];
a[0]=a[i];
a[i]=key;
for(j=0;j<i;j++)
{
    heap(a,j,a[j]);
}
     }
}
Tags: c program to HEAPSORT, write a c program for heap sort, heap sort recursive program in c, c program for heap sort using recursion.

QUICK SORT - Write a c program to quick sort an array.

#include<stdio.h>
int n;
main()
{
     int a[20],i,l,r;
     void qsort();
     clrscr();
     printf("enter how many elements do u want to enter: ");
     scanf("%d",&n);
     printf("enter the array elements: ");
     for(i=0;i<n;i++)
     scanf("%d",&a[i]);
     l=0;
     r=n-1;
     qsort(a,l,r);
     printf("final sorted array is: ");
     for(i=0;i<n;i++)
printf("%d  ",a[i]);
}
void qsort(int b[],int left,int right)
{
     int i,j,k,p,temp,finished;
     if(right>left)
     {
 i=left;
 j=right;
 p=b[left];
 printf("\n P,the partioning element is: %d\n",p);
 finished=0;
 while(!finished)
 {
      do
      {
   ++i;
      }while((b[i]<=p)&&(i<=right));
      while((b[j]>=p)&&(j>left))
      {
   --j;
      }
      if(j<i)
   finished=1;
      else
      {
   printf("\nswapping %d and %d. \n",b[i],b[j]);
   temp=b[i];
   b[i]=b[j];
   b[j]=temp;
   for(k=0;k<n;k++)
printf("%5d",b[k]);
   printf("\n");
      }
 }
 printf("\n I is greater than J,");
 printf("so b[left] and b[j] are swapped.");
 printf("Swapping %d and %d.\n\n",b[left],b[j]);
 temp=b[left];
 b[left]=b[j];
 b[j]=temp;
 for(k=0;k<n;k++)
     printf("%5d",b[k]);
 printf("\n");
 printf("\nsub file one is : elements %d to %d.\n",left,j-1);
 qsort(b,left,j-1);
 printf("\nsub file two is : elements %d to %d.\n",i,right);
 qsort(b,i,right);
     }
     else
     {
 if(right!=left)
      printf("Right (%d) is less than Left(%d).\n",right,left);
 else
 {
      printf("Right (%d) is less than Left(%d).\n",right,left);
      printf("subfile has only one element.\n");
 }
     }
     return;
}
Tags: c program to quicksort an array, c program for quicksort using arrays, c program to sort an array using quicksort, quicksort array java, c program for quicksort using divide and conquer, c program for quicksort with explanation, c program for quicksort using recursion, c program for quicksort in data structure, c program for quicksort using divide and conquer method.

INSERTION SORT - Write a c program to INSERTION SORT an array.

#include<stdio.h>
main()
{
     int a[20],i,n;
     clrscr();
     printf("enter how many elements do u want to enter: ");
     scanf("%d",&n);
     printf("enter the array elements: ");
     for(i=0;i<n;i++)
scanf("%d",&a[i]);
     ins_sort(a,n);
     getch();
}
ins_sort(int a[],int n)
{
     int i,j,k;
     for(j=1;j<n;j++)
     {
 k=a[j];
 for(i=j-1;i>=0&&k<a[i];i--)
 {
      a[i+1]=a[i];
 }
 a[i+1]=k;
 printf("element %d inserted in appropriate location",k);
 for(i=0;i<n;i++)
      printf("%d  ",a[i]);
 printf("\n");
     }
     printf("final sorted array is: ");
     for(i=0;i<n;i++)
printf("%d  ",a[i]);
}
Tags:c program to INSERTION SORT an array, c program for insertion sort using array, insertion sort array java, c program for insertion sort with explanation, c program for insertion sort using functions, c program for insertion sort using recursion, c program for insertion sort algorithm, c program for insertion sort using linked list, c program for insertion sort in descending order.

SELECTION SORT - Write a c program to SELECTION SORT an array

#include<stdio.h>
main()
{
     int i,n,a[50];
     clrscr();
     printf("enter how many elements do u want to enter: ");
     scanf("%d",&n);
     printf("enter the array elements: ");
     for(i=0;i<n;i++)
scanf("%d",&a[i]);
     sel_sort(a,n);
     getch();
}
sel_sort(int a[],int n)
{
     int i,j,temp,pass,min;
     for(pass=0;pass<n-1;pass++)
     {
 min=pass;         /*assume that pass is the minimum index*/
 for(j=pass+1;j<n;j++)
 {
     if(a[min]>a[j])   /*update min*/
 min=j;
 }
 if(min!=pass)     /*swaping elements*/
 {
     temp=a[pass];
     a[pass]=a[min];
     a[min]=temp;
 }
     }
     printf("final sorted array is: ");
     for(i=0;i<n;i++)
printf("%d  ",a[i]);
}
Tags: c program to SELECTION SORT an array, c program for selection sort using arrays, write a c program to perform selection sort on an array of n elements, c program for selection sort using functions, c program for selection sort using linked list, c program for selection sort with output, c program for selection sort with explanation, c program for selection sort using pointers, c program for selection sort in descending order.

BUBBLE SORT - Write a c program to BUBBLE SORT an array

#include<stdio.h>
main()
{
     int a[10],i,n;
     clrscr();
     printf("enter how many elements do u want to enter: ");
     scanf("%d",&n);
     printf("enter the array elements: ");
     for(i=0;i<n;i++)
 scanf("%d",&a[i]);
     bub_sort(a,n);
     getch();
}
bub_sort(int a[],int n)
{
     int i,j,temp,limit;
     limit=n-1;
     for(i=0;i<limit;i++)
     {
 for(j=0;j<limit-i;j++)
 {
      if(a[j]>a[j+1])
      {
   temp=a[j];
   a[j]=a[j+1];
   a[j+1]=temp;
      }
 }
     }
     printf("final sorted array is: ");
     for(i=0;i<n;i++)
 printf("%d  ",a[i]);
}
Tags: c program to BUBBLE SORT an array, c program for bubble sort using arrays with explanation, bubble sort array java, c program for bubble sort with output, c program for bubble sort using recursion, c program for bubble sort using pointers, c program for bubble sort using functions, c program for bubble sort algorithm, c program for bubble sort with flag.

Multiplying 100*100 matrix using threads and also produce the sum of the diagonal elements in another matrix

How To Write a C Program To Multiplying 100*100 matrix using threads and also produce the sum of the diagonal elements in another matrix in C Programming Language ?


Solution For Multiplying 100*100 matrix using threads and also produce the sum of the diagonal elements in another matrix :

#include <omp.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
long int a[100][100],c[100][100],b[100][100],d[100][100],i,k,j,val;
int id,ids;
#pragma omp parallel for private(i,j) shared(a)
for(j=0;j<100;j++)
{
for(i=0;i<100;i++)
{
a[i][j]=rand()%999;
}
}

#pragma omp parallel for private(i,j) shared(b)
for(j=0;j<100;j++)
{
for(i=0;i<100;i++)
{
b[i][j]=rand()%999;
}
}

val=0;
#pragma omp parallel for private(i,j,k) shared(a,b,c)
for(k=0;k<100;k++)
{
for(i=0;i<100;i++)
{
for(j=0;j<100;j++)
{
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
} }
}
/*
for(j=0;j<100;j++)
{
for(i=0;i<100;i++)
{
printf("%ld\n",a[i][j]);
}
}
for(j=0;j<100;j++)
{
for(i=0;i<100;i++)
{
printf("%ld\n",b[i][j]);
}
}
*/
#pragma omp parallel for private(i,j) shared(c)
for(j=0;j<100;j++)
{
for(i=0;i<100;i++)
{
printf("%ld\n",c[i][j]);
}
}
#pragma omp critical
#pragma omp parallel for private(i,j) shared(id)
for(i=0;i<100;i++)
{
for(j=0;j<100;j++)
{
ids=omp_get_num_threads();
id=omp_get_thread_num();
if(id%2!=0)
{
while(id%2!=0)
{
ids=omp_get_num_threads();
id=omp_get_thread_num();
}
}
else
{ if(id%2==0)
{
printf("Thread number is %d\n",id);
d[i][j]=a[i][j]+b[i][j];
printf("%ld\n",d[i][j]);
}
else
{
while(id%2!=0)
{
ids=omp_get_num_threads();
id=omp_get_thread_num();
}
}
}
}
}
return 0;
}

Tags:  Multiplying 100*100 matrix using threads and also produce the sum of the diagonal elements in another matrix, matrix multiplication example, matrix addition, matrix vector multiplication, matrix multiplier, matrix division, matrix multiplication rules, matrix transpose, matrix inverse.

Read first 50 char from 4 Scull_Dev using proc (edit main.c)

How To Write a C Program To Read first 50 char from 4 Scull_Dev using proc in C Programming Language ?


Solution For C Program To Read first 50 char from 4 Scull_Dev using proc:

/*
 * main.c -- the bare scull char module
 *
 * Copyright (C) 2001 Alessandro Rubini and Jonathan Corbet
 * Copyright (C) 2001 O'Reilly & Associates
 *
 * The source code in this file can be freely used, adapted,
 * and redistributed in source or binary form, so long as an
 * acknowledgment appears in derived source files.  The citation
 * should list that the code comes from the book "Linux Device
 * Drivers" by Alessandro Rubini and Jonathan Corbet, published
 * by O'Reilly & Associates.   No warranty is attached;
 * we cannot take responsibility for errors or fitness for use.
 *
 */
#ifndef __KERNEL__
#  define __KERNEL__
#endif
#define SCULL_DEBUG
#ifndef MODULE
#  define MODULE
#endif
#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h>   /* printk() */
#include <linux/slab.h>   /* kmalloc() */
#include <linux/fs.h>       /* everything... */
#include <linux/errno.h>    /* error codes */
#include <linux/types.h>    /* size_t */
#include <linux/proc_fs.h>
#include <linux/fcntl.h>    /* O_ACCMODE */
#include <asm/system.h>     /* cli(), *_flags */
#include "scull.h"          /* local definitions */
#define SCULL_DEBUG
/*
 * I don't use static symbols here, because we export no symbols
 */
int scull_major =   SCULL_MAJOR;
int scull_nr_devs = SCULL_NR_DEVS;    /* number of bare scull devices */
int scull_quantum = SCULL_QUANTUM;
int scull_qset =    SCULL_QSET;
MODULE_PARM(scull_major,"i");
MODULE_PARM(scull_nr_devs,"i");
MODULE_PARM(scull_quantum,"i");
MODULE_PARM(scull_qset,"i");
MODULE_AUTHOR("Alessandro Rubini");
Scull_Dev *scull_devices; /* allocated in scull_init_module */
/*
 * Different minors behave differently, so let's use multiple fops
 */

struct file_operations *scull_fop_array[]={
    &scull_fops,      /* type 0 */
    &scull_priv_fops, /* type 1 */
    &scull_pipe_fops, /* type 2 */
    &scull_sngl_fops, /* type 3 */
    &scull_user_fops, /* type 4 */
    &scull_wusr_fops  /* type 5 */
};
#define SCULL_MAX_TYPE 5

int scull_trim(Scull_Dev *dev)
{
    Scull_Dev *next, *dptr;
    int qset = dev->qset;   /* "dev" is not-null */
    int i;
    for (dptr = dev; dptr; dptr = next) { /* all the list items */
        if (dptr->data) {
            for (i = 0; i < qset; i++)
                if (dptr->data[i])
                    kfree(dptr->data[i]);
            kfree(dptr->data);
            dptr->data=NULL;
        }
        next=dptr->next;
        if (dptr != dev) kfree(dptr); /* all of them but the first */
    }
    dev->size = 0;
    dev->quantum = scull_quantum;
    dev->qset = scull_qset;
    dev->next = NULL;
    return 0;
}
#ifdef SCULL_DEBUG /* use proc only if debugging */
/*
 * The proc filesystem: function to read and entry
 */
int scull_read_procmem(char *buf, char **start, off_t offset,
                   int count, int *eof, void *data)
{
    int i, j, len = 0;
    int limit = count - 80; /* Don't print more than this */
    for (i = 0; i < scull_nr_devs && len <= limit; i++) {
        Scull_Dev *d = &scull_devices[i];
        if (down_interruptible(&d->sem))
                return -ERESTARTSYS;
        len += sprintf(buf+len,"\nDevice %i: qset %i, q %i, sz %li\n",
                       i, d->qset, d->quantum, d->size);
        for (; d && len <= limit; d = d->next) { /* scan the list */
            len += sprintf(buf+len, "  item at %p, qset at %p\n", d, d->data);
            if (d->data && !d->next) /* dump only the last item - save space */
                for (j = 0; j < d->qset; j++) {
                    if (d->data[j])
                        len += sprintf(buf+len,"    % 4i: %8p\n",j,d->data[j]);
                }
        }
        up(&scull_devices[i].sem);
    }
    *eof = 1;
    return len;
}
int scull_read_50(char *buf, char **start, off_t offset,
                   int count, int *eof, void *data)
{
    int i, j, len = 0;
    int limit = count - 80; /* Don't print more than this */
    for (i = 0; i < scull_nr_devs && len <= limit; i++) {
        Scull_Dev *d = &scull_devices[i];
     
                for (j = 0; j < 50; j++) {
                 
                        len += sprintf(buf+len,"%c",(*((char*)d->data[0]+j)));
                }
        }
   
    }
    *eof = 1;
    return len;
}
#ifdef USE_PROC_REGISTER
static int scull_get_info(char *buf, char **start, off_t offset,
                int len, int unused)
{
    int eof = 0;
    return scull_read_procmem (buf, start, offset, len, &eof, NULL);
}
struct proc_dir_entry scull_proc_entry = {
        namelen:    8,
        name:       "scullmem",
        mode:       S_IFREG | S_IRUGO,
        nlink:      1,
        get_info:   scull_get_info,
};
static void scull_create_proc()
{
    proc_register_dynamic(&proc_root, &scull_proc_entry);
}
static void scull_remove_proc()
{
    proc_unregister(&proc_root, scull_proc_entry.low_ino);
}
#else  /* no USE_PROC_REGISTER - modern world */
static void scull_create_proc()
{
    create_proc_read_entry("scullmem", 0 /* default mode */,
                           NULL /* parent dir */, scull_read_procmem,
                           NULL /* client data */);
create_proc_read_entry("scull50", 0 /* default mode */,
                           NULL /* parent dir */, scull_read_50,
                           NULL /* client data */);
}
static void scull_remove_proc()
{
    /* no problem if it was not registered */
    remove_proc_entry("scullmem", NULL /* parent dir */);
}

#endif /* USE_PROC_REGISTER */




#endif /* SCULL_DEBUG */




/*
 * Open and close
 */

/* In scull_open, the fop_array is used according to TYPE(dev) */
int scull_open(struct inode *inode, struct file *filp)
{
    Scull_Dev *dev; /* device information */
    int num = NUM(inode->i_rdev);
    int type = TYPE(inode->i_rdev);
    /*
     * the type and num values are only valid if we are not using devfs.
     * However, since we use them to retrieve the device pointer, we
     * don't need them with devfs as filp->private_data is already
     * initialized
     */
    /*
     * If private data is not valid, we are not using devfs
     * so use the type (from minor nr.) to select a new f_op
     */
    if (!filp->private_data && type) {
        if (type > SCULL_MAX_TYPE) return -ENODEV;
        filp->f_op = scull_fop_array[type];
        return filp->f_op->open(inode, filp); /* dispatch to specific open */
    }
    /* type 0, check the device number (unless private_data valid) */
    dev = (Scull_Dev *)filp->private_data;
    if (!dev) {
        if (num >= scull_nr_devs) return -ENODEV;
        dev = &scull_devices[num];
        filp->private_data = dev; /* for other methods */
    }
    MOD_INC_USE_COUNT;  /* Before we maybe sleep */
    /* now trim to 0 the length of the device if open was write-only */
    if ( (filp->f_flags & O_ACCMODE) == O_WRONLY) {
        if (down_interruptible(&dev->sem)) {
            MOD_DEC_USE_COUNT;
            return -ERESTARTSYS;
        }
        scull_trim(dev); /* ignore errors */
        up(&dev->sem);
    }
    return 0;          /* success */
}
int scull_release(struct inode *inode, struct file *filp)
{
    MOD_DEC_USE_COUNT;
    return 0;
}
/*
 * Follow the list
 */
Scull_Dev *scull_follow(Scull_Dev *dev, int n)
{
    while (n--) {
        if (!dev->next) {
            dev->next = kmalloc(sizeof(Scull_Dev), GFP_KERNEL);
            memset(dev->next, 0, sizeof(Scull_Dev));
        }
        dev = dev->next;
        continue;
    }
    return dev;
}
/*
 * Data management: read and write
 */
ssize_t scull_read(struct file *filp, char *buf, size_t count,
                loff_t *f_pos)
{
    Scull_Dev *dev = filp->private_data; /* the first listitem */
    Scull_Dev *dptr;
    int quantum = dev->quantum;
    int qset = dev->qset;
    int itemsize = quantum * qset; /* how many bytes in the listitem */
    int item, s_pos, q_pos, rest;
    ssize_t ret = 0;
    if (down_interruptible(&dev->sem))
            return -ERESTARTSYS;
    if (*f_pos >= dev->size)
        goto out;
    if (*f_pos + count > dev->size)
        count = dev->size - *f_pos;
    /* find listitem, qset index, and offset in the quantum */
    item = (long)*f_pos / itemsize;
    rest = (long)*f_pos % itemsize;
    s_pos = rest / quantum; q_pos = rest % quantum;
    /* follow the list up to the right position (defined elsewhere) */
    dptr = scull_follow(dev, item);
    if (!dptr->data)
        goto out; /* don't fill holes */
    if (!dptr->data[s_pos])
        goto out;
    /* read only up to the end of this quantum */
    if (count > quantum - q_pos)
        count = quantum - q_pos;
    if (copy_to_user(buf, dptr->data[s_pos]+q_pos, count)) {
        ret = -EFAULT;
goto out;
    }
    *f_pos += count;
    ret = count;
 out:
    up(&dev->sem);
    return ret;
}
ssize_t scull_write(struct file *filp, const char *buf, size_t count,
                loff_t *f_pos)
{
    Scull_Dev *dev = filp->private_data;
    Scull_Dev *dptr;
    int quantum = dev->quantum;
    int qset = dev->qset;
    int itemsize = quantum * qset;
    int item, s_pos, q_pos, rest;
    ssize_t ret = -ENOMEM; /* value used in "goto out" statements */
    if (down_interruptible(&dev->sem))
            return -ERESTARTSYS;
    /* find listitem, qset index and offset in the quantum */
    item = (long)*f_pos / itemsize;
    rest = (long)*f_pos % itemsize;
    s_pos = rest / quantum; q_pos = rest % quantum;
    /* follow the list up to the right position */
    dptr = scull_follow(dev, item);
    if (!dptr->data) {
        dptr->data = kmalloc(qset * sizeof(char *), GFP_KERNEL);
        if (!dptr->data)
            goto out;
        memset(dptr->data, 0, qset * sizeof(char *));
    }
    if (!dptr->data[s_pos]) {
        dptr->data[s_pos] = kmalloc(quantum, GFP_KERNEL);
        if (!dptr->data[s_pos])
            goto out;
    }
    /* write only up to the end of this quantum */
    if (count > quantum - q_pos)
        count = quantum - q_pos;
    if (copy_from_user(dptr->data[s_pos]+q_pos, buf, count)) {
        ret = -EFAULT;
goto out;
    }
    *f_pos += count;
    ret = count;
    /* update the size */
    if (dev->size < *f_pos)
        dev-> size = *f_pos;
  out:
    up(&dev->sem);
    return ret;
}
/*
 * The ioctl() implementation
 *
 * This is done twice, once the 2.2 way, followed by the 2.0 way.  One
 * would not normally do things in this manner, but we wanted to illustrate
 * both ways...
 */
#ifndef LINUX_20
int scull_ioctl(struct inode *inode, struct file *filp,
                 unsigned int cmd, unsigned long arg)
{
struct Scull_Dev *dev=filp->private_data;
    int err = 0, tmp;
char temp;
int dev_size=cmd,new_size,i,j;
    int ret = 0;
 
    /*
     * extract the type and number bitfields, and don't decode
     * wrong cmds: return ENOTTY (inappropriate ioctl) before access_ok()
     */
    if (_IOC_TYPE(cmd) != SCULL_IOC_MAGIC) return -ENOTTY;
    if (_IOC_NR(cmd) > SCULL_IOC_MAXNR) return -ENOTTY;
    /*
     * the direction is a bitmask, and VERIFY_WRITE catches R/W
     * transfers. `Type' is user-oriented, while
     * access_ok is kernel-oriented, so the concept of "read" and
     * "write" is reversed
     */
    if (_IOC_DIR(cmd) & _IOC_READ)
        err = !access_ok(VERIFY_WRITE, (void *)arg, _IOC_SIZE(cmd));
    else if (_IOC_DIR(cmd) & _IOC_WRITE)
        err =  !access_ok(VERIFY_READ, (void *)arg, _IOC_SIZE(cmd));
    if (err) return -EFAULT;
    switch(cmd) {
#ifdef SCULL_DEBUG
      case SCULL_IOCHARDRESET:
         /*
          * reset the counter to 1, to allow unloading in case
          * of problems. Use 1, not 0, because the invoking
          * process has the device open.
          */
         while (MOD_IN_USE)
             MOD_DEC_USE_COUNT;
         MOD_INC_USE_COUNT;
         /* don't break: fall through and reset things */
#endif /* SCULL_DEBUG */
      case SCULL_IOCRESET:
        scull_quantum = SCULL_QUANTUM;
        scull_qset = SCULL_QSET;
        break;
     
      case SCULL_IOCSQUANTUM: /* Set: arg points to the value */
        if (! capable (CAP_SYS_ADMIN))
            return -EPERM;
        ret = __get_user(scull_quantum, (int *)arg);
        break;
      case SCULL_IOCTQUANTUM: /* Tell: arg is the value */
        if (! capable (CAP_SYS_ADMIN))
            return -EPERM;
        scull_quantum = arg;
        break;
      case SCULL_IOCGQUANTUM: /* Get: arg is pointer to result */
        ret = __put_user(scull_quantum, (int *)arg);
        break;
      case SCULL_IOCQQUANTUM: /* Query: return it (it's positive) */
        return scull_quantum;
      case SCULL_IOCXQUANTUM: /* eXchange: use arg as pointer */
        if (! capable (CAP_SYS_ADMIN))
            return -EPERM;
        tmp = scull_quantum;
        ret = __get_user(scull_quantum, (int *)arg);
        if (ret == 0)
            ret = __put_user(tmp, (int *)arg);
        break;
      case SCULL_IOCHQUANTUM: /* sHift: like Tell + Query */
        if (! capable (CAP_SYS_ADMIN))
            return -EPERM;
        tmp = scull_quantum;
        scull_quantum = arg;
        return tmp;
     
      case SCULL_IOCSQSET:
        if (! capable (CAP_SYS_ADMIN))
            return -EPERM;
        ret = __get_user(scull_qset, (int *)arg);
        break;
      case SCULL_IOCTQSET:
        if (! capable (CAP_SYS_ADMIN))
            return -EPERM;
        scull_qset = arg;
        break;
      case SCULL_IOCGQSET:
        ret = __put_user(scull_qset, (int *)arg);
        break;
      case SCULL_IOCQQSET:
        return scull_qset;
      case SCULL_IOCXQSET:
        if (! capable (CAP_SYS_ADMIN))
            return -EPERM;
        tmp = scull_qset;
        ret = __get_user(scull_qset, (int *)arg);
        if (ret == 0)
            ret = put_user(tmp, (int *)arg);
        break;
      case SCULL_IOCHQSET:
        if (! capable (CAP_SYS_ADMIN))
            return -EPERM;
        tmp = scull_qset;
        scull_qset = arg;
        return tmp;
        /*
         * The following two change the buffer size for scullpipe.
         * The scullpipe device uses this same ioctl method, just to
         * write less code. Actually, it's the same driver, isn't it?
         */
      case SCULL_P_IOCTSIZE:
        scull_p_buffer = arg;
        break;
      case SCULL_P_IOCQSIZE:
        return scull_p_buffer;
case SCULL_SORTFIRSTQUANT:
if(!dev->data)
return -EAGAIN;
if(!dev->data[0])
return -EAGAIN;
if(dev_size < scull_quantum)
new_size = dev_size;
else
new_size = scull_quantum;
printk("device size %d",dev_size);
for(i=0;i<new_size;i++)
{
for(j=0;j<new_size;j++)
{ if( (*((char*)dev->data[0]+j)) > (*((char*)dev->data[0]+j+1)))
{
temp = (*((char*)dev->data[0]+j));
(*((char*)dev->data[0]+j))= (*((char*)dev->data[0]+j+1));
(*((char*)dev->data[0]+j+1))=temp;
}
}
} break;
      default:  /* redundant, as cmd was checked against MAXNR */
        return -ENOTTY;
    }
    return ret;
}
#else  /* LINUX_20 */
int scull_ioctl(struct inode *inode, struct file *filp,
                 unsigned int cmd, unsigned long arg)
{
    int err = 0, tmp;
 
    /*
     * extract the type and number bitfields, and don't decode
     * wrong cmds: return ENOTTY before verify_area()
     */
    if (_IOC_TYPE(cmd) != SCULL_IOC_MAGIC) return -ENOTTY;
    if (_IOC_NR(cmd) > SCULL_IOC_MAXNR) return -ENOTTY;
    /*
     * the direction is a bitmask, and VERIFY_WRITE catches R/W
     * transfers. `Type' is user-oriented, while
     * verify_area is kernel-oriented, so the concept of "read" and
     * "write" is reversed
     */
    if (_IOC_DIR(cmd) & _IOC_READ)
        err = verify_area(VERIFY_WRITE, (void *)arg, _IOC_SIZE(cmd));
    else if (_IOC_DIR(cmd) & _IOC_WRITE)
        err =  verify_area(VERIFY_READ, (void *)arg, _IOC_SIZE(cmd));
    if (err) return err;
    switch(cmd) {
      case SCULL_IOCRESET:
        scull_quantum = SCULL_QUANTUM;
        scull_qset = SCULL_QSET;
        break;
     
      case SCULL_IOCSQUANTUM: /* Set: arg points to the value */
        scull_quantum = get_user((int *)arg);
        break;
      case SCULL_IOCTQUANTUM: /* Tell: arg is the value */
        scull_quantum = arg;
        break;
      case SCULL_IOCGQUANTUM: /* Get: arg is pointer to result */
        put_user(scull_quantum, (int *)arg);
        break;
      case SCULL_IOCQQUANTUM: /* Query: return it (it's positive) */
        return scull_quantum;
      case SCULL_IOCXQUANTUM: /* eXchange: use arg as pointer */
        tmp = scull_quantum;
        scull_quantum = get_user((int *)arg);
        put_user(tmp, (int *)arg);
        break;
      case SCULL_IOCHQUANTUM: /* sHift: like Tell + Query */
        tmp = scull_quantum;
        scull_quantum = arg;
        return tmp;
     
      case SCULL_IOCSQSET:
        scull_qset = get_user((int *)arg);
        break;
      case SCULL_IOCTQSET:
        scull_qset = arg;
        break;
      case SCULL_IOCGQSET:
        put_user(scull_qset, (int *)arg);
        break;
      case SCULL_IOCQQSET:
        return scull_qset;
      case SCULL_IOCXQSET:
        tmp = scull_qset;
        scull_qset = get_user((int *)arg);
        put_user(tmp, (int *)arg);
        break;
      case SCULL_IOCHQSET:
        tmp = scull_qset;
        scull_quantum = arg;
        return tmp;
        /*
         * The following two change the buffer size for scullpipe.
         * The scullpipe device uses this same ioctl method, just to
         * write less code. Actually, it's the same driver, isn't it?
         */
      case SCULL_P_IOCTSIZE:
        scull_p_buffer = arg;
        break;
      case SCULL_P_IOCQSIZE:
        return scull_p_buffer;

      default:  /* redundant, as cmd was checked against MAXNR */
        return -ENOTTY;
    }
    return 0;
}

#endif /* LINUX_20 */

/*
 * The "extended" operations -- only seek
 */
loff_t scull_llseek(struct file *filp, loff_t off, int whence)
{
    Scull_Dev *dev = filp->private_data;
    loff_t newpos;
    switch(whence) {
      case 0: /* SEEK_SET */
        newpos = off;
        break;
      case 1: /* SEEK_CUR */
        newpos = filp->f_pos + off;
        break;
      case 2: /* SEEK_END */
        newpos = dev->size + off;
        break;
      default: /* can't happen */
        return -EINVAL;
    }
    if (newpos<0) return -EINVAL;
    filp->f_pos = newpos;
    return newpos;
}

/*
 * The following wrappers are meant to make things work with 2.0 kernels
 */
#ifdef LINUX_20
int scull_lseek_20(struct inode *ino, struct file *f,
                off_t offset, int whence)
{
    return (int)scull_llseek(f, offset, whence);
}
int scull_read_20(struct inode *ino, struct file *f, char *buf, int count)
{
    return (int)scull_read(f, buf, count, &f->f_pos);
}
int scull_write_20(struct inode *ino, struct file *f, const char *b, int c)
{
    return (int)scull_write(f, b, c, &f->f_pos);
}
void scull_release_20(struct inode *ino, struct file *f)
{
    scull_release(ino, f);
}
/* Redefine "real" names to the 2.0 ones */
#define scull_llseek scull_lseek_20
#define scull_read scull_read_20
#define scull_write scull_write_20
#define scull_release scull_release_20
#define llseek lseek
#endif  /* LINUX_20 */
struct file_operations scull_fops = {
    llseek:     scull_llseek,
    read:       scull_read,
    write:      scull_write,
    ioctl:      scull_ioctl,
    open:       scull_open,
    release:    scull_release,
};
/*
 * Finally, the module stuff
 */
#ifdef CONFIG_DEVFS_FS
devfs_handle_t scull_devfs_dir;
static char devname[4];
#endif
/*
 * The cleanup function is used to handle initialization failures as well.
 * Thefore, it must be careful to work correctly even if some of the items
 * have not been initialized
 */
void scull_cleanup_module(void)
{
    int i;
#ifndef CONFIG_DEVFS_FS
    /* cleanup_module is never called if registering failed */
    unregister_chrdev(scull_major, "scull");
#endif
#ifdef SCULL_DEBUG /* use proc only if debugging */
    scull_remove_proc();
#endif
    if (scull_devices) {
        for (i=0; i<scull_nr_devs; i++) {
            scull_trim(scull_devices+i);
            /* the following line is only used for devfs */
            devfs_unregister(scull_devices[i].handle);
        }
        kfree(scull_devices);
    }
    /* and call the cleanup functions for friend devices */
    scull_p_cleanup();
    scull_access_cleanup();
    /* once again, only for devfs */
    devfs_unregister(scull_devfs_dir);
}

int scull_init_module(void)
{
    int result, i;
    SET_MODULE_OWNER(&scull_fops);
#ifdef CONFIG_DEVFS_FS
    /* If we have devfs, create /dev/scull to put files in there */
    scull_devfs_dir = devfs_mk_dir(NULL, "scull", NULL);
    if (!scull_devfs_dir) return -EBUSY; /* problem */
#else /* no devfs, do it the "classic" way  */

    /*
     * Register your major, and accept a dynamic number. This is the
     * first thing to do, in order to avoid releasing other module's
     * fops in scull_cleanup_module()
     */
    result = register_chrdev(scull_major, "scull", &scull_fops);
    if (result < 0) {
        printk(KERN_WARNING "scull: can't get major %d\n",scull_major);
        return result;
    }
    if (scull_major == 0) scull_major = result; /* dynamic */
#endif /* CONFIG_DEVFS_FS */
    /*
     * allocate the devices -- we can't have them static, as the number
     * can be specified at load time
     */
    scull_devices = kmalloc(scull_nr_devs * sizeof(Scull_Dev), GFP_KERNEL);
    if (!scull_devices) {
        result = -ENOMEM;
        goto fail;
    }
    memset(scull_devices, 0, scull_nr_devs * sizeof(Scull_Dev));
    for (i=0; i < scull_nr_devs; i++) {
        scull_devices[i].quantum = scull_quantum;
        scull_devices[i].qset = scull_qset;
        sema_init(&scull_devices[i].sem, 1);
#ifdef CONFIG_DEVFS_FS
        sprintf(devname, "%i", i);
        devfs_register(scull_devfs_dir, devname,
                       DEVFS_FL_AUTO_DEVNUM,
                       0, 0, S_IFCHR | S_IRUGO | S_IWUGO,
                       &scull_fops,
                       scull_devices+i);
#endif
    }
    /* At this point call the init function for any friend device */
    if ( (result = scull_p_init()) )
        goto fail;
    if ( (result = scull_access_init()) )
        goto fail;
    /* ... */
#ifndef SCULL_DEBUG
    EXPORT_NO_SYMBOLS; /* otherwise, leave global symbols visible */
#endif
#ifdef SCULL_DEBUG /* only when debugging */
    scull_create_proc();
#endif
    return 0; /* succeed */
  fail:
    scull_cleanup_module();
    return result;
}


module_init(scull_init_module);
module_exit(scull_cleanup_module);

//Be into the folder scull and copy this main.c into that ... and perform this commands
//make clean
//make
//sh scull_unload
//sh scull_load
//cat main.c>/dev/scull0
//cat main.c>/dev/scull1
//cat main.c>/dev/scull2
//cat main.c>/dev/scull3
//cat /proc/scull50

Tags:  C Program To Read first 50 char from 4 Scull_Dev using proc, char driver linux example, scull driver tutorial, scull driver source code, linux file_operations, scull device driver, file operations in linux device driver, mod_inc_use_count, linux file operations commands.