Download as pdf or txt
Download as pdf or txt
You are on page 1of 52

Assignment No 1

1) Write a C program to read an amount (integer value) and break the amount into
smallest possible number of Bangladeshi bank notes.

SOLUTION :

#include<stdio.h>
main()
{
int amt,th,fih,twh,oh,fif,tw,te,fiv,sum=0;
printf("Enter Amount:");
scanf("%d",&amt);
th=amt/1000; amt=amt%1000; fih=amt/500; amt=amt%500;
twh=amt/200; amt=amt%200; oh=amt/100; amt=amt%100;
fif=amt/50; amt=amt%50; tw=amt/20; amt=amt%20;
te=amt/10; amt=amt%10; fiv=amt/5; amt=amt%5;
sum=th+fih+twh+oh+fif+tw+te+fiv;

printf("Thousand tk note=%d\nFive hudred tk note=%d\nTwo hundred tk note=%d\nOne hundred tk


note=%d\nFifty tk note=%d\nTwinty tk note=%d\nTen tk note=%d\nFive tk note=%d\nTotal note
=%d",th,fih,twh,oh,fif,tw,te,fiv,sum);
}

Output:
Enter Amount:1885
Thousand tk note=1
Five hudred tk note=1
Two hundred tk note=1
One hundred tk note=1
Fifty tk note=1
Twinty tk note=1
Ten tk note=1
Five tk note=1
Total note =8
Assignment No 2
2) Write a C program that reads three floating values and check if it is possible to make
a triangle with them. Also calculate the perimeter and area of the triangle if the given
values are valid.

SOLUTION :

#include<stdio.h>
main()
{
float a,b,c,s,p,area;
printf("Enter the three side of triangle: \n");
scanf("%f %f %f",&a,&b,&c);
if(a+b>c&&b+c>a&&c+a>b)
{

printf("Triangle possible.\n");
p=a+b+c;
s=p/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("Perimeter=%f\n Area = %f",p,area);

}
else
printf("Triangle not possible.");
}

Output :

Enter the three side of triangle:


3 4 5
Triangle possible.
Perimeter=12.000000
Area = 6.000000

Enter the three side of triangle:


112
Triangle not possible.
// Assignment No 3
3) Write a C program that reads an integer between 1 and 12 and print the month of the
year in English.

SOLUTION :
#include<stdio.h>
main()
{
int m;
l:;
printf("Enter a number between 1 to 12 : \n");
scanf("%d",&m);
if(m<1||m>12) goto l;
if(m==1) printf("Month = January");
if(m==2) printf("Month = February");
if(m==3) printf("Month = March");
if(m==4) printf("Month = April");
if(m==5) printf("Month = May");
if(m==6) printf("Month = June");
if(m==7) printf("Month = July");
if(m==8) printf("Month = August");
if(m==9) printf("Month = September");
if(m==10) printf("Month = October");
if(m==11) printf("Month = November");
if(m==12) printf("Month = December");
}
Output :
Enter a number between 1 to 12 :
5
Month = May

// Assignment No 4
4) Write a C program to check whether a given integer is positive even, negative even,
positive odd or negative odd. Print even if the number is 0.

SOLUTION :

#include<stdio.h>
main()
{
int a;
printf("Enter an integer number: ");
scanf("%d",&a);
if(a==0)printf("Even integer");
else if(a%2==0&&a>0)printf("Positive even integer");
else if(a%2==0&&a<0)printf("Negative even integer");
else if(a%2!=0&&a>=0)printf("Positive odd integer");
else printf("Negative odd integer");
}

Output :
Enter an integer number: -2
Negative even integer
Enter an integer number: 2
Positive even integer
Enter an integer number: 0
Even integer
Enter an integer number: -11
Negative odd integer
Enter an integer number: 11
Positive odd integer

// Assignment 5
5) Write a C program to read a password until it is correct. For wrong password print
"Incorrect password" and for correct password print "Correct password" and quit the
program. Assume the correct password is 609.

#include<stdio.h>
main()
{
int p;
l:;
printf("Enter the password : ");
scanf("%d",&p);
if(p==609)printf(" \"Correct Password \" ");
else
{
printf("\"Incorrect Password\"\n\n ");
goto l;
}
}
Output:
Enter the password : 509
"Incorrect Password"

Enter the password : 609


"Correct Password "

Assignment 6
6) Write a C program to read the coordinates (x, y) (in Cartesian system) and find the
quadrant to which it belongs. Also find the distance between this point and y=x line.

SOLUTION :
#include<stdio.h>
#include<math.h>
main()
{
float x,y,d;
printf("Enter the co-ordinate x and y : \n");
scanf("%f %f",&x,&y);
if(x>0&&y>0) printf("Co-ordinate belongs to \" First Qudrant \" ");
else if(x<0&&y>0) printf("Co-ordinate belongs to \" Second Qudrant \" ");
else if(x==0&&(y>0||y<0)) printf("On y axis");
else if(y==0(&&x>0||x<0) )printf("On x axis");
else if(x==0&&y==0) printf("At origin");
else if(x<0&&y<0) printf("Co-ordinate belongs to \" Third Qudrant \" ");
else if(x>0&&y<0) printf("Co-ordinate belongs to \" Fourth Qudrant \" ");
d=fabs(x-y)/sqrt(2);
printf("\n Distance = %f",d);
}

Output:
Enter the co-ordinate x and y : -3 2
Co-ordinate belongs to " Second Qudrant "
Distance = 3.535534
Enter the co-ordinate x and y :0 0
At origin
Distance = 0.000000
Enter the co-ordinate x and y :5 5
Co-ordinate belongs to " First Qudrant "
Distance = 0.000000
Enter the co-ordinate x and y :-2 -3
Co-ordinate belongs to " Third Qudrant "
Distance = 0.707107
Enter the co-ordinate x and y :2 -4
Co-ordinate belongs to " Fourth Qudrant "
Distance = 4.242640
Enter the co-ordinate x and y :0 5
On y axis
Distance = 3.535534
Enter the co-ordinate x and y :-5 0
On x axis
Distance = 3.535534

Assignment NO 7
7) Write a C program that reads an integer and find all its divisor. Also determine
whether the integer is a prime or perfect number.
SOLUTION :

#include<stdio.h>
main()
{
int n,i,m,sum=0;
printf("Enter the integer number: ");
scanf("%d",&n);
printf("All divisors of %d are below:\n",n);
for(i=1;i<=n/2;i++)
{
if(n%i==0)
{
printf("%d\n",i);
sum=sum+i;
}
}
printf("%d",n);
if(sum==n&&n!=0) printf("\n\n%d is a perfect number.");
else if(sum==1)
printf("\n\n%d is a prime number.");
else
printf("\n\n%d is not a prime number nor a perfect number.");
}
Output :
Enter the integer number: 6
All divisors of 6 are below:
1
2
3
6
6 is a perfect number.
Enter the integer number: 27

