Posts

‘C’ program to add two matrices of order m X n.

C File => /* Write a ‘C’ program to add two matrices of order mXn */ #include<stdio.h> int main()  {      int m,n,i,j,mat1[20][20];      int mat2[20][20],add[20][20];        printf("\n Enter no. of rows : ");      scanf("%d",&m);      printf("\n Enter no. of cols : ");      scanf("%d",&n);        printf("\n Input Matrix1 : \n");      for(i=0;i<m;i++)     {        for(j=0;j<n;j++)       {            printf("\n Enter element %d%d : ", i+1,j+1);            scanf("%d",&mat1[i][j]);       }    }        printf("\n Input Matrix2 : \n");      for(i=0;i<m;i++)     {         for(j=0;j<n;j++)        {           printf("\n Enter element %d%d : ", i+1,j+1);           scanf("%d",&mat2[i][j]);         }      }         printf("\n\n Addition Matrix is :\n");       for(i=0;i<m;i++)      {         for(j=0;j<n;j++)        {              add[i][j]=mat1[i][

Preprocessor in 'C'

Image
Preprocessor  : As the name suggests a processor which performs pre-processing or some processing on a program before submitting it for actual compilation is called preprocessor.  For example inclusion of header files in a program. Role of Pre-processor : Program written in C language is given to preprocessor.  Preprocessor checks of preprocessor directives if any present in given program. It performs specific actions according to directive instructions and submit modified code to the compiler.  Compiler then compiles it and produces object code which is then executed with the help of linker. Format of preprocessor directive : Preprocessor directive is an instruction given to preprocessor to perform certain task. All preprocessor directives begins with # followed by identifier and directive name respectively.  For example #include ,#define ,#undef etc. In C preprocessor allows us to do inclusion of header files, expansion of macros, c

Assignment III - Basic 'C'

Assignment III Write a ‘C’ program to add two matrices of order m X n. Write a ‘C’ program to check if a n X n matrix is symmetric. Write a ‘C’ program to read a matrix and calculate the sum of its diagonal elements. Write a ‘C’ program to subtract two matrices of order m Xn. Write a ‘C’ program to read a matrix and display its transpose. Write a ‘C’ program to check if a matrix is upper triangular. Write a ‘C’ program with menu to perform the following operations on a character. 1. Check uppercase or lowercase 2. Display its ASCII value 3. Display its next and previous character 4. Exit. Write a menu driven program to perform the following operations on an integer. Write separate functions.  1. Check if is even or odd   2. Check if it is prime   3. Exit Accept two numbers and perform the following operations till the user selects Exit.  1. Maximum    2. Display all numbers between the two   3. Sum and average    4. EXIT Write a function in ‘C’, which accepts

‘C’ program, which accepts annual basic salary of an employee and calculates and displays the Income tax

C File => /* Write a ‘C’ program, which accepts annual basic salary of an employee and calculates and displays the Income tax as per the following rules.   Basic: < 1,50,000 Tax = 0   1,50,000 to 3,00,000 Tax = 20%   > 3,00,000 Tax = 30% */ #include<stdio.h> int main()  {      int sal;      float tax;        printf("\n Enter basic salary : ");      scanf("%d",&sal);        if(sal<150000)          printf("\n Income tax for given salary : 0 Rs.");     else if(sal>150000 && sal<=300000)    {       tax=sal*0.2;       printf("\n Income tax = %f",tax);    }     else if(sal>300000)    {        tax=sal*0.3;        printf("\n Income tax = %f",tax);    }       return 0;  }

Accept two numbers and perform the following operations till the user selects Exit.

C File => /* Accept two numbers and perform the following operations till the user selects Exit.  i. Maximum  ii. Display all numbers between the two  iii. Sum and average  iv. EXIT */ #include<stdio.h> #include<stdlib.h> int main()  {        int n1,n2,i,ch,sum;       float avg;         printf("\n Enter number1 : ");       scanf("%d",&n1);       printf("\n Enter number2 : ");       scanf("%d",&n2);         printf("\n Menu : \n1. Maximum");       printf("\n 2. Display all numbers between %d and %d",n1,n2);       printf("\n 3. Sum and Average of two");       printf("\n 4. Exit\n Enter your choice (1-4) : ");       scanf("%d",&ch);         switch(ch)      {         case 1: if(n1>n2)                           printf("\n Maximum = %d",n1);                       else                           printf("\n Maximum = %d",n2);        break;        case 2: for

Menu driven program to perform operations on an integer.

