You are here

C programs

Matrix multiplication in C

Matrix multiplication in C language: C program to multiply matrices (two-dimensional array), this program multiplies two matrices which will be entered by a user. Firstly user will enter the order of a matrix. If the entered orders of two matrices are such that they can't be multiplied then an error message is displayed on the screen. You have already studied the logic to multiply them in Mathematics. Matrices are frequently used while doing programming and are used to represent graph data structure, in solving system of linear equations and many more.

Pattern programs in C

These program prints various different patterns of numbers and stars. These codes illustrate how to create various patterns using C programming. Most of these C programs involve usage of nested loops and space. A pattern of numbers, star or characters is a way of arranging these in some logical manner or they may form a sequence. Some of these patterns are triangles which have special importance in mathematics. Some patterns are symmetrical while other are not. Please see the complete page and look at comments for many different patterns.

    *
   ***
  *****
 *******
*********

Pascal triangle in C

Pascal Triangle in c: C program to print Pascal triangle which you might have studied in Binomial Theorem in Mathematics. Number of rows of Pascal triangle to print is entered by the user. First four rows of Pascal triangle are shown below :-

   1
  1 1
 1 2 1
1 3 3 1

Pascal triangle in c

#include<stdio.h>
 
long factorial(int);
 
main()
{
   int i, n, c;
 
   printf("Enter the number of rows you wish to see in pascal triangle\n");
   scanf("%d",&n);
 
   for ( i = 0 ; i < n ; i++ )
   {
      for ( c = 0 ; c <= ( n - i - 2 ) ; c++ )

C read file

C read file

How to read a file in C? You already know what a file is and their types (text/binary (audio/images/video etc.)). When a computer is off, they are present on the hard disk.

Suppose you made a C program last week to sort numbers, and you wish to see the program again. How do you do it? You locate the file on the system and open it in an IDE/text editor. That is cool! But what's more interesting is to open the file through your program.

For simplicity, we discuss reading text files only.

Reverse array in C

C program to reverse an array :- This program reverses the array elements. For example,if a is an array of integers with three elements
such that
a[0] = 1
a[1] = 2
a[2] = 3
then on reversing the array will be
a[0] = 3
a[1] = 2
a[0] = 1

Given below is the c code to reverse an array .

C programming code

#include <stdio.h>

int main()
{
   int n, c, d, a[100], b[100];

   printf("Enter the number of elements in array\n");
   scanf("%d", &n);

   printf("Enter the array elements\n");

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

   /*

C program to swap two strings

C program to swap strings.

C programming code

#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<conio.h>

main()
{
   char first[100], second[100], *temp;

   printf("Enter the first string ");
   gets(first);

   printf("Enter the second string ");
   gets(second);

   printf("\nBefore Swapping\n");
   printf("First string: %s\n",first);
   printf("Second string: %s\n\n",second);

   temp = (char*)malloc(100);

   strcpy(temp,first);
   strcpy(first,second);
   strcpy(second,temp);

   printf("After Swapping\n");

Transpose of a matrix in C

Matrix transpose

Transpose of a matrix in C language: This C program prints transpose of a matrix. To obtain it, we interchange rows and columns of the matrix. For example, consider the following 3 X 2 matrix:
1 2
3 4
5 6
Transpose of the matrix:
1 3 5
2 4 6
When we transpose a matrix, its order changes, but for a square matrix, it remains the same.

Pages

Subscribe to RSS - C programs