All divisors of 27 are below:


1
3
9
27
27 is not a prime number nor a perfect number.

// Assignment No.8
8) Write a C program that swaps two numbers without using third variable.
#include<stdio.h>
main()
{
int a,b;
printf("Enter two integer number a and b\n");
scanf("%d %d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("First number a= %d\n Second number b =%d",a,b);
}
Output :
Enter two integer number a and b:10 20
First number a= 20
Second number b =10
// Assignment No.9
9) Write a C program to reverse and print a given number.

#include<stdio.h>
main()
{
int num,rev=0,rem;
printf("Enter an integer number= ");
scanf("%d",&num);
while(num>=1)
{
rem=num%10;
rev=rev*10+rem;
num=num/10;
}
printf("Revers number=%d",rev);
}

Output :
Enter an integer number= 258
Revers number=852

// Assignment No.10
10) Write a C program to determine whether a given positive integer is divisible by
three by the technique of summing up the digits repeatedly.

SOLUTION :

#include<stdio.h>
main()
{
int a,r=1,sum=0,num;
printf("Enter an integer number: ");
scanf("%d",&num);
a=num;
lev:
sum=0;
while(a>=1)
{
r=a%10;
a=a/10;
sum=sum+r;
}
printf("sum=%d\n",sum);
if(sum>9)
{
a=sum;
goto lev;
}
if(sum%3==0) printf("%d is divisible by 3\nSo %d is divisivle by 3",sum,num);
else printf("%d is not divisible by 3\n",sum);
}
Output :
Enter an integer number: 456789
sum=39
sum=12
sum=3
3 is divisible by 3
So 456789 is divisivle by 3

// Assignment No.11
11) Write a C program that prints out the prime numbers between m and n (m<n). The
output should be such that each row contains a maximum of five prime numbers.

SOLUTION :

#include<stdio.h>
main()
{
int m,n,r,flag,a=0,i=0;
printf("Enter the minimum and maximum value :\n");
scanf("%d %d",&m,&n);
printf("Prime numbers are =\n");
for(m;m<=n;m++)
{
flag=1;
for(r=2;r<=m/2;r++)
{
if(m%r==0)
{
flag=0;
break;
}
}
if(flag==1&&m!=1&&m!=0) {printf("%4d",m);i=i+1;if(i==5){printf("\n");i=0;}}
}
}

Output :
Enter the minimum and maximum value :
1
100
Prime numbers are =
2 3 5 7 11
13 17 19 23 29
31 37 41 43 47
53 59 61 67 71
73 79 83 89 97

// Assignment 12
12) Write a C program to print a binomial coefficient table.

SOLUTION :

#include<stdio.h>
main()
{
int m=0,x=0,bi=1,n;
printf("Enter value of n:");
scanf("%d",&n);
printf("Binomial co-efficient table:\n");

do
{
printf("n=%3d",m);
x=0;bi=1;
while(x<=m)
{
if(m==0||x==0) printf(" %4d",bi);
else
{
bi=bi*(m-x+1)/x;
printf(" %4d",bi);
}
x=x+1;
}
printf("\n");
m=m+1;
}
while(m<=n);
}

Output :
Enter value of n:10
Binomial co-efficient table:
n= 0 1
n= 1 1 1
n= 2 1 2 1
n= 3 1 3 3 1
n= 4 1 4 6 4 1
n= 5 1 5 10 10 5 1
n= 6 1 6 15 20 15 6 1
n= 7 1 7 21 35 35 21 7 1
n= 8 1 8 28 56 70 56 28 8 1
n= 9 1 9 36 84 126 126 84 36 9 1
n= 10 1 10 45 120 210 252 210 120 45 10 1

// Assignment 13
13) Write a C program to print the alphabet set in decimal (ASCII value) and character
form.
#include<stdio.h>
main()
{
char c;
printf("Alphabet with their ascii value\n");
for(c=65;c<=122;c++)
{

if(c>90&&c<97) continue;
printf("|%d - %c|\t",c,c);
}
}

Output:
Alphabet with their ascii value
|65 - A| |66 - B||67 - C||68 - D||69 - E||70 - F||71 - G||72 - H||73 - I||74 - J||75 - K||76 - L||77 - M|
|78 - N||79 - O||80 - P||81 - Q||82 - R||83 - S||84 - T||85 - U||86 - V||87 - W||88 - X||89 - Y||90 - Z|
|97 - a||98 - b||99 - c||100 - d||101 - e||102 - f||103 - g||104 - h||105 - i||106 - j||107 - k||108 - l||109 - m|
|110 - n||111 - o||112 - p||113 - q||114 - r||115 - s||116 - t||117 - u||118 - v||119 - w||120 - x||121 - y|
|122 - z|
// Assignment 14
14) Write a C program that accepts one seven-digit number and separates the number
into its individual digits, and prints the digits separated from one another by two spaces
each.

#include<stdio.h>
main()
{
int n,r[50],i;
up:
printf("Enter a seven digit a number:");
scanf("%d",&n);
if(n>9999999) goto up;
for(i=0;n>=1;i++)
{
r[i]=n%10;
n=n/10;
}
printf("Seperate numbers are:\n");
for(i=6;i>=0;i--)
printf("%3d",r[i]);
}
Output :
Enter a seven digit a number:12345678
Enter a seven digit a number:1234567
Seperate numbers are:
1 2 3 4 5 6 7

// Assignment NO. 15
15) Write a C program that reads in a five-digit integer and determines whether it is a
palindrome or not.

#include<stdio.h>
main()
{
int num,r,rev=0,m;
up:
printf("Enter a five digit number :\n");
scanf("%d",&num);
if(num>99999) goto up;
m=num;
while(m>=1)
{
r=m%10;
rev=rev*10+r;
m=m/10;
}
if(num==rev) printf("%d is a palindrome number.",num);
else printf("%d is not palindrome number.",num);
}
Output :
Enter a five digit number :
123456
Enter a five digit number :
22222
22222 is a palindrome number.

// Assignment 16
16) Write a c program that reads a set of data of 10 positive values and find their
arithmetic mean,geometric mean ,harmonic mean mode and median

#include<stdio.h>
main()
{
int i,c,j,count=0,n=1,k=0,d[20],t,p=1,mode,z=0,b[15]={0},max=0,m=0;
float median,sum=0.0,mean,gm,hm,summ=0.0;

printf("Enter 10 Numbers:\n");
for(i=1;i<=10;i++)
{scanf("%d",&d[i]);sum=sum+d[i];p=p*d[i];summ=summ+1.0/d[i];}
mean=sum/10.0;
gm=pow(p,0.1);
hm=10.0/summ;
for(i=1;i<=10;i++)
{for(j=i+1;j<=10;j++)
{
if(d[i]<d[j])
{t=d[i];d[i]=d[j];d[j]=t;}}
}
median=(d[5]+d[6])/2.0;
printf("Mean=%f\nG.M=%f\nH.M=%f\nMedian=%f",mean,gm,hm,median);

for(i=1;i<10;i++)
{
mode=0;
for(j=i+1;j<=10;j++)
{if(d[i]==d[j])mode=mode+1;}

if((mode>=max)&&(mode!=0)){if((mode>max)&&k!=0){b[k]=0;k=0;m=d[i];max=mode;}
else {max=mode;b[k]=d[i];k++;}}

}
printf("\nMode =");
if(m!=0)printf("%d",m);
else if(k==0||max==9||(k==5&&max==1)||(k==2&&max==4)) {printf("No Mode in this
data.\n");}
else
for(i=0;i<k;i++)
printf("\t%4d",b[i]);

Output :
Enter 10 Numbers:
45 23 6 58 45 23 56 45 58 45
Mean=40.400002
G.M=7.544250
H.M=25.325947
Median=45.000000
Mode =45

Enter 10 Numbers:
1 2 3 4 5 6 7 8 9 10
Mean=5.500000
G.M=4.528728
H.M=3.414171
Median=5.500000
Mode =No Mode in this data.

Enter 10 Numbers:
5555555555
Mean=5.000000
G.M=5.000000
H.M=5.000000
Median=5.000000
Mode =No Mode in this data.

Enter 10 Numbers:
5555544444
Mean=4.500000
G.M=4.472136
H.M=4.444445
Median=4.500000
Mode =No Mode in this data.

Enter 10 Numbers:
5555512346
Mean=4.100000
G.M=3.675541
H.M=3.076923
Median=5.000000
Mode = 5
Enter 10 Numbers:
2233445566
Mean=4.000000
G.M=3.727919
H.M=3.448276
Median=4.000000
Mode =No Mode in this data.

Enter 10 Numbers:
122 3344556
Mean=3.500000
G.M=3.116387
H.M=2.678571
Median=3.500000
Mode = 5 4 3 2

Enter 10 Numbers:
1333444555
Mean=3.700000
G.M=3.415430
H.M=2.985075
Median=4.000000
Mode = 5 4 3

// Assignment No . 17
17) Write a C program to find the angle between (12:00 to 11:59) the hour hand and the
minute hand of a clock.

#include<stdio.h>
main()
{
int h,m;
float a;
printf("Enter Hour and Minute : \n");
scanf("%d %d",&h,&m);
a=fabs((60*h-11*m)/2.0);
if(a>180) a=360.0-a;
printf(" Angle between hour and minute at %d:%d is=%f Degree\n",h,m,a);
}

Output:
Enter Hour and Minute :
3
30
Angle between hour and minute at 3:30 is=75.000000 Degree

Assignment 18
18) Write a C program to find the last non-zero digit of the factorial of a given positive
integer.

SOLUTION :

#include<stdio.h>
main()
{
int n5=0,n2=0,num,i,n[1000],a,q,l,ld=1;
printf("Enter number:");
scanf("%d",&num);
a=num;
while(num>1)
{
n5=n5+num/5;num=num/5;
}

for(i=1;i<=a;i++)
{
if((n2<n5)&&(i%2==0)) {n[i]=i/2;n2++;}
else n[i]=i;
lev:
if(n[i]%5==0) n[i]=n[i]/5;
if(n[i]%5==0) goto lev;
}

for(i=1;i<=a;i++)
{
ld=ld*n[i];
ld=ld%10;
}
printf("\nlast non zero digit= %d\n",ld);
}
Output :
Enter number:25
last non zero digit= 4

Enter number:5
last non zero digit= 2

// Assignment NO. 19
19) Write a C program to check if a given number is nearly prime or not. Nearly prime
number is a positive integer which is equal to product of two prime numbers .
SOLUTION :

#include<stdio.h>
main()
{
int num,i,flag;
printf("Enter a number:\n");
scanf("%d",&num);
if(prime(num)==prime){flag=0;goto down;}

for(i=2;i<=num/2;i++)
{
if(num%i==0)
{
if(prime(i)==prime&&prime(num/i)==prime){flag=1;break;}
}
}
down:
if(flag==1)
printf("%d is nearly prime number.\n Divisors are= %d and %d",num,i,num/i);
else printf("%d is not nearly prime number",num);
}
int prime(int n)
{
int i,flag=1;
if(n==1||n%2==0) {flag=0;goto lev;}
for(i=3;i<=sqrt(n);i=i+2)
{if(n%i==0) {flag=0;break;}
}
lev:
if(flag==1||n==2)return(prime);
}
Output:
Enter a number:
25
25 is nearly prime number.
Divisors are= 5 and 5
Enter a number:
20
20 is not nearly prime number

// Assignment 20
20) Write a C program that reads n digits (given) chosen from 0 to 9 and prints the
number of combinations where the sum of the digits equals to another given number (s).
Do not use the same digits in a combination.
SOLUTION :
#include<stdio.h>
main()
{
int i,j,k,num,n=0;
l:
printf("Enter a number from 0 to 9 :\n");
scanf("%d",&num);
if(9<num||num<0) goto l;
printf("Combinations are:\n");
for(i=0;i<=num;i++)
for(j=i+1;j<=num;j++)
for(k=j+1;k<=num;k++)
if(i+j+k==num)
{n++;
printf("%d+%d+%d=%d\n",i,j,k,num);
}
printf("Total number of combinatiobn=%d",n);
}
Output :
Enter a number from 0 to 9 :
8
Combinations are:
0+1+7=8
0+2+6=8
0+3+5=8
1+2+5=8
1+3+4=8
Total number of combinatiobn=5
// Assignment No . 21
21) Write a C program to generate a random number and guess the number from user-
end repeatedly.

SOLUTION :
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
main()
{
int m,n,l,f;
printf("Enter Lower limit :");
scanf("%d",&l);
printf("Enter upper limit :");
scanf("%d",&f);
printf("Random number will be produce from %d to %d\n",l,f);
srand(time(0));
n=rand()%(l-f+1)+l;
le:
printf("Enter your guess number:");
scanf("%d",&m);
if(m!=n) goto le;
printf("Your guess no is correct\n The number is =%d",n);
}
Output :
Enter Lower limit :1
Enter upper limit :10
Random number will be produce from 1 to 10
Enter your guess number:5
Enter your guess number:2
Enter your guess number:7
Enter your guess number:9
Enter your guess number:10
Enter your guess number:1
Enter your guess number:8
Enter your guess number:6
Enter your guess number:3
Enter your guess number:4
Your guess no is correct
The number is =4

// Assignment no 22
22) Write a C program to sort the elements of an array.
SOLUTION :
#include<stdio.h>
main()
{
int a[100],i,n,j,m,k;
printf("How may number you want to take: ");
scanf("%d",&n);
printf("Enter %d number:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Number sorting of descending:\n");
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]<a[j])
{
m=a[i];a[i]=a[j];a[j]=m;
}
}
printf("%d\t",a[i]);
}

printf("\nSorting number ascending order:\n");


for(i=0;i<n;i++)
{k=n-(i+1);printf("%d\t",a[k]);}
}

Output :
How may number you want to take: 10
Enter 10 number:
10 25 16 300 500 89 45 63 28 5000
Number sorting of descending:
5000 500 300 89 63 45 28 25 16 10
Sorting number ascending order:
10 16 25 28 45 63 89 300 500 5000

//Assignment No. 23
23) Write a C program to find whether a given year is a leap year or not.

#include<stdio.h>
main()
{
int y;
printf("Enter year:\n");
scanf("%d",&y);
if(y%400==0||y%4==0&&y%100!=0)printf("%d is a leap year.",y);
else
printf("%d is not leap year.",y);
}
Output :
Enter year:
1600
1600 is a leap year.
Enter year:
1500
1500 is not leap year
//Assignment No. 24
24) Write a C program to calculate the (real or complex) root of a Quadratic Equation
#include<stdio.h>
main()
{
int a,b,c,d;float x,y,r1,r2;
printf("Enter a,b,c:\n");
scanf("%d %d %d",&a,&b,&c);
d=b*b-4*a*c;
x=-b/(2.0*a);y=sqrt(fabs(d))/(2.0*a);
if(d>=0)
{r1=x+y;r2=x-y;
printf("Two roots are:\tr1=%3.2f\t r2=%3.2f",r1,r2);
}
else
{printf("Two roots are :\n");
printf("r1=\t%3.2f + r2=\t%3.2fi\n",x,y);
printf("%3.2f - %3.2fi",x,y);}
}
Output :
Enter a,b,c:
1 -7 12
Two roots are: r1=4.00 r2=3.00

Enter a,b,c:
123
Two roots are :
r1= -1.00 + r2= 1.41i
-1.00 - 1.41i

Enter a,b,c:
1 -10 25
Two roots are: r1=5.00 r2=5.00
//Assignment No. 25
25) Write a C program to check whether a triangle is Equilateral, Isosceles, Right-
angled or Scalene.

#include<stdio.h>
main()
{
int a,b,c ;
printf("Enter three side of a triangle:\n");
scanf("%d %d %d",&a,&b,&c);
if(a+b>c&&b+c>a&&c+a>b)
{
if(a==b&&b==c)printf("Equilateral Triangle.");
if(a==b&&b!=c||b==c&&c!=a||c==a&&a!=b)printf("Isolateral Triangle.");
else if(a!=b&&b!=c)printf("Scalene Triangle.");
if(a*a+b*b==c*c||b*b+c*c==a*a||c*c+a*a==b*b)printf("\nRight angle Triangle.");
}
else
printf("Triangle not possible.");
}
Output:
Enter three side of a triangle:
123
Triangle not possible.
Enter three side of a triangle:
456
Scalene Triangle.
Enter three side of a triangle:
345
Scalene Triangle.
Right angle Triangle.
Enter three side of a triangle:
445
Isolateral Triangle.
Enter three side of a triangle:
222
Equilateral Triangle.
//Assignment No. 26
26) Write a program in C to read a numbers n from keyboard and find sum of the series
1+2+3+..+n and print “1+2+3+..+n=sum” (every term).

#include<stdio.h>
main()
{
int n,sum=1,i;
printf("Enter a number:\n");
scanf("%d",&n);
printf("Sum of series:\n");
printf("%d",sum);
for(i=2;i<=n;i++)
{sum=sum+i;
printf("+%d",i);
}
printf("=%d",sum);
}

Output :
Enter a number:
20
Sum of series:
1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20=210

// Assignment No. 27
27) Write a program in C to display the multiplication table of a given integer.

#include<stdio.h>
main()
{
int n,i;
printf("Enter a number:\n");
scanf("%d",&n);
printf("Multiplication table of %d\n",n);
for(i=1;i<=10;i++)
{
printf("%dX%d=%d\n",n,i,n*i);
}
}
Output :
Enter a number:
5
Multiplication table of 5
5X1=5
5X2=10
5X3=15
5X4=20
5X5=25
5X6=30
5X7=35
5X8=40
5X9=45
5X10=50

// Assignment No. 28
28) Write a program in C to make such a pattern like right angle triangle with number
increased by 1.
1
23
456
7 8 9 10
.................

SOLUTION :

#include<stdio.h>
main()
{
int n,i,j=1,k;
printf("Enter number of row:\n");
scanf("%d",&n);
printf("Pattern like right angle triangle:\n\n");
for(i=1;i<=n;i++)
{for(k=1;k<=i;k++)
{printf("%d\t",j);j++;}
printf("\n");}}
Output :
Enter number of row:
10
Pattern like right angle triangle:

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55

// Assignment No. 29
29) Write a C program to write all the twin prime numbers pair-wise in a certain range.

#include<stdio.h>
#include<math.h>
main()
{
int m,n,i=0,flag,j,p[100],k,a,tp;
printf("Enter upper and lower limit:\n");
scanf("%d %d",&m,&n);
printf("Twin prime numbers are:\n");
for(m;m<=n;m++)
{
if(m==1||m%2==0) {flag=0;goto down;}
flag=1;
for(j=3;j<=m/2;j=j+2)
{
if(m%j==0)
{flag=0;break;}
}
down:
if(flag==1||m==2){p[i]=m;i++;}
}
for(k=0;k<i;k++)
{
tp=p[k+1]-p[k];
if(tp==2) printf("%d \t%d\n",p[k],p[k+1]);
}
}
Output :
Enter upper and lower limit:
1 100
Twin prime numbers are:
3 5
5 7
11 13
17 19
29 31
41 43
59 61
71 73

// Assignment No. 30
30) Write a C program to find the GCD of two positive integers by Euclid’s method.
#include<stdio.h>
main()
{
int a,b,i,rem=1;
printf("Enter two number:\n");
scanf("%d %d",&a,&b);
while(rem>=1)
{rem=a%b;
a=b;b=rem;}
printf("GCD=%d",a);

}
Output :
Enter two number:
2 5
GCD=1
Enter two number:
8 4
GCD=4

// Assignment No. 31
31) Write a program in C to read a sentence and replace lowercase characters by
uppercase and vice-versa.

#include<stdio.h>
#include<string.h>
main()
{
int i,n;
char sen[50];
printf("Enter a sentence:");
gets(sen);
n=strlen(sen);
for(i=0;i<=n;i++)
{
if(islower(sen[i]))
putchar(toupper(sen[i]));
else if(isupper(sen[i]))
putchar(tolower(sen[i]));
else
printf(" ");
}
}
Output:
Enter a sentence:
This is Bangladesh
tHIS IS bANGLADESH

// Assignment 32
32) Write a program in C to find the largest and smallest word in a string.
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
char word[80][80],large[80],small[80];
int i=0,j=0,k,l;
printf("Enter a asentence:\n");
gets(str);
for(k=0;str[k]!='\0';k++)
{
if(str[k]!=' ' && str[k]!='\0'){word[i][j++]=str[k];}
else {word[i][j]='\0';i++;j=0;}

}
l=i+1;
strcpy(small,word[0]);
strcpy(large,word[0]);
for(k=0;k<l;k++)
{
if(strlen(small)>strlen(word[k]))
{strcpy(small,word[k]);}
if(strlen(large)<strlen(word[k]))
{strcpy(large,word[k]);}
}
printf("smallest=%s\n",small);
printf("largest=%s",large);
}
Output :
Enter a asentence:
My name is Rasel
smallest=My
largest=Rasel

// Assignment no 33
33) Write a program in C to split string by space into words.

#include<stdio.h>
main()
{
int l,i;
char str[100];
printf("Enter a string:\n");
gets(str);
printf("Split strings:\n");
l=strlen(str);
for(i=0;i<=l;i++)
{ putchar(str[i]);
if(str[i]==' ')printf("\n");
}
}
Output :
Enter a string:
This is a test string
Split strings:
This
is
a
test
string

// Assignment NO 34
34) Write a C programming to count of each character in a given string.

#include<stdio.h>
main()
{
int l;
char str[100];
printf("Enter a string:\n");
gets(str);
l=strlen(str);
printf("String lenth=%d",l);
}

Output :
Enter a string:
The quick brown fox jumps over the lazy dog
String lenth=43

// Assignment 35
35) Write a program in C to add two numbers using pointers.
#include<stdio.h>
main()
{
int *p,*q,x,y,s;
printf("Enter two number:\n");
scanf("%d %d",&x,&y);
p=&x;q=&y;
s=*p+*q;
printf("Sum=%d",s);
}
Output:
Enter two number:
10
20
Sum=30

//Assignment 36
36) Write a program in C to print all permutations of a given string using pointers.
#include<stdio.h>
#include<string.h>
void changePosition(char *ch1,char *ch2)
{
char tmp;
tmp= *ch1;
*ch1= *ch2;
*ch2=tmp;
}
void charPermu(char *cht, int stno, int endno)
{
int i;
if(stno==endno)
printf("%s\n",cht);
else
{
for(i=stno;i<endno;i++)
{
changePosition((cht+stno),(cht+i));
charPermu(cht,stno+1,endno);
changePosition((cht+stno),(cht+i));
}
}
}

main()
{
int n;
char str[100];
printf("Enter a string:\n");
gets(str);
n=strlen(str);
printf("The permutation of the given string are:\n");
charPermu(str,0,n-1);
printf("\n\n");
}

Output :
Enter a string:
Rasel
The permutation of the given string are:
Rasel
Raesl
Rsael
Rseal
Resal
Reasl
aRsel
aResl
asRel
aseRl
aesRl
aeRsl
saRel
saeRl
sRael
sReal
seRal
seaRl
easRl
eaRsl
esaRl
esRal
eRsal
eRasl

//Assignment no 37
37) Write a program in C to swap elements using call by reference
#include<stdio.h>
void swapnumber(float *a,float *b,float*c);
main()
{
float a,b,c;
printf("Enter three number according to a,b,c:\n");
scanf("%f %f %f",&a,&b,&c);
swapnumber(&a,&b,&c);
printf("a=%f\nb=%f\nc=%f",a,b,c);
}
void swapnumber(float *a,float *b,float*c)
{
float t;
t=*b;
*b=*a;
*a=*c;
*c=t;
}
Output
Enter three number according to a,b,c: 10 20 30
a=30.000000
b=10.000000
c=20.000000

// Assignment 38
38) Write a program in C to sort an array using Pointer.

void sort(int n, int *ptr)


{
int i,j,t;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(*(ptr+i)<*(ptr+j))
{
t=*(ptr+i);
*(ptr+i)= *(ptr+j);
*(ptr+j)=t;
}
}
}
printf("Sorting number in descending order:\t");
for(i=0;i<n;i++)
printf("%d\t",*(ptr+i));

printf("\nSorting number in ascending order:\t");


for(i=n-1;i>=0;i--)
printf("%d\t",*(ptr+i));
}

int main()
{
int i,n,arr[100];
printf("How many number:");
scanf("%d",&n);
printf("Enter number:\n");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
sort(n,arr);
return 0;
}
Output :
How many number:10
Enter number:
11 14 25 36 47 58 69 36 25 14
Sorting number in descending order: 69 58 47 36 36 25 25 14 14 11

Sorting number in ascending order: 11 14 14 25 25 36 36 47 58 69

// Assignment 39
39) Write a program in C to swap two numbers using function
#include<stdio.h>
void swapnum(int a,int b);
main()
{
int a,b;
printf("Enter two number:\n");
scanf("%d %d",&a,&b);
printf("After swaping two number are:\n");
swapnum(a,b);

}
void swapnum(int a,int b)
{int t;
t=a;a=b;b=t;
printf("a=%d\nb=%d",a,b);
}
Output
Enter two number:
10 20
After swaping two number are:
a=20
b=10

// Assignment NO 40
40) Write a program in C to check armstrong and perfect numbers using the function.

