Telegram Web Link
105. Program to remove white space in string.

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

void main()
{
int i = 0, j = 0, len;
char buff[50];
clrscr();

printf("Enter String with white space : ");
gets(buff);

len = (int) strlen(buff);

while (i != len)
{
if (buff[i] != ' ')
buff[j++] = buff[i];
i++;
}
buff[j] = 0;

printf("\nYour String is : %s.", buff);
getch();
}
@C_Codings
106. Program to reverse the string.

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

char *strrev(char *str)
{
char *p1, *p2;

if (!str || !*str)
return str;
for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
{
*p1 ^= *p2;
*p2 ^= *p1;
*p1 ^= *p2;
}
return str;
}

void main()
{
char arr[100];
clrscr();

printf("\nEnter a string to reverse : ");
gets(arr);

strrev(arr);

printf("\nReverse of entered string is : %s", arr);

getch();
}
@C_Codings
107. Program to reverse the string using pointer.

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

void main()
{
char str_array[10000], *ptr;
int i, len;
clrscr();

printf("Enter a string : ");
gets(str_array);
ptr = str_array;

for (i = 0; i < 10000; i++)
{
if (*ptr == '\0')
break;
ptr++;
}

len = i;
ptr--;
printf("\nReversed String is : ");
for (i = len; i > 0; i--)
{
printf("%c", *ptr--);
}
getch();
}
@C_Codings
108. Program to sort the strings.

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

void main()
{
int i, j, n;
char str[20][20], temp[20];
clrscr();

printf("Enter the number of string to be sorted : ");
scanf("%d", &n);

for (i = 0; i <= n; i++)
gets(str[i]);

for (i = 0; i <= n; i++)
for (j = i + 1; j <= n; j++)
{
if (strcmp(str[i], str[j]) > 0)
{
strcpy(temp, str[i]);
strcpy(str[i], str[j]);
strcpy(str[j], temp);
}
}

printf("\nSorted string :");
for (i = 0; i <= n; i++)
{
puts(str[i]);
}
getch();
}
@C_Codings
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
2024/06/13 02:40:33
Back to Top
HTML Embed Code: