Matrix Multiplication in C

Matrix Multiplication in C

Matrix multiplication in C

In this program, I have discussed about how to multiply two matrices using C programming language.
#include<stdio.h>
int main()
{
 int ar1[50][50],ar2[50][50],mul[20][20],i1,i2,j2,j1,k,l,row1,col1,row2,col2,z,m,n,w;
printf("Enter the number of rows of 1st array: \n");
scanf("%d",&row1);
printf("Enter the number of columns in 1st  array: \n");
scanf("%d",&col1);
for(i1=0;i1<row1;i1++)
{
for(j1=0;j1<col1;j1++)
{
printf("\nEnter an element for %dth row and %dth column of 1st array: ",i1,j1);
scanf("%d",&ar1[i1][j1]);
}
}
printf("\nEnter the number of rows of 2nd array: \n");
scanf("%d",&row2);
printf("\nEnter the number of columns in 2nd  array: \n");
scanf("%d",&col2);
for(i2=0;i2<row2;i2++)
{
for(j2=0;j2<col2;j2++)
{
printf("\nEnter an element for %dth row and %dth column of 2nd array: ",i2,j2);
scanf("%d",&ar2[i2][j2]);
}
}
printf("\n");
if(col1!=row2)
{ printf("Matrix multiplication is not possible !!");
}
else
{ for(k=0;k<col1;k++)
{
for(l=0;l<col1;++l)
{ mul[k][l]=0;
for(w=0;w<col1;w++)
{
mul[k][l]+=ar1[k][w]*ar2[w][l];
}

}
}

for(m=0;m<row1;m++)
{
for(n=0;n<col2;n++)
{ printf("%d  ",mul[m][n]);
}
printf("\n");
}
}
}


Just copy and paste the program in your desired compiler (Dev C++ is more preferable) and run it.
Now you can do Matrix multiplication in C.
,

C program to find GCD of two or three numbers using function

 C program to find GCD of two or three numbers using function

GCD of two or three numbers using function


 In this program, I have discussed about how to find the GCD of two or three number using function in the C programming language.


The full form of GCD is greatest common divisor. If we consider three number 10,15,20 then the GCD of these three number will be 5.


int hcf2(int,int);
int hcf3(int,int,int);
 int main()
{ int a,b,c,d,hc1,hc2,n;
printf("Enter the number of numbers whose HCF you want to calculate (2<=number<=3) \n");
scanf("%d",&n);
switch(n)
{ case 2:
printf("Enter two numbers \n");
scanf("%d%d",&a,&b);
hc1=hcf2(a,b);
printf("The HCF of %d and %d is %d \n",a,b,hc1);
break;
case 3:
printf("Enter three numbers \n");
scanf("%d%d%d",&a,&b,&c);
hc2=hcf3(a,b,c);
printf("The HCF of %d, %d and %d is %d \n",a,b,c,hc2);
break;
}
}
int hcf2( int k1,int k2)
{
int i,h1=0;
 for(i=1;i<=k1;i++)
{
if(k1%i==0&&k2%i==0)
{
if(i>h1)
{h1=i;
}
}
}
return h1;
}
int hcf3( int k1,int k2,int k3)
{int i,h2=0;
 for(i=1;i<=k1;i++)
{
if(k1%i==0&&k2%i==0&&k3%i==0)
{
if(i>h2)
{h2=i;
}
}
}
return h2;
}


Just copy and paste the program in your desired compiler (Dev C++ is more preferable) and run it.

Enjoy programming in C.

Program to find GCD of two or three numbers in C

Program to find the GCD of three numbers in C

GCD of three numbers in C

 In this program, I have discussed about how to find the GCD of three numbers in C.

#include<stdio.h>
int main()
{ int a,b,c,d,hcf=0,i;
printf("Enter three numbers: \n");
scanf("%d%d%d",&a,&b,&c);
for(i=1;i<=a;i++)
{
if(a%i==0&&b%i==0&c%i==0)
{
if(i>hcf)
{hcf=i;
}
}
}
printf("HCF is %d ",hcf);
}

Just copy and paste the program in your desired compiler (Dev C++ is more preferable) and run it.




Enjoy programming in C.

,

C program to check strong number

C program to check strong number

C program to check strong number

 In this program, I have discussed about how to check strong number.

145 is called a strong number because the sum of factorial of its digits is equal to that number, i.e., 1!+4!+5!=145.
Strong numbers are also called as special numbers.


#include<stdio.h>
int fact(int);
int main()
{ int n,i,j,sum=0,k,num;
printf("Enter a number :");
scanf("%d",&n);
num=n;
while(n!=0)
{ k=n%10;
j=fact(k);
sum+=j;
n=n/10;
}
if(sum==num)
{ printf("Strong Number");
}
else
{ printf("Not a strong number");
}
}
int fact( int g)
{ int l=1,i;
for(i=1;i<=g;i++)
{ l*=i;
}
return l;
}

Just copy and paste the program in your desired compiler (Dev C++ is more preferable) and run it.



Enjoy programming in C.













,

C Program to Find Factorial of a Number Using Recursion

C Program to Find Factorial of a Number Using Recursion

C program to find factorial of a number using recursion


In this program, I have discussed about the C program to find factorial of a number using recursion.