#include<stdio.h>
void armper(int n)
{
int i,sum=1,sum2=0,d,m,f=0;
m=n;
for(i=2;i<=n/2;i++)
{
if(n%i==0){sum=sum+i;;}
}
if(sum==m&&m!=1){printf("%d is perfect number.\n",m);f=1;}
while(n>0)
{
d=n%10;
sum2=sum2+d*d*d;
n=n/10;
}

if(sum2==m){printf("%d is an armstrong number.\n",m);f=1;}


if(f==0) printf("Neither perfect nor armstrong number ");
}
main()
{
int n;
printf("Enter number:");
scanf("%d",&n);
armper(n);

}
Output :
Enter number:0
0 is an armstrong number.
Enter number:371
371 is an armstrong number.
Enter number:6
6 is perfect number.
Enter number:100
Neither perfect nor armstrong number

// Assignment No. 41
41) Write a program in C to check whether two given strings are an anagram.

#include<stdio.h>
#include<string.h>
main()
{
int i,j,t,k,l,m,n,s;
char str1[100],str2[100];
printf("Enter a string\n");
gets(str1);
printf("Enter another string\n");
gets(str2);
l=strlen(str1);
m=strlen(str2);

if(l!=m) {printf("These strings are not anargam.\n");goto lev;}


for(i=0;i<=(l-1);i++)
{

for(j=i+1;j<=(l-1);j++)
{if(str1[i]<str1[j]){t=str1[i];str1[i]=str1[j];str1[j]=t;}
if(str2[i]<str2[j]){s=str2[i];str2[i]=str2[j];str2[j]=s;}
}
}

n=strcmp(str1,str2);
if(n==0)printf("These string are anargram.");
else printf("These strings are not anargam.");
lev:;
}

OUTPUT
Enter a string
CAPITAL
Enter another string
ACTPAIL
These string are anargram.
Enter a string
rasel
Enter another string
Masel

// Assignment No 42
42) Write a C program to find the square root of a number using Babylonian method
and Newton-Raphson method.

#include<stdio.h>
#include<math.h>
main()
{
int i,r;
float x=1.0,n,b=1.0,a;
printf("Enter a number for root:\n");
scanf("%f",&n);
for(i=1;fabs(n-x*x)>0.0001;i++)
{x=0.5*(x+n/x);}
printf("root by newt rupsun met=%4.2f",x);
a=n;
for(i=0;(a-b)>0.0001;i++)
{a=(a+b)/2.0;b=n/a;}
printf("\nroot by bl method=%4.2f",a);

Output
Enter a number for root:
16
root by newt rupsun met=4.00
root by bl method=4.00
Enter a number for root:
2
root by newt rupsun met=1.41
root by bl method=1.41
// Assignment no 43
43) Write a C program to divide two integers (dividend and divisor) to find quotient and
remainder without using multiplication, division and mod operator.

#include<stdio.h>
main()
{
int num,n,i,q,r;
printf("Enter divident and divisor:\t");
scanf("%d %d",&num,&n);
if(num<n) {q=0;r=num;goto l;}
for(i=1;num>0;i++)
{
if(num>=n) num=num-n;
else break;
}
q=i-1;r=num;
l:
printf("quotient=%d \t remainder=%d",q,r);
}
Output
Enter divident and divisor: 10 3
quotient=3 remainder=1
Enter divident and divisor: 5 7
quotient=0 remainder=5

// Assignment 44
44) Write a program in C to print Fibonacci series using recursion.

#include<stdio.h>
int fibonacci(int);
main()
{
int n,i=0,c;
printf("How many terms of fibonacci:");
scanf("%d",&n);
printf("Fibonacci series\n");
for(c=1;c<=n;c++)
{
printf("%d\t",fibonacci(i));
i++;
}

}
int fibonacci(int n)
{
if(n==0)return 0;
if(n==1)return 1;
else return (fibonacci(n-1)+fibonacci(n-2));
}
Output :
How many terms of fibonacci: 20
Fibonacci series
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
610 987 1597 2584 4181

// Assignment NO 45
45) Write a program in c to find gcd of two numbers using recursion .
#include<stdio.h>
main()
{
int a,b;
printf("Enter divisor and dividend:");
scanf("%d %d",&a,&b);
printf("Gcd=");
printf("%d\n",gcd(a,b));

}
int gcd(int a,int b)
{
if(a!=0)return (gcd(b%a,a));
else
return b;

}
Output
Enter divisor and dividend: 10 2
Gcd=2
Enter divisor and dividend:17 2
Gcd=1

// Assignment 46
46) Write a C program to print following Pyramid:
*
**
***
****
*****
*****
****
***
**
*

#include<stdio.h>
main()
{
int i,j,n,t;
printf("Enter number of row:\t");
scanf("%d",&n);
printf("Shape of pyramid\n");
for(i=1;i<=n;i++)
{
t=1;if(i>n/2)t=n-i+1;
while(t<=i&&t>0)
{
printf("*");
if(i<=n/2) t++;
if(i>n/2) t--;
}
printf("\n");
}
}

Output :
Enter number of row: 10
Shape of pyramid
*
**
***
****
*****
*****
****
***
**
*
// Assignment No 47
47) Write C program to print following character pyramid:
ABCDEDCBA
ABCD DCBA
ABC CBA
AB BA
A A

SOLUTION :

#include<stdio.h>
main()
{
int i,j,k,l,m;
printf("Required Shape\n\n\n");
for(i=0;i<=6;i++)
{
for(k=65;k<=69-i;k++)
printf("%c",k);
for(j=1;j<=i*2-1;j++)
printf(" ");
for(l=69-i;l>=65;l--)
if(l!=69)printf("%c",l);
printf("\n");
}

Output
Required Shape

ABCDEDCBA
ABCD DCBA
ABC CBA
AB BA
A A

// Assignment NO. 48
48) Write a C program to calculate the sum of the series 1 + (1+2) + (1+2+3) +
(1+2+3+4) + ... + (1+2+3+...+n)
SOLUTION :

#include<stdio.h>
main()
{
int i,n,sum=0,j;
printf("Value of N:");
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=i;j++)
sum=sum+j;
printf("Sum=%d",sum);
}

Output :
Value of N:10
Sum=220

// Assignment NO. 49
49) Write a C program to calculate sum of the series 1 + 11 + 111 + 1111 + ... N terms.

#include<stdio.h>
main()
{
int i,n,d=1,sum=1,term=1;
printf("Value of N:");
scanf("%d",&n);
for(i=1;i<n;i++)
{d=d*10;term=term+d;sum=sum+term;}
printf("Sum=%d",sum);

Output:
Value of N:3
Sum=123

// Assignment 50
50) Write a C program to calculate sinx and cosx by their Maclaurin series upto n terms.

