Programs Library | Think Better ! Do Better !!

C program for find inverse of 3×3 matrix

Version/Compatibility

Turbo C++ Version 3.0 Borland International, Inc.

Source Code


//C program for find inverse of 3x3 matrix

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

int main(void)
{
	int a[3][3],i,j;
	float determinant=0;
	clrscr();
	printf("Enter elements of 3x3 matrix:\n");
	for(i=0;i<3;i++)
	{
		for(j=0;j<3;j++)
		{
			scanf("%d",&a[i][j]);
		}
	}
	printf("\nThe entered matrix is:\n");
	for(i=0;i<3;i++)
	{
		for(j=0;j<3;j++)
		{
			printf("%d\t",a[i][j]);
		}
		printf("\n");
	}
	for(i=0;i<3;i++)
	{
		determinant = determinant + (a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3] - a[1][(i+2)%3]*a[2][(i+1)%3]));
	}
	if(determinant==0)
	{
		printf("Inverse does not exist (Determinant=0).\n");
	}
	else
	{
		printf("\nInverse of matrix is: \n");

		for(i=0;i<3;i++)
		{
			for(j=0;j<3;j++)
			{
				printf("%.2f\t",((a[(i+1)%3][(j+1)%3] *a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
			}
			printf("\n");
		}
	}
	getch();
	return 0;
}

Output

C Program to Calculate Standard Deviation

Version/Compatibility

Turbo C++ Version 3.0 Borland International, Inc.

Source Code


//C Program to Calculate Standard Deviation

#include <stdio.h>
#include <math.h>

float standard_deviation(float data[], int n);
int main(void)
{
	int n, i;
	float data[100];
	clrscr();
	printf("Enter number of datas(should be less than 100): ");
	scanf("%d",&n);
	printf("Enter elements: ");
	for(i=0; i<n; ++i)
		scanf("%f",&data[i]);
	printf("\n");
	printf("Standard Deviation = %.2f", standard_deviation(data,n));
	getch();
	return 0;
}

float standard_deviation(float data[], int n)
{
	float mean=0.0, sum_deviation=0.0;
	int i;
	for(i=0; i<n;++i)
		mean+=data[i];
	mean=mean/n;
	for(i=0; i<n;++i)
		sum_deviation+=(data[i]-mean)*(data[i]-mean);
	return sqrt(sum_deviation/n);
}

Output

C Program to decode a given String

Version/Compatibility

Turbo C++ Version 3.0 Borland International, Inc.

Source Code


//C Program to decode a given String

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

int main(void)
{
	char s[30];
	int l,i;
	int a[30];
	clrscr();
	printf("\n Enter string for encoding: ");
	gets(s);
	l=strlen(s);
	printf("\n Encoded string: ");
	for(i=0;i<l;i++)
	{
		printf("%d",a[i]=s[i]);
	}
	printf("\n Decoded string :");
	for(i=0;i<l;i++)
	{
		printf("%c",a[i]);
	}
	getch();
	return 0;
}

Output

C program for calculate roots of quadratic equation

Version/Compatibility

Turbo C++ Version 3.0 Borland International, Inc.

Source Code


//C program for calculate roots of quadratic equation

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

int main(void)
{
	int a,b,c;
	float d,r1,r2;
	clrscr();
	printf("\nEnter the values of coefficient: ");
	scanf("%d%d%d",&a,&b,&c);
	d=b*b-4*a*c;
	if(d<0)
	{
		printf("\n The roots are complex");
	}
	else
	{
		if(d==0)
		{
			printf("\n The roots are real and equal");
			r1=-b/(2*a);
			printf("\nroot1=%f, root2=%f",r1,r1);
		}
		else
		{
			printf("\nThe roots are real and distinct");
			r1=(-b+sqrt(d))/(2*a);
			r2=(-b-sqrt(d))/(2*a);
			printf("\n\nroot1=%f\troot2=%f",r1,r2);
		}
	}
	getch();
	return 0;
}

Output

C Program to Sort Elements in Lexicographical Order (Dictionary Order)

Version/Compatibility

Turbo C++ Version 3.0 Borland International, Inc.

Source Code


//C Program to Sort Elements in Lexicographical Order (Dictionary Order)

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

int main(void)
{
	int i,j;
	char str[10][50],temp[50];
	clrscr();
	printf("Enter 10 words:\n");
	for(i=0;i<10;++i)
	gets(str[i]);
	for(i=0;i<9;++i)
	{
		for(j=i+1;j<10 ;++j)
		{
			if(strcmp(str[i],str[j])>0)
			{
				strcpy(temp,str[i]);
				strcpy(str[i],str[j]);
				strcpy(str[j],temp);
			}
		}
	}
	printf("In lexicographical order: \n");
	for(i=0;i<10;++i)
	{
		puts(str[i]);
	}
	getch();
	return 0;
}

Output