Search This Blog

Showing posts with label c programming tutorial. Show all posts
Showing posts with label c programming tutorial. Show all posts

C Program to Calculate square of a number using functions

C Program to Calculate square of a number using functions 

This program to calculate square in c allows the user to enter an integer value and then finds the square of that number using Functions.

From the below code snippet, you can see we are using the Calculate_Square function. When the compiler reaches to Calculate_Square(number) line in main() program, the compiler will immediately jump to int Calculate_Square (int Number) function.

Calculate_Square (int Number) function will calculate the square and return the value.

CODE


#include<stdio.h>

int square(int); // function prototype declaration.

void main()
{
     int number, answer;
   
     printf("Enter your number:");
     scanf("%d", &number);
   
     answer = square(number);  //Call function.
   
     printf("Square of %d is %d.", number, answer);
     }

int square(int n)
{
     //function logic is written here..
     return(n*n); //This will return answer to main function.
}

 OUTPUT

 


C program to perform addition, subtraction, multiplication and division

WELCOME BACK TO codejunction.in , Make sure to check out our other blogs .


  • C program to perform basic arithmetic operations which are addition, subtraction, multiplication, and division of two numbers. 
  • Numbers are assumed to be integers and will be entered by a user. In C language when we divide two integers we get an integer as a result, for example, 5/2 evaluates to 2. As a general rule integer/integer = integer and float/integer = float or integer/float = float. 
  • So we convert denominator to float in our program, you may also write float in the numerator. This explicit conversion is known as typecasting.



CODE


 #include <stdio.h>

 int main()
{
   int first, second, add, subtract, multiply;
   float divide;

   printf("Enter two integers\n");
   scanf("%d%d", &first, &second);

   add = first + second;
   subtract = first - second;
   multiply = first * second;
   divide = first / (float)second;   //typecasting

   printf("Sum = %d\n", add);
   printf("Difference = %d\n", subtract);
   printf("Multiplication = %d\n", multiply);
   printf("Division = %.2f\n", divide);

   return 0;
}



OUTPUT

  

  

C program to subtract two matrices

WELCOME BACK TO codejunction.in ,  GLAD TO SEE YOU AGAIN , HOPE YOU ARE ENJOYING OUR BLOGS.


C program to read elements in two matrices and find the difference of two matrices. Program to subtract two matrices in C. Logic to subtract two matrices in C programming.


EXAMPLE:-

Input
Input elements in 3x3 matrix1:
1 2 3
4 5 6
7 8 9

Input elements in 3x3 matrix2:
9 8 7                 
6 5 4
3 2 1

Output
Difference of both matrices =
-8 -6 -4
-2  0  2
4  6  8


Matrix Subtraction
Elements of two matrices can only be subtracted if and only if both matrices are of same size. Matrix subtraction is done element wise (entry wise) i.e. Difference of two matrices A and B of size mXn is defined by
A - B = Aij - Bij (Where 1 ≤ i ≤ m and 1 ≤ j ≤ n)






CODE

#include <stdio.h>

#define SIZE 3 // Size of the matrix

int main()
{
    int A[SIZE][SIZE];  // Matrix 1
    int B[SIZE][SIZE];  // Matrix 2
    int C[SIZE][SIZE];  // Resultant matrix

    int row, col;

    /* Input elements in first matrix */
    printf("Enter elements in matrix A of size 3x3: \n");
    for(row=0; row<SIZE; row++)
    {
        for(col=0; col<SIZE; col++)
        {
            scanf("%d", &A[row][col]);
        }
    }

    /* Input elements in second matrix */
    printf("\nEnter elements in matrix B of size 3x3: \n");
    for(row=0; row<SIZE; row++)
    {
        for(col=0; col<SIZE; col++)
        {
            scanf("%d", &B[row][col]);
        }
    }

    /*
     * Subtract both matrices and store the result in matrix C
     */
    for(row=0; row<SIZE; row++)
    {
        for(col=0; col<SIZE; col++)
        {
            /* Cij = Aij - Bij */
            C[row][col] = A[row][col] - B[row][col];
        }
    }

    /* Print difference of both matrices A and B */
    printf("\nDifference of two matrices A-B = \n");
    for(row=0; row<SIZE; row++)
    {
        for(col=0; col<SIZE; col++)
        {
            printf("%d ", C[row][col]);
        }
        printf("\n");
    }

    return 0;
}