#include<stdio.h>
main()
{
long int i,x,term,n,k,k2;
double sum=0.0,fact=1.0,sum2=1.0,fact2=1.0;
printf("Enter value of x and total term:");
scanf("%d %d",&x,&n);

for(i=1;i<=n;i++)
{
k=2*i-1;k2=2*i;
if(k==1)fact=1;
else fact=fact*(k)*(k-1);
fact2=fact2*(k2)*(k2-1);
sum=sum+(pow(-1,i+1)*pow(x,k))/fact;
sum2=sum2+(pow(-1,i)*pow(x,k2))/fact2;
}
printf("Sum of sin(%ld)=%Lf\nSum of cos(%ld)=%Lf",x,sum,x,sum2);
}

Output :
Enter value of x and total term: 3 8
Sum of sin(3)=0.141120
Sum of cos(3)=-0.989992

// Assignment 51
51) Write a C program to find an approximate value of Pi by Monte-Carlo simulation.

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
main()
{
double i,n,tp=0.0,ipc=0.0,opc=0.0;
float r,x,y,pi;
printf("Enter total point: \n");
scanf("%lf",&n);
srand(time(0));
for(i=0;i<=n;i++)
{
x=(rand()%500)/250.0;
y=(rand()%500)/250.0;
r=pow(x-1.0,2)+pow(y-1.0,2);
tp++;
if(r>1.0) opc++;//opc=ouitside point of circle
}
ipc=tp-opc;//ipc=inside point of circle tp=total point
pi=ipc/tp*4.0;
printf("Value of Pi=%f",pi);
}
Output :
Enter total point: 1000000
Value of Pi=3.143385

// Assignment 52
52) Write a C program to find the approximate likelihood (or probability) of showing up
of every face of a ludo dice if the dice is rolled 1 million times.

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
main()
{
int i, n,c1=0,c2=0,c3=0,c4=0,c5=0,c6=0;
float p1,p2,p3,p4,p5,p6;
srand(time(0));
for(i=1;i<=1000000;i++)
{
n=rand()%6+1;
if(n==1)c1++;
if(n==2)c2++;
if(n==3)c3++;
if(n==4)c4++;
if(n==5)c5++;
if(n==6)c6++;
}
p1=c1/1000000.0;
p2=c2/1000000.0;
p3=c3/1000000.0;
p4=c4/1000000.0;
p5=c5/1000000.0;
p6=c6/1000000.0;
printf("pro of 1=%f\npro of 2=%f\npro of 3=%f\npro of 4=%f\npro of 5=%f\npro of
6=%f\n",p1,p2,p3,p4,p5,p6);
}
Output :
pro of 1=0.166237
pro of 2=0.166555
pro of 3=0.167093
pro of 4=0.166825
pro of 5=0.166531
pro of 6=0.166759
// Assignment 53
53) Find the GCD and LCM of two positive integers by divisor method.
#include<stdio.h>
main()
{
int a,b,rem,p,lcm;
printf("Enter the two integer number :\n");
scanf("%d %d",&a,&b);
p=a*b;
while(rem>0)
{
rem=a%b;
a=b;
b=rem;
}
printf("GCD is =%d\n",a);
lcm=p/a;
printf("LCM IS =%d",lcm);
}
Output :
Enter the two integer number :
2 10
GCD is =2
LCM IS =10

// ASSIGNMENT 54
54) Write a C program to find the solution of 3x3 system of linear equation by Cramer’s
rule.

#include<stdio.h>
float det(float a[][3]);
void main()
{
float a[3][3],b[3],ax[3][3],ay[3][3],az[3][3];
int i,j;
float x,y,z;
printf("Enter matrix a:");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%f",&a[i][j]);

if(det(a)==0)
{printf("Unique solution does not exist.");return;}
printf("Enter matrix b:");
for(i=0;i<3;i++)
scanf("%f",&b[i]);

for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
if(j==0)ax[i][j]=b[i];
else ax[i][j]=a[i][j];

if(j==1)ay[i][j]=b[i];
else ay[i][j]=a[i][j];

if(j==2)az[i][j]=b[i];
else az[i][j]=a[i][j];
}
x=det(ax)/det(a);
y=det(ay)/det(a);
z=det(az)/det(a);
printf("x=%f\ty=%f\tz=%f",x,y,z);
}
float det(float a[][3])
{
float sum;
sum=a[0][0]*a[1][1]*a[2][2]+
a[0][1]*a[1][2]*a[2][0]+
a[0][2]*a[1][0]*a[2][1]-
a[0][2]*a[1][1]*a[2][0]-
a[0][1]*a[1][0]*a[2][2]-
a[0][0]*a[1][2]*a[2][1];
return (sum);
}
Output
Enter matrix a:
1 2 3
2 4 7
3 5 10
Enter matrix b:
14 31 43
x=1.000000 y=2.000000 z=3.000000

Enter matrix a:
1 2 3
4 5 6
7 8 9
Unique solution does not exist.
// Assignment 55
55) Write a C program to print string one by one characters using loop.

#include<stdio.h>
main()
{
int i,l;
char str[100];
printf("Enter a string\n");
gets(str);
l=strlen(str);
for(i=0;i<=l;i++)
{if(str[i]!=' ') putchar(str[i]);printf("\t");}

Output :
Enter a string
This is a string
T h i s i s a s t r i n g

// Assignment 56
56) Write C program to convert number from Decimal to Binary/Octal/Hexadecimal.

#include<stdio.h>
main()
{
int b[100],o[100],i,j,k,n,no,nh,rem,d;
char h[100];
printf(“Enter a decimal number\n”);
scanf("%d",&n);
no=n;
nh=n;
for(i=0;n>=2;i++)
{b[i]=n%2;n=n/2;}
b[i]=n;

printf("Equvalent binary number:\t");


for(i=i;i>=0;i--)
printf("%d",b[i]);

for(j=0;no>=8;j++)
{o[j]=no%8;no=no/8;}
o[j]=no;
printf("\nEquivalent octal numer:\t");
for(j=j;j>=0;j--)
printf("%d",o[j]);

for(k=0;nh!=0;k++)
{
rem=nh%16;
if(nh>=10&&nh<16) {d=nh-10;h[k]=65+d;nh=nh/16;}
else if(rem<10) {h[k]=48+rem;nh=nh/16;}
else {h[k]=55+rem;nh=nh/16;}
}
printf("\nEquivalent hexadecimal numer:\t");
for(k=k;k>=0;k--)
printf("%c",h[k]);
}
Output
Enter a decimal number
100
Equvalent binary number: 1100100
Equivalent octal numer: 144
Equivalent hexadecimal numer: 64