C File => /* Write a menu driven program to perform the following operations on an  integer. Write separate functions.  1. Check if is even or odd  2. Check if it is prime  3. Exit */ #include<stdio.h> #include<stdlib.h> int main()  {       int n,ch,i;         void chk_even(int n)      {         if(n%2==0)            printf("\n Number is EVEN..");        else           printf("\n Number is ODD...");      }              void chk_prime(int n)      {         int flag=1;         for(i=2;i<n;i++)        {            if(n%i==0)           {               flag=0;               break;            }         }          if(flag==0)              printf("\n Number is NOT prime..");       else             printf("\n Number is PRIME..");       }        printf("\n Enter an integer : ");      scanf("%d",&n);            printf("\n Menu driven operations:\n 1. Check if is even or odd\n  2. Check if it is prime\n 3. Exit&

‘C’ program with menu to perform operations on a character.

C File => /*Write a ‘C’ program with menu to perform the following operations on a  character.  1. Check uppercase or lowercase  2. Display its ASCII value  3. Display its next and previous character  4. Exit */ #include<stdio.h> #include<stdlib.h> #include<ctype.h> int main()  {      int ch;      char c;        printf("\n Enter any character : ");      scanf("%c",&c);      printf("\n Menu : \n1. Check uppercase or lowercase");      printf("\n 2. Display its ASCII value");      printf("\n 3. Display its next and previous character\n 4. Exit");      printf("\n Enter your choice (1-4)");      scanf("%d",&ch);        switch(ch)     {        case 1: if(isupper(c))                           printf("Uppercase character");                      else if(islower(c))                           printf("Lowercase character");                     else                           printf

‘C’ program to check if a matrix is upper triangular.

C File => /* Write a ‘C’ program to check if a matrix is upper triangular. */ #include<stdio.h> int main()  {      int i,j,r,c,mat[30][30],flag=1;        printf("\n Enter number of rows in matrix : ");      scanf("%d",&r);      printf("\n Enter number of cols in matrix : ");      scanf("%d",&c);     //Accepting matrix     for(i=0;i<r;i++)    {        for(j=0;j<c;j++)       {           printf("\n Enter matrix element : ");           scanf("%d",&mat[i][j]);       }     }       //checking upper triangular       for(i=0;i<r;i++)     {         for(j=0;j<c;j++)        {             if(r>c && mat[i][j]!=0)             {                  flag=0;                  break;              }          }       }         if(flag==0)              printf("\n Matrix is NOT upper triangular..");      else             printf("\n Matrix is upper triangular.. ");        return 0; }

‘C’ program to display matrix transpose.

C File => /* Write a ‘C’ program to read a matrix and display its transpose. */ #include<stdio.h> int main()  {      int mat[30][30],trans[30][30],i,j,m,n;        printf("\n Enter how many rows?");      scanf("%d",&m);      printf("\n Enter how many cols?");      scanf("%d",&n);        printf("\n Input matrix");     for(i=0;i<m;i++)    {       for(j=0;j<n;j++)      {         printf("\n Enter element %d%d",i+1,j+1);         scanf("%d",&mat[i][j]);       }     }       printf("\n\n Given matrix is\n");     for(i=0;i<m;i++)    {       for(j=0;j<n;j++)      {          printf("%d\t",mat[i][j]);          trans[j][i]=mat[i][j];       }       printf("\n");     }        printf("\n\n Transpose of given matrix\n");     for(i=0;i<m;i++)    {      for(j=0;j<n;j++)      {         printf("%d\t",trans[i][j]);       }      printf("\n");

‘C’ program to subtract two matrices of order m X n.

C File => /* Write a ‘C’ program to subtract two matrices of order mXn */ #include<stdio.h> int main()  {      int m,n,i,j,mat1[20][20];      int mat2[20][20],sub[20][20];        printf("\n Enter no. of rows : ");      scanf("%d",&m);      printf("\n Enter no. of cols : ");      scanf("%d",&n);      printf("\n Input Matrix1 : \n");      for(i=0;i<m;i++)     {       for(j=0;j<n;j++)      {         printf("\n Enter element %d%d : ",i+1,j+1);         scanf("%d",&mat1[i][j]);       }     }       printf("\n Input Matrix2 : \n");     for(i=0;i<m;i++)    {       for(j=0;j<n;j++)      {          printf("\n Enter element %d%d : ",i+1,j+1);          scanf("%d",&mat2[i][j]);       }     }     printf("\n\n Resultant Matrix is :\n");     for(i=0;i<m;i++)    {        for(j=0;j<n;j++)       {          sub[i][j]=mat1[i][j]-mat2[i][j];          printf(