109. Program to swap two strings.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char str1[100], str2[100], *temp;
clrscr();
printf("Enter first string : ");
gets(str1);
printf("Enter second string : ");
gets(str2);
printf("\nBefore Swapping : \n");
printf("First string : %s\n", str1);
printf("Second string : %s\n", str2);
temp = (char *) malloc(100);
strcpy(temp, str1);
strcpy(str1, str2);
strcpy(str2, temp);
printf("\nAfter Swapping : \n");
printf("First string : %s\n", str1);
printf("Second string : %s\n", str2);
getch();
}
@C_Codings
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char str1[100], str2[100], *temp;
clrscr();
printf("Enter first string : ");
gets(str1);
printf("Enter second string : ");
gets(str2);
printf("\nBefore Swapping : \n");
printf("First string : %s\n", str1);
printf("Second string : %s\n", str2);
temp = (char *) malloc(100);
strcpy(temp, str1);
strcpy(str1, str2);
strcpy(str2, temp);
printf("\nAfter Swapping : \n");
printf("First string : %s\n", str1);
printf("Second string : %s\n", str2);
getch();
}
@C_Codings
110. Program to add two matrix.
#include<stdio.h>
#include<conio.h>
void main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
clrscr();
printf("Enter the number of rows and columns of matrix :");
scanf("%d%d", &m, &n);
printf("\nEnter the elements of first matrix : \n");
for (c = 0; c < m; c++)
{
for (d = 0; d < n; d++)
{
scanf("%d", &first[c][d]);
}
}
printf("\nEnter the elements of second matrix : \n");
for (c = 0; c < m; c++)
{
for (d = 0; d < n; d++)
{
scanf("%d", &second[c][d]);
}
}
printf("\nSum of entered matrices : \n");
for (c = 0; c < m; c++)
{
for (d = 0; d < n; d++)
{
sum[c][d] = first[c][d] + second[c][d];
printf("%d\t", sum[c][d]);
}
printf("\n");
}
getch();
}
@C_Codings
#include<stdio.h>
#include<conio.h>
void main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
clrscr();
printf("Enter the number of rows and columns of matrix :");
scanf("%d%d", &m, &n);
printf("\nEnter the elements of first matrix : \n");
for (c = 0; c < m; c++)
{
for (d = 0; d < n; d++)
{
scanf("%d", &first[c][d]);
}
}
printf("\nEnter the elements of second matrix : \n");
for (c = 0; c < m; c++)
{
for (d = 0; d < n; d++)
{
scanf("%d", &second[c][d]);
}
}
printf("\nSum of entered matrices : \n");
for (c = 0; c < m; c++)
{
for (d = 0; d < n; d++)
{
sum[c][d] = first[c][d] + second[c][d];
printf("%d\t", sum[c][d]);
}
printf("\n");
}
getch();
}
@C_Codings
111. Program to arrange array numbers in ascending order.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, a, n, number[30];
clrscr();
printf("Enter total numbers : ");
scanf("%d", &n);
printf("\n Enter the numbers :\n");
for (i = 0; i < n; ++i)
{
scanf("%d", &number[i]);
}
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (number[i] > number[j])
{
a = number[i];
number[i] = number[j];
number[j] = a;
}
}
}
printf("The numbers arranged in ascending order are :\n");
for (i = 0; i < n; ++i)
{
printf("%d\n", number[i]);
}
getch();
}
@C_Codings
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, a, n, number[30];
clrscr();
printf("Enter total numbers : ");
scanf("%d", &n);
printf("\n Enter the numbers :\n");
for (i = 0; i < n; ++i)
{
scanf("%d", &number[i]);
}
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (number[i] > number[j])
{
a = number[i];
number[i] = number[j];
number[j] = a;
}
}
}
printf("The numbers arranged in ascending order are :\n");
for (i = 0; i < n; ++i)
{
printf("%d\n", number[i]);
}
getch();
}
@C_Codings
112. Program to check whether the matrix is sparse matrix or not.
#include<stdio.h>
#include<conio.h>
void main()
{
static int array[10][10];
int i, j, m, n;
int counter = 0;
clrscr();
printf("Enter the order of the matix : ");
scanf("%d %d", &m, &n);
printf("\nEnter the co-efficients of the matix :\n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array[i][j]);
if (array[i][j] == 0)
{
++counter;
}
}
}
if (counter > ((m * n) / 2))
{
printf("The given matrix is sparse matrix \n");
}
else
{
printf("The given matrix is not a sparse matrix \n");
}
printf("There are %d number of zeros", counter);
getch();
}
@C_Codings
#include<stdio.h>
#include<conio.h>
void main()
{
static int array[10][10];
int i, j, m, n;
int counter = 0;
clrscr();
printf("Enter the order of the matix : ");
scanf("%d %d", &m, &n);
printf("\nEnter the co-efficients of the matix :\n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array[i][j]);
if (array[i][j] == 0)
{
++counter;
}
}
}
if (counter > ((m * n) / 2))
{
printf("The given matrix is sparse matrix \n");
}
else
{
printf("The given matrix is not a sparse matrix \n");
}
printf("There are %d number of zeros", counter);
getch();
}
@C_Codings
113. Program to delete an element from array.
#include<stdio.h>
#include<conio.h>
void main()
{
int array[100], position, c, n;
clrscr();
printf("Enter total number of elements in array : ");
scanf("%d", &n);
printf("\nEnter element %d : ", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("\nEnter the location from where you wish to delete element : ");
scanf("%d", &position);
if (position >= n + 1)
printf("\nDeletion not possible.");
else
{
for (c = position - 1; c < n - 1; c++)
array[c] = array[c + 1];
printf("\nResultant array is :");
for (c = 0; c < n - 1; c++)
printf("\n%d", array[c]);
}
getch();
}
@C_Codings
#include<stdio.h>
#include<conio.h>
void main()
{
int array[100], position, c, n;
clrscr();
printf("Enter total number of elements in array : ");
scanf("%d", &n);
printf("\nEnter element %d : ", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("\nEnter the location from where you wish to delete element : ");
scanf("%d", &position);
if (position >= n + 1)
printf("\nDeletion not possible.");
else
{
for (c = position - 1; c < n - 1; c++)
array[c] = array[c + 1];
printf("\nResultant array is :");
for (c = 0; c < n - 1; c++)
printf("\n%d", array[c]);
}
getch();
}
@C_Codings
114. Program to delete given number from array.
#include<stdio.h>
#include<conio.h>
void main()
{
int array[10];
int i, n, pos, element, found = 0;
clrscr();
printf("Enter total number of elements : ");
scanf("%d", &n);
printf("\nEnter the elements : \n");
for (i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
printf("Input array elements are\n");
for (i = 0; i < n; i++)
{
printf("\n%d", array[i]);
}
printf("\nEnter the element to be deleted : ");
scanf("%d", &element);
for (i = 0; i < n; i++)
{
if (array[i] == element)
{
found = 1;
pos = i;
break;
}
}
if (found == 1)
{
for (i = pos; i < n - 1; i++)
{
array[i] = array[i + 1];
}
printf("\nResultant array elements are : ");
for (i = 0; i < n - 1; i++)
{
printf("\n%d", array[i]);
}
}
else
{
printf("\nElement %d is not found in the array", element);
}
getch();
}
@C_Codings
#include<stdio.h>
#include<conio.h>
void main()
{
int array[10];
int i, n, pos, element, found = 0;
clrscr();
printf("Enter total number of elements : ");
scanf("%d", &n);
printf("\nEnter the elements : \n");
for (i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
printf("Input array elements are\n");
for (i = 0; i < n; i++)
{
printf("\n%d", array[i]);
}
printf("\nEnter the element to be deleted : ");
scanf("%d", &element);
for (i = 0; i < n; i++)
{
if (array[i] == element)
{
found = 1;
pos = i;
break;
}
}
if (found == 1)
{
for (i = pos; i < n - 1; i++)
{
array[i] = array[i + 1];
}
printf("\nResultant array elements are : ");
for (i = 0; i < n - 1; i++)
{
printf("\n%d", array[i]);
}
}
else
{
printf("\nElement %d is not found in the array", element);
}
getch();
}
@C_Codings
115. Program to calculate the determinant of 2Γ2 matrix.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][2], i, j;
long determinant;
clrscr();
printf("Enter the 4 elements of matrix :\n");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("\nThe matrix is : \n");
for (i = 0; i < 2; i++)
{
printf("\n");
for (j = 0; j < 2; j++)
{
printf("%d\t", a[i][j]);
}
}
determinant = a[0][0] * a[1][1] - a[1][0] * a[0][1];
printf("\nDeterminant of 2X2 matrix: %ld", determinant);
getch();
}
@C_Codings
#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][2], i, j;
long determinant;
clrscr();
printf("Enter the 4 elements of matrix :\n");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("\nThe matrix is : \n");
for (i = 0; i < 2; i++)
{
printf("\n");
for (j = 0; j < 2; j++)
{
printf("%d\t", a[i][j]);
}
}
determinant = a[0][0] * a[1][1] - a[1][0] * a[0][1];
printf("\nDeterminant of 2X2 matrix: %ld", determinant);
getch();
}
@C_Codings
116. Program to calculate the determinant of 3Γ3 matrix.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3], i, j;
long determinant;
clrscr();
printf("Enter the 9 elements of matrix :\n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("\nThe matrix is :\n");
for (i = 0; i < 3; i++)
{
printf("\n");
for (j = 0; j < 3; j++)
{
printf("%d\t", a[i][j]);
}
}
determinant = a[0][0] * ((a[1][1] * a[2][2]) - (a[2][1] * a[1][2]))
- a[0][1] * (a[1][0] * a[2][2] - a[2][0] * a[1][2])
+ a[0][2] * (a[1][0] * a[2][1] - a[2][0] * a[1][1]);
printf("\nDeterminant of 3X3 matrix : %ld", determinant);
getch();
}
@C_Codings
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3], i, j;
long determinant;
clrscr();
printf("Enter the 9 elements of matrix :\n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("\nThe matrix is :\n");
for (i = 0; i < 3; i++)
{
printf("\n");
for (j = 0; j < 3; j++)
{
printf("%d\t", a[i][j]);
}
}
determinant = a[0][0] * ((a[1][1] * a[2][2]) - (a[2][1] * a[1][2]))
- a[0][1] * (a[1][0] * a[2][2] - a[2][0] * a[1][2])
+ a[0][2] * (a[1][0] * a[2][1] - a[2][0] * a[1][1]);
printf("\nDeterminant of 3X3 matrix : %ld", determinant);
getch();
}
@C_Codings
117. Program to find Largest and Smallest number in array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[50], size, i, largest, smallest;
clrscr();
printf("Enter the size of the array : ");
scanf("%d", &size);
for (i = 0; i < size; i++)
{
printf("Enter element %d in to the array : ", i);
scanf("%d", &a[i]);
}
largest = a[0];
for (i = 1; i < size; i++)
{
if (largest < a[i])
{
largest = a[i];
}
}
printf("Largest element : %d", largest);
smallest = a[0];
for (i = 1; i < size; i++)
{
if (smallest > a[i])
{
smallest = a[i];
}
}
printf("\nSmallest element : %d", smallest);
getch();
}
@C_Codings
#include<stdio.h>
#include<conio.h>
void main()
{
int a[50], size, i, largest, smallest;
clrscr();
printf("Enter the size of the array : ");
scanf("%d", &size);
for (i = 0; i < size; i++)
{
printf("Enter element %d in to the array : ", i);
scanf("%d", &a[i]);
}
largest = a[0];
for (i = 1; i < size; i++)
{
if (largest < a[i])
{
largest = a[i];
}
}
printf("Largest element : %d", largest);
smallest = a[0];
for (i = 1; i < size; i++)
{
if (smallest > a[i])
{
smallest = a[i];
}
}
printf("\nSmallest element : %d", smallest);
getch();
}
@C_Codings
118. Program to reverse the array.
#include<stdio.h>
#include<conio.h>
void main()
{
int size, i, j, a[100], b[100];
clrscr();
printf("Enter the size of array : ");
scanf("%d", &size);
for (i = 0; i < size; i++)
{
printf("Enter element %d :", i);
scanf("%d", &a[i]);
}
/*
* Copying elements into array b starting from end of array a
*/
for (i = size - 1, j = 0; i >= 0; i--, j++)
b[j] = a[i];
/*
* Copying reversed array into original.
* Here we are modifying original array, this is optional.
*/
for (i = 0; i < size; i++)
a[i] = b[i];
printf("Reverse array is :\n");
for (i = 0; i < size; i++)
printf("%d\n", a[i]);
getch();
}
@C_Codings
#include<stdio.h>
#include<conio.h>
void main()
{
int size, i, j, a[100], b[100];
clrscr();
printf("Enter the size of array : ");
scanf("%d", &size);
for (i = 0; i < size; i++)
{
printf("Enter element %d :", i);
scanf("%d", &a[i]);
}
/*
* Copying elements into array b starting from end of array a
*/
for (i = size - 1, j = 0; i >= 0; i--, j++)
b[j] = a[i];
/*
* Copying reversed array into original.
* Here we are modifying original array, this is optional.
*/
for (i = 0; i < size; i++)
a[i] = b[i];
printf("Reverse array is :\n");
for (i = 0; i < size; i++)
printf("%d\n", a[i]);
getch();
}
@C_Codings
119. Program to insert an element in array.
#include<stdio.h>
#include<conio.h>
void main()
{
int array[100], position, i, size, value;
clrscr();
printf("Enter size of array : ");
scanf("%d", &size);
for (i = 0; i < size; i++)
{
printf("Enter %d element : ", i);
scanf("%d", &array[i]);
}
printf("Enter the location where you wish to insert an element : ");
scanf("%d", &position);
printf("Enter the value to insert : ");
scanf("%d", &value);
for (i = size - 1; i >= position - 1; i--)
{
array[i + 1] = array[i];
}
array[position - 1] = value;
printf("\nResultant array is : ");
for (i = 0; i <= size; i++)
{
printf(" %d ", array[i]);
}
getch();
}
@C_Codings
#include<stdio.h>
#include<conio.h>
void main()
{
int array[100], position, i, size, value;
clrscr();
printf("Enter size of array : ");
scanf("%d", &size);
for (i = 0; i < size; i++)
{
printf("Enter %d element : ", i);
scanf("%d", &array[i]);
}
printf("Enter the location where you wish to insert an element : ");
scanf("%d", &position);
printf("Enter the value to insert : ");
scanf("%d", &value);
for (i = size - 1; i >= position - 1; i--)
{
array[i + 1] = array[i];
}
array[position - 1] = value;
printf("\nResultant array is : ");
for (i = 0; i <= size; i++)
{
printf(" %d ", array[i]);
}
getch();
}
@C_Codings
120. Program to sort array using Insertion sort
#include<math.h>
#include<stdio.h>
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
void printArray(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{
printf("\n\n Enter the number of data element to be sorted: ");
scanf(β%dβ, &n);
printf("\n Enter element: ");
for(i=0; i<n; i++)
{
scanf(β%dβ, &arr[i]);
}
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
printf(βSorted List: β);
printArray(arr, n);
return 0;
}
# program by @lakshyatyagi24
#include<math.h>
#include<stdio.h>
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
void printArray(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{
printf("\n\n Enter the number of data element to be sorted: ");
scanf(β%dβ, &n);
printf("\n Enter element: ");
for(i=0; i<n; i++)
{
scanf(β%dβ, &arr[i]);
}
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
printf(βSorted List: β);
printArray(arr, n);
return 0;
}
# program by @lakshyatyagi24
121. Binary Search
#include <stdio.h>
int bin(int arr[],int n,int t)
{
int l=0;
int r=n-1;
while(l<=r)
{
int m=l+(r-l)/2;
if(arr[m]<t)
l=m+1;
else if (arr[m]>t)
r=m-1;
else return m;
}
return -1;
}
int main()
{
int n;
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("%d",bin(arr,7,7));
return 0;
}
# program by @freakyLuffy
#include <stdio.h>
int bin(int arr[],int n,int t)
{
int l=0;
int r=n-1;
while(l<=r)
{
int m=l+(r-l)/2;
if(arr[m]<t)
l=m+1;
else if (arr[m]>t)
r=m-1;
else return m;
}
return -1;
}
int main()
{
int n;
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("%d",bin(arr,7,7));
return 0;
}
# program by @freakyLuffy
122. Multiplication of two Matrices
#include<stdio.h>
int main()
{
int a[5][5], b[5][5], c[5][5], m, n, p, q, i, j, k;
printf( "Enter rows and columns of first matrix: ");
scanf("%d%d",&m,&n);
printf("Enter rows and columns of second matrix: ");
scanf("%d%d",&p ,&q);
if (n == p)
{
printf("\nEnter first matrix:\n");
for(i = 0; i < m; ++i)
{for (j = 0; j < n; ++j)
scanf("%d",&a[i][j]);
}
printf("\nEnter second matrix:\n");
for (i = 0; i < p; ++i)
for (j = 0; j < q; ++j)
scanf("%d",&b[i][j]);
printf("\nThe new matrix is:\n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < q; ++j)
{
c[i][j] = 0;
for (k = 0; k < n; ++k)
c[i][j] = c[i][j] + (a[i][k] * b[k][j]);
printf("%d\t",c[i][j]);
}
printf( "\n");
}
}
else
printf("\n Matrix can't be multiplied");
return 0;
}
#Program_by : @freakyLuffy
#include<stdio.h>
int main()
{
int a[5][5], b[5][5], c[5][5], m, n, p, q, i, j, k;
printf( "Enter rows and columns of first matrix: ");
scanf("%d%d",&m,&n);
printf("Enter rows and columns of second matrix: ");
scanf("%d%d",&p ,&q);
if (n == p)
{
printf("\nEnter first matrix:\n");
for(i = 0; i < m; ++i)
{for (j = 0; j < n; ++j)
scanf("%d",&a[i][j]);
}
printf("\nEnter second matrix:\n");
for (i = 0; i < p; ++i)
for (j = 0; j < q; ++j)
scanf("%d",&b[i][j]);
printf("\nThe new matrix is:\n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < q; ++j)
{
c[i][j] = 0;
for (k = 0; k < n; ++k)
c[i][j] = c[i][j] + (a[i][k] * b[k][j]);
printf("%d\t",c[i][j]);
}
printf( "\n");
}
}
else
printf("\n Matrix can't be multiplied");
return 0;
}
#Program_by : @freakyLuffy
123. Goldbach's Conjecture
/*Goldbach's conjecture is a rule in math that states the following: every even number greater than 2 can be expressed as the sum of two prime numbers.*/
#include<stdio.h>
#include<math.h>
int prime (int n)
{
int flag = 0;
for(int i = 2 ; i <= sqrt(n); i ++ )
{
if(n % i == 0)
{
flag = 1;
break ;
}
}
if(!flag)
return 1;
else
return 0;
}
int main()
{
int m;
scanf("%d", & m);
for(int i = 2 ; i <= (m )/2 ; i ++)
{
int k = i , l = m - i;
if(prime(k) && prime(l))
{
printf("%d + %d\n",k , l);
}
}
}
#Program_by : @flippy_boy
/*Goldbach's conjecture is a rule in math that states the following: every even number greater than 2 can be expressed as the sum of two prime numbers.*/
#include<stdio.h>
#include<math.h>
int prime (int n)
{
int flag = 0;
for(int i = 2 ; i <= sqrt(n); i ++ )
{
if(n % i == 0)
{
flag = 1;
break ;
}
}
if(!flag)
return 1;
else
return 0;
}
int main()
{
int m;
scanf("%d", & m);
for(int i = 2 ; i <= (m )/2 ; i ++)
{
int k = i , l = m - i;
if(prime(k) && prime(l))
{
printf("%d + %d\n",k , l);
}
}
}
#Program_by : @flippy_boy
124. Numerical Spiral Pattern.
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
#include<stdio.h>
int main()
{
int n,m,l,k=1;
scanf("%d",&n);
m =(l=n);
int a[m][n] ;
int row = 0 , col = 0 ;
while(k <= l*l )
{
for(int i = col ; i < n; i ++)
{
a[row][i] = k++;
}
row++;
for(int i = row; i < m ; i ++)
{
a[i][n-1] =k ++;
}
n --;
if(row < m)
{
for(int i = n - 1; i >= col ; i --)
{ a[m-1][i] = k ++;
}
}
m--;
if(col < n)
{
for(int i = m-1; i >= row ; i --)
{
a[i][col] = k ++;
}
}
col ++;
}
for(int i = 0 ; i < l ; i ++)
{
for(int j = 0 ; j < l ; j ++)
printf("%d ",a[i][j]);
printf("\n");
}
}
#Program_by : @flippy_boy
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
#include<stdio.h>
int main()
{
int n,m,l,k=1;
scanf("%d",&n);
m =(l=n);
int a[m][n] ;
int row = 0 , col = 0 ;
while(k <= l*l )
{
for(int i = col ; i < n; i ++)
{
a[row][i] = k++;
}
row++;
for(int i = row; i < m ; i ++)
{
a[i][n-1] =k ++;
}
n --;
if(row < m)
{
for(int i = n - 1; i >= col ; i --)
{ a[m-1][i] = k ++;
}
}
m--;
if(col < n)
{
for(int i = m-1; i >= row ; i --)
{
a[i][col] = k ++;
}
}
col ++;
}
for(int i = 0 ; i < l ; i ++)
{
for(int j = 0 ; j < l ; j ++)
printf("%d ",a[i][j]);
printf("\n");
}
}
#Program_by : @flippy_boy
125. Program to sort array using Selection Sort.
#include<stdio.h>
#include<conio.h>
void main()
{
int array[20], n, i, j, pos, temp;
clrscr();
printf("Enter total number of elements :");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("\nEnter element number %d : ", i+1);
scanf("%d", &array[i]);
}
for (i = 0; i < (n - 1); i++)
{
pos = i;
for (j = i + 1; j < n; j++)
{
if (array[pos] > array[j])
pos = j;
}
if (pos != i)
{
temp = array[i];
array[i] = array[pos];
array[pos] = temp;
}
}
printf("\nSorted elements :");
for (i = 0; i < n; i++)
printf("%d\n", array[i]);
getch();
}
@C_Codings
#include<stdio.h>
#include<conio.h>
void main()
{
int array[20], n, i, j, pos, temp;
clrscr();
printf("Enter total number of elements :");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("\nEnter element number %d : ", i+1);
scanf("%d", &array[i]);
}
for (i = 0; i < (n - 1); i++)
{
pos = i;
for (j = i + 1; j < n; j++)
{
if (array[pos] > array[j])
pos = j;
}
if (pos != i)
{
temp = array[i];
array[i] = array[pos];
array[pos] = temp;
}
}
printf("\nSorted elements :");
for (i = 0; i < n; i++)
printf("%d\n", array[i]);
getch();
}
@C_Codings
126. Program to sort array using Bubble Sort.
#include<stdio.h>
#include<conio.h>
void main()
{
int total, element[100], i, j, temp;
clrscr();
printf("Enter total number of elements :");
scanf("%d", &total);
for(i=0; i<total; i++)
{
printf("Enter element no%d :", i+1);
scanf("%d", &element[i]);
}
for (i=0; i<(total - 1); i++)
{
for (j = 0; j < total - 1; j++)
{
if (element[j] > element[j + 1])
{
temp = element[j];
element[j] = element[j + 1];
element[j + 1] = temp;
}
}
}
printf("\nSorted list in ascending order :");
for (i = 0; i < total; i++)
{
printf("\n%d", element[i]);
}
getch();
}
//To sort in descending order, use < (less than) in if condition.
@C_Codings
#include<stdio.h>
#include<conio.h>
void main()
{
int total, element[100], i, j, temp;
clrscr();
printf("Enter total number of elements :");
scanf("%d", &total);
for(i=0; i<total; i++)
{
printf("Enter element no%d :", i+1);
scanf("%d", &element[i]);
}
for (i=0; i<(total - 1); i++)
{
for (j = 0; j < total - 1; j++)
{
if (element[j] > element[j + 1])
{
temp = element[j];
element[j] = element[j + 1];
element[j + 1] = temp;
}
}
}
printf("\nSorted list in ascending order :");
for (i = 0; i < total; i++)
{
printf("\n%d", element[i]);
}
getch();
}
//To sort in descending order, use < (less than) in if condition.
@C_Codings
127. Program for infix to postfix conversion.
#include<stdio.h>
char ifix[200],pfix[200],stack[100];
int p=0,s=0,i=0;
void fun()
{
if(ifix[i]==')')
{
s--;
while(stack[s]!='(')
pfix[p++]=stack[s--];
}
if(ifix[i]=='+' || ifix[i]=='-')
{
if(!s)
stack[s++]=ifix[i];
else if(stack[s-1]=='(')
stack[s++]=ifix[i];
else
{
s--;
while(s!=-1 && stack[s]!='(')
{
pfix[p++]=stack[s--];
}
stack[++s]=ifix[i];
s++;
}
}
}
int main()
{
printf("Enter your infix expression(using operators +, -, %%, /, *) : ");
scanf("%s",ifix);
for(i;ifix[i]!='\0';i++)
{
if(ifix[i]!='*' && ifix[i]!='/' && ifix[i]!='-' && ifix[i]!='+' && ifix[i]!='(' && ifix[i]!=')' && ifix[i]!='%')
pfix[p++]=ifix[i];
else if(ifix[i]=='(' || ifix[i]=='*' || ifix[i]=='/' || ifix[i]=='%')
stack[s++]=ifix[i];
else
fun();
}
if(ifix[i]=='\0')
{
if(s)
{
s--;
while(s!=-1)
{
if(stack[s]==')' || stack[s]=='(')
s--;
pfix[p++]=stack[s--];
}
}
pfix[p]='\0';
}
printf("The postfix expression is : %s",pfix);
return 0;
}
//used by taking into considering operators +, -, /, *, %, (, )
#program_by : @sidg28
#include<stdio.h>
char ifix[200],pfix[200],stack[100];
int p=0,s=0,i=0;
void fun()
{
if(ifix[i]==')')
{
s--;
while(stack[s]!='(')
pfix[p++]=stack[s--];
}
if(ifix[i]=='+' || ifix[i]=='-')
{
if(!s)
stack[s++]=ifix[i];
else if(stack[s-1]=='(')
stack[s++]=ifix[i];
else
{
s--;
while(s!=-1 && stack[s]!='(')
{
pfix[p++]=stack[s--];
}
stack[++s]=ifix[i];
s++;
}
}
}
int main()
{
printf("Enter your infix expression(using operators +, -, %%, /, *) : ");
scanf("%s",ifix);
for(i;ifix[i]!='\0';i++)
{
if(ifix[i]!='*' && ifix[i]!='/' && ifix[i]!='-' && ifix[i]!='+' && ifix[i]!='(' && ifix[i]!=')' && ifix[i]!='%')
pfix[p++]=ifix[i];
else if(ifix[i]=='(' || ifix[i]=='*' || ifix[i]=='/' || ifix[i]=='%')
stack[s++]=ifix[i];
else
fun();
}
if(ifix[i]=='\0')
{
if(s)
{
s--;
while(s!=-1)
{
if(stack[s]==')' || stack[s]=='(')
s--;
pfix[p++]=stack[s--];
}
}
pfix[p]='\0';
}
printf("The postfix expression is : %s",pfix);
return 0;
}
//used by taking into considering operators +, -, /, *, %, (, )
#program_by : @sidg28
128. Pattern
#include<stdio.h>
int main()
{
int i,j,k,l,n;
scanf("%d",&n);
for(k=i=1;i<=n;i++)
{
l=k+i-1;
for(j=1;j<=i;j++)
{
if(i%2==1)
printf("%d ",k);
else
printf("%d ",l);
k++;
l--;
}
printf("\n");
}
return 0;
}
#program_by : @aShish_bhushan
1
3 2
4 5 6
10 9 8 7
11 12 13 14 15
21 20 19 18 17 16#include<stdio.h>
int main()
{
int i,j,k,l,n;
scanf("%d",&n);
for(k=i=1;i<=n;i++)
{
l=k+i-1;
for(j=1;j<=i;j++)
{
if(i%2==1)
printf("%d ",k);
else
printf("%d ",l);
k++;
l--;
}
printf("\n");
}
return 0;
}
#program_by : @aShish_bhushan
129. Pattern
1
8 2
14 9 3
19 15 10 4
23 20 16 11 5
26 24 21 17 12 6
28 27 25 22 18 13 7
#include<stdio.h>
int main() {
int n,i,j,k,l=1,d;
scanf("%d",&n);
for(i=0;i<n;++i)
{
for(j=i-1;j>=0;--j)
{
d=n-1;
l=i+1;
for(k=0;k<=j;++k)
{
l+=d;
d--;
}
printf("%d ",l);
}
printf("%d\n",i+1);
}
return 0;
}
#program_by : @Sherry060199
1
8 2
14 9 3
19 15 10 4
23 20 16 11 5
26 24 21 17 12 6
28 27 25 22 18 13 7
#include<stdio.h>
int main() {
int n,i,j,k,l=1,d;
scanf("%d",&n);
for(i=0;i<n;++i)
{
for(j=i-1;j>=0;--j)
{
d=n-1;
l=i+1;
for(k=0;k<=j;++k)
{
l+=d;
d--;
}
printf("%d ",l);
}
printf("%d\n",i+1);
}
return 0;
}
#program_by : @Sherry060199