// Assignment 57
57) Write C program to print the characteristic equation of a 3x3 matrix
#include<stdio.h>
float det(float a[][3]);
void main()
{
float a[3][3],t=0.0,sum2;
int i,j;
float x,y,z;
printf("Enter matrix a:\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
scanf("%f",&a[i][j]);
if(i==j) t=t+a[i][j];
}
sum2=(a[1][1]*a[2][2]- a[2][1]*a[1][2])
+(a[0][0]*a[2][2]- a[0][2]*a[2][0])
+(a[0][0]*a[1][1]- a[1][0]*a[0][1]);

printf("Characteristic equation =\n");


printf("x^3 - (%3.1f) x^2 + (%3.1f) x - (%3.1f) = 0",t,sum2,det(a));
}
float det(float a[][3])
{
float sum;
sum=a[0][0]*a[1][1]*a[2][2]+
a[0][1]*a[1][2]*a[2][0]+
a[0][2]*a[1][0]*a[2][1]-
a[0][2]*a[1][1]*a[2][0]-
a[0][1]*a[1][0]*a[2][2]-
a[0][0]*a[1][2]*a[2][1];
return (sum);
}

Output:
Enter matrix a:
123
456
789
Characteristic equation =
x^3 - (15.0) x^2 + (-18.0) x - (0.0) = 0

// Assignment 58
58) Write a C program to print the equation of plane passing through three given points.

#include<stdio.h>
main()
{
float x1,y1,z1,x2,y2,z2,x3,y3,z3,a1,b1,c1,d;
char eq[50],x,y,z;
printf("Enter first point:\n");
scanf("%f %f %f",&x1,&y1,&z1);
printf("Enter second point:\n");
scanf("%f %f %f",&x2,&y2,&z2);
printf("Enter third point:\n");
scanf("%f %f %f",&x3,&y3,&z3);
a1=y2*(z3-z1)-y3*(z2-z1)-y1*(z3-z2);
b1=z2*(x3-x1)-z3*(x2-x1)-z1*(x3-x2);
c1=x2*(y3-y1)-x3*(y2-y1)-x1*(y3-y2);
d=a1*(-x1)+b1*(-y1)+c1*(-z1);
printf("Equation of plane=\t");
printf("%5.2f x+(%4.2f) y+(%4.2f) z+(%4.2f)=0",a1,b1,c1,d);

}
Output :
Enter first point:
123
Enter second point:
-2 5 3
Enter third point:
0 6 -3
Equation of plane= -18.00 x+(-18.00) y+(-9.00) z+(81.00)=0

// Assignment 59
59) Write a C program to find the inverse of a 3x3 matrix by finding its adjoint.

#include<stdio.h>
#include<math.h>
void cofactor(float [][25], float);
float determinant(float [][25], float);
void transpose(float [][25], float [][25], float);

int main()
{
float a[25][25], n, d;
int i, j;

printf("Enter the order of the Matrix: ");


scanf("%f", &n);
printf("Enter the elements of a matrix: \n");
for (i = 0;i < n; i++)
{
for (j = 0;j < n; j++)
{
scanf("%f", &a[i][j]);
}
}

d = determinant(a, n);
if (d == 0)
printf("Since the determinant is zerp (0), therefor inverse is not possible.");
else
cofactor(a, n);
}

float determinant(float a[25][25], float k)


{
float s = 1, det = 0, b[25][25];
int i, j, m, n, c;
if (k == 1)
{
return (a[0][0]);
}
else
{
det = 0;
for (c = 0; c < k; c++)
{
m = 0;
n = 0;
for (i = 0;i < k; i++)
{
for (j = 0 ;j < k; j++)
{
b[i][j] = 0;
if (i != 0 && j != c)
{
b[m][n] = a[i][j];
if (n < (k - 2))
n++;
else
{
n = 0;
m++;
}
}
}
}
det = det + s * (a[0][c] * determinant(b, k - 1));
s = -1 * s;
}
}

return (det);
}

void cofactor(float num[25][25], float f)


{
float b[25][25], fac[25][25];
int p, q, m, n, i, j;
for (q = 0;q < f; q++)
{
for (p = 0;p < f; p++)
{
m = 0;
n = 0;
for (i = 0;i < f; i++)
{
for (j = 0;j < f; j++)
{
if (i != q && j != p)
{
b[m][n] = num[i][j];
if (n < (f - 2))
n++;
else
{
n = 0;
m++;
}
}
}
}
fac[q][p] = pow(-1, q + p) * determinant(b, f - 1);
}
}
transpose(num, fac, f);
}
void transpose(float num[25][25], float fac[25][25], float r)
{
int i, j;
float b[25][25], inverse[25][25], d;

for (i = 0;i < r; i++)


{
for (j = 0;j < r; j++)
{
b[i][j] = fac[j][i];
}
}

d = determinant(num, r);
for (i = 0;i < r; i++)
{
for (j = 0;j < r; j++)
{
inverse[i][j] = b[i][j] / d;
}
}

printf("\nThe inverse of matrix: \n");


for (i = 0;i < r; i++)
{
for (j = 0;j < r; j++)
{
printf("\t%f", inverse[i][j]);
}
printf("\n");
}
}
Output :
Enter the order of the Matrix: 3
Enter the elements of a matrix:
3 -4 2
-2 1 0
-1 -1 1

The inverse of matrix:


1.000000 2.000000 -2.000000
2.000000 5.000000 -4.000000
3.000000 7.000000 -5.000000

//Assignment 60
60) Find the dot product, cross product and angle between two vectors.
#include<stdio.h>
#include<math.h>
main()
{
float a1,a2,a3,b1,b2,b3,dot,f1,f2,f3,v1,v2,a;
printf("Enter first vector:\n");
scanf("%f %f %f",&a1,&a2,&a3);
printf("Enter second vector:\n");
scanf("%f %f %f",&b1,&b2,&b3);
dot=a1*b1+a2*b2+a3*b3;
printf("Dot Product=%f\n",dot);
f1=a2*b3-b2*a3;f2=-(a1*b3-b1*a3);f3=a1*b2-b1*a2;
printf("Cross product=%3.1fi +%3.1fj+%3.1fk\n",f1,f2,f3);
v1=sqrt(a1*a1+a2*a2+a3*a3);v2=sqrt(b1*b1+b2*b2+b3*b3);
a=acos(dot/(v1*v2));
printf("Angle between them=%f\n",a*(180/3.1416));
}
Output :
Enter first vector:
123
Enter second vector:
456
Dot Product=32.000000
Cross product=-3.0i +6.0j+-3.0k
Angle between them=12.933130

You might also like