OUTPUT



C Program to Find Transpose of a Matrix

C Program to Find Transpose of a Matrix

WELCOME BACK CODERS , GOOD  TO SEE YOU AGAIN ON OUR BLOGS.

The transpose of a matrix is a new matrix whose rows are the columns of the original. ... Here is a matrix and its transpose: The superscript "T" means "transpose". Another way to look at the transpose is that the element at row r column c in the original is placed at row c column r of the transpose

This program takes a matrix of order r*c from the user and computes the transpose of that matrix.

In this program, user is asked to entered the number of rows r and columns c. The value of r and c should be less than 10 in this program.
The user is asked to enter elements of the matrix (of order r*c).
Then, the program computes the transpose of the matrix and displays it on the screen.

CODE

#include <stdio.h>

int main()
{
    int a[10][10], transpose[10][10], r, c, i, j;
    printf("Enter rows and columns of matrix: ");
    scanf("%d %d", &r, &c);

    // Storing elements of the matrix
    printf("\nEnter elements of matrix:\n");
    for(i=0; i<r; ++i)
        for(j=0; j<c; ++j)
        {
            printf("Enter element a%d%d: ",i+1, j+1);
            scanf("%d", &a[i][j]);
        }

    // Displaying the matrix a[][] */
    printf("\nEntered Matrix: \n");
    for(i=0; i<r; ++i)
        for(j=0; j<c; ++j)
        {
            printf("%d  ", a[i][j]);
            if (j == c-1)
                printf("\n\n");
        }

    // Finding the transpose of matrix a
    for(i=0; i<r; ++i)
        for(j=0; j<c; ++j)
        {
            transpose[j][i] = a[i][j];
        }

    // Displaying the transpose of matrix a
    printf("\nTranspose of Matrix:\n");
    for(i=0; i<c; ++i)
        for(j=0; j<r; ++j)
        {
            printf("%d  ",transpose[i][j]);
            if(j==r-1)
                printf("\n\n");
        }

    return 0;
}


OUTPUT



Matrix multiplication in Language C

WELCOME BACK CODERS ON OUR NEW BLOG. Make sure to check out our other blogs too.


Matrix multiplication in C
Matrix multiplication in C language: C program to multiply two matrices (two-dimensional arrays) which will be entered by a user. The user will enter the order of a matrix and then its elements and similarly inputs the second matrix. If the orders of the matrices are such that they can't be multiplied by each other, then an error message is displayed. You may have studied the method to multiply matrices in Mathematics.

Matrix multiplication in C language

CODE

#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 number of rows and columns of first matrix\n");
  scanf("%d%d", &m, &n);
  printf("Enter elements of first matrix\n");

  for (= 0; c < m; c++)
    for (= 0; d < n; d++)
      scanf("%d", &first[c][d]);

  printf("Enter number of rows and columns of second matrix\n");
  scanf("%d%d", &p, &q);

  if (!= p)
    printf("The matrices can't be multiplied with each other.\n");
  else
  {
    printf("Enter elements of second matrix\n");

    for (= 0; c < p; c++)
      for (= 0; d < q; d++)
        scanf("%d", &second[c][d]);

    for (= 0; c < m; c++) {
      for (= 0; d < q; d++) {
        for (= 0; k < p; k++) {
          sum = sum + first[c][k]*second[k][d];
        }

        multiply[c][d] = sum;
        sum = 0;
      }
    }

    printf("Product of the matrices:\n");

    for (= 0; c < m; c++) {
      for (= 0; d < q; d++)
        printf("%d\t", multiply[c][d]);

      printf("\n");
    }
  }

  return 0;
}

  OUTPUT



Download CEHv9 Study Guide PDF || Certified Ethical Hacker V9 Book

Download CEHv9 Study Guide PDF This book will be a great journey for our users in the field of cyber security . This book contains in ...