The formula which we will use is stated below.-
(n)!/(r!*(n-r)!)

#include<stdio.h>
int fact(int);
int main()
{ int a,b,c,n,r,res;
printf("Enter the value of n and r :\n");
scanf("%d%d",&n,&r);
a=fact(n);
b=fact(r);
c=fact(n-r);
res=a/(b*c);
printf("The value is %d \n",res);
}
int fact(int s)
{ int k=1,i;
for(i=1;i<=s;i++)
{ k*=i;
}
return k;
}


Just copy and paste the program in your desired compiler (Dev C++ is more preferable) and run it.

Enjoy programming in C.


,

C Program to Calculate the Sum of cos(x) Series.

C program to calculate the sum of cos(x)

C Program to Calculate the Sum of cos(x) Series

In this program, I have discussed about the C Program to Calculate the Sum of cos(x) Series.

The value from 0° to 180 ° in steps of 30 °  is tabulated in the main program..

#include<stdio.h>
#include<math.h>
double func(long);
double fact(int);
int main()
{ int i,k;long j;
for(i=0;i<=180;i+=30)
{ k=func(i);
printf("The value of the series when X is %d is %d \n",i,k);
}
}
double func(long as)
{ float p=0,r; int l,e=0;
r=((3.14/180)*as);  //angle is converted into radians.
for(l=1;l<=10;l++,e+=2)
{p=p+pow(-1,l+1)*(pow(r,e)/fact(e));
}
return p;
}
double fact(int i)
{ int u=1,y;
for(y=1;y<=i;y++)
{ u*=y;
}
return u;
}

Just copy and paste the program in your desired compiler (Dev C++ is more preferable) and run it.

Enjoy programming in C.


C Program To Search An Element In An Array.

C program to search an element in an array.

C Program To Search An Element In An Array.

In this program, I have discussed about the C program to search an element in an array.

This method is also known as linear search.
#include<stdio.h>
int main()
{ int ar[20],i,j,key,n,pos,fl=0;;
printf("Enter the size of the array \n");
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("Enter an element :\n");
scanf("%d",&ar[i]);
}
printf("Enter the key value to search :\n");
scanf("%d",&key);
for(j=0;j<n;j++)
{ if(ar[j]==key)
{
printf("Search Successfull\n");
printf("Element %d is found in %d position of the array\n",key,j);
fl=1;
break;
}
}
if(fl!=1)
{ printf("Not found !!!");
}
}

Just copy and paste the program in your desired compiler (Dev C++ is more preferable) and run it.

Enjoy programming in C.


C program to print the pattern

 C program to print the pattern

C program to print the pattern

In this program, I have discussed about the C program to print the pattern shown above.


#include<stdio.h>
int main()
{ int i,j,k,l,m,p=1,q=3,z;
for(i=4;i>=1;i--)
{ for(j=1;j<i;j++)
{ printf(" ");
}
for(k=p;k>=1;k--)
{ printf("%d",k);
}
p++;
printf("\n");
}
for(l=1;l<=3;l++)
{ for(z=1;z<=l;z++)
{printf(" ");
}
for(m=q;m>=1;m--)
{ printf("%d",m);
}
q--;
printf("\n");
}

}

Just copy and paste the program in your desired compiler (Dev C++ is more preferable) and run it.

Enjoy programming in C.

C Program to Check whether a given Number is Perfect Number

C program to check whether a given number is perfect number


 C Program to Check whether a given Number is Perfect  Number

In this program, I have discussed about the C program to check whether a given number is perfect number

A number is called a perfect number if the sum of all the divisors of that number is equal to that number.
For example:- 6,,,,,,,, 1+2+3=6.

 
#include<stdio.h>
int main()
{ int n,sum=0,k,i;
printf("Enter the number:");
scanf("%d",&n);
k=n/2;
for(i=1;i<=k;i++)
{ if(n%i==0)
{ sum=sum+i;
}
}
if(n==sum)
{printf("Perfect Number !!");
}
else
printf("Not a perfect number !!");
}

Just copy and paste the program in your desired compiler (Dev C++ is more preferable) and run it.


Enjoy programming in C.
,

C Program to Check Whether a Character is a Vowel or Consonant

C Program to Check Whether a Character is a Vowel or Consonant

C Program to Check Whether a Character is a Vowel or Consonant


In this program, I have discussed about the C program to check whether a character is a vowel or consonant.

#include<stdio.h>
int main()
{ char a;

printf("Enter an alphabet:");
scanf("%c",&a);
switch(a)
{
case 'a': printf("Vowel");
break;
case 'e': printf("Vowel");
break;
case 'i': printf("Vowel");
break;
case 'o': printf("Vowel");
break;
case 'u': printf("Vowel");
break;
case 'A': printf("Vowel");
break;
case 'E': printf("Vowel");
break;
case 'O': printf("Vowel");
break;
case 'I': printf("Vowel");
break;
case 'U': printf("Vowel");
break;
default: printf("Consonant");
}
}

Just copy and paste the program in your desired compiler (Dev C++ is more preferable) and run it.

Enjoy programming in C.

Pageviews

Contact Form

Name

Email *

Message *

 

Copyright 2020 Programming in C All Right Reserved