Category: Programming Examples in C

Program to perform addition of two 2 by 2 matrix 2

Program to perform addition of two 2 by 2 matrix

An array is a collective name given to a group of similar variables. In this program we will accept two 2-D array (2 by 2 Matrix) as input from the user. This program will then perform addition of two 2 by 2 matrix. It will then display the result after adding two matrix. Here is the program for matrix addition. If you found this article useful, do post a comment. #include #include void main(){ int arr1[10][10], arr2[10][10]; int arr3[][3]={ {0,0,0}, {0,0,0}, {0,0,0} }; int i, j; clrscr(); printf(“Enter 3×3 array 1:\n”); for(i=0;i<3;i++){ for(j=0;j<3;j++){ printf(“Enter element %d x %d:”,i,j); scanf(“%d”,&arr1[i][j]); }...

Program to copy the contents of one array into another in the reverse order 7

Program to copy the contents of one array into another in the reverse order

The below mentioned program copies the content of one array name ‘source’ into another array named ‘dest’ in the reverse order. In case you have any queries related to the below mentioned program, please don’t hesitate to comment on this post. Your comments are always welcomed. #include #include void main(){ int source[100], dest[100],i,j,no; clrscr(); printf(“Enter the number of elements: “); scanf(“%d”,&no); for(i=0;i<no;i++){ printf(“Enter Source Array Element%d :”,i+1); scanf(“%d”,&source[i]); } for(i=0,j=no-1;i<no;i++,j–){ dest[j]=source[i]; } printf(“Destination array:\n”); for(i=0;i<no;i++){ printf(“%d\t”,dest[i]); } getch(); } Output: Enter the number of elements: 10 Enter Source Array Element1 :1 Enter Source Array Element2 :2 Enter Source Array Element3...

Demo on Structures in C 12

Demo on Structures in C

The Structure is a special type of C data type. A structure contains a number of data types grouped together. Structure in C allows multiple data types to be grouped together. The below mentioned program is to demonstrate structures in C Programming Language. #include #include void main(){ struct animal{ int age; char name[10]; char gender; }a[10]; int no,i; clrscr(); printf(“Enter the number of animals: “); scanf(“%d”,&no); for(i=0;i<no;i++){ printf(“\nEnter the age of animal %d: “, i+1); scanf(“%d”,&a[i].age); printf(“\nEnter the name of animal %d: “,i+1); scanf(“%s”,&a[i].name); printf(“\nEnter the gender(M/F) of animal %d: “,i+1); scanf(“%s”,&a[i].gender); } for(i=0;i<no;i++){ printf(“\n%d”,a[i].age); printf(“\t%s”,a[i].name); printf(“\t%c”,a[i].gender); } getch(); }...

Pointer Operations Demo in C 12

Pointer Operations Demo in C

Here, we will demonstrate pointer operations in C programming. #include #include void main(){ int arr[5]={12,13,14,15,16}; int *ptrArr, *ptrArr1,i; clrscr(); ptrArr = arr; printf(“\nPrinting the first element of array: %d\n\n”,*ptrArr); /*Pointer variable can be increased. If added 1 to it, it will point to the next element of the same array*/ for(i=0;i<5;i++){ printf(“%d\t”, *ptrArr); ptrArr++; } printf(“\n\n”); /*Now the Pointer is pointing to a memory location that is not reserved so if you will print the value at pointer location it will print garbage and some times it show any RUN TIME error because C compiler will not check array boundaries*/...

sprintf function Demo 2

sprintf function Demo

Understand how sprintf function works in C programming language with the help of an example. Here we demonstrate sprintf function in C language. If you have any questions, do feel free to comment. #include #include void main(){ char *str; char *first=”Learn”, *second = “C”, *third=”Online”; clrscr(); sprintf(str,”%s%s%s.com is a great site”, first, second, third); printf(“%s”,str); getch(); } Output: LearnCOnline.com is a great site Also See: Explanation on sprintf function

sscanf function Demo 1

sscanf function Demo

Understand how sscanf function works in C programming language with the help of an example. Here we demonstrate sscanf function in C language. If you have any questions, do feel free to comment. #include<stdio.h> #include<conio.h> int main(){ char *str = “Learn C Online”; char *first, *second, *third; clrscr(); sscanf(str,”%s %s %s”, first, second, third); printf(“%s”,first); printf(“\n%s”,second); printf(“\n%s”,third); return 0; } Output: Learn C Online Also See: Explanation on sscanf function

FREE C Cheatsheet - Speed Up Your C Programming.

FREE C Cheatsheet - Speed Up Your C Programming.

Download a 7-page free cheat sheet for easy and quick access to C Concepts, Snippets, and Syntax.

Thank you! Check you inbox and access your cheat-sheet