C Programs With Solutions-2
C Programs With Solutions-2
#include<stdio.h>
main()
{
int a,b;
clrscr();
printf("Enter value for a:");
scanf("%d",&a);
printf("/nEnter value for b:");
scanf("%d",&b);
printf("/n%d+%d=%d",a,b,a+b);
printf("/n%d-%d=%d",a,b,a-b);
printf("/n%dX%d=%d",a,b,a*b);
printf("/n%d/%d=%d",a,b,a/b);
}
o/p:
Enter value for a:5
Enter value for b:2
5+2=7
5-2=3
5X2=10
5/2=2
o/p:
Enter principle :5700.50
Enter Time Period:1.6
Enter Rate of Interest:2.5
Simple Interest=228.020004
4. Distance Travelled*/
#include<stdio.h>
main()
{
float a,u,t,dis;
clrscr();
printf("\nEnter velocity, acceleration and time:");
scanf("%f%f%f",&a,&u,&t);
dis=(u*t)+(0.5*a*t*t);
printf("Distance travelled=%f",dis);
getch();
}
if(a==b)
{
printf("%d and %d are equal"a,b);
}
}
}
o/p:
Enter any number:153
153 is amstrong number
Enter any number:235
235 is not amstrong number
24. /*strong number: The sum of factorial of individual digits should be equal to the
original no. eg:145=(1!+4!+5!)*
#include<stdio.h>
main()
{
long int n,d,sum=0,m,i,f;
clrscr();
printf("Enter any number:");
scanf("%ld",&n);
m=n;
while(n!=0)
{
d=n%10;
f=1;
for(i=d;i>=1;i--)
f=f*i;
sum=sum+f;
n=n/10;
}
if(m==sum)
printf("%ld is strong number",m);
else
printf("%ld is not strong number",m);
getch();
}
o/p:
Enter any number:145
145 is strong number
Enter any number:568
568 is not strong number
25. /*strong number: The sum of factorial of individual digits should be equal to the
original no. eg:145=(1!+4!+5!)*/
#include<stdio.h>
main()
{
long int n,d,sum=0,m,i,f;
clrscr();
printf("Enter any number:");
scanf("%ld",&n);
m=n;
while(n!=0)
{
d=n%10;
f=1;
for(i=d;i>=1;i--)
f=f*i;
sum=sum+f;
n=n/10;
}
if(m==sum)
printf("%ld is strong number",m);
else
printf("%ld is not strong number",m);
getch();
}
o/p:
Enter any number:145
145 is strong number
Enter any number:568
568 is not strong number
#include <stdio.h>
main()
{
int n,i=1,t=1;
clrscr();
printf("/nEnter a table:");
scanf("%d",&n);
while(i<=10)
{
t=n*i;
printf("%2d X%2d=%2d/n",n,i,t);
i++;
}
getch();
}
o/p:
Enter a table:5
5 X 1= 5
5 X 2=10
5 X 3=15
5 X 4=20
5
5
5
5
5
5
X 5=25
X 6=30
X 7=35
X 8=40
X 9=45
X10=50
#include <stdio.h>
main()
{
int count=0,i=1;
clrscr();
while(count!=20)
{
if(i%2!=0)
{
printf("%d\n",i);
count++;
}
i++;
}
getch();
}
30.
#include<stdio.h>
main()
{
int n,i=0,a[20],d,j,m;
clrscr();
printf("Enter decimal number:");
scanf("%d",&n);
m=n;
while(n!=0)
{
d=n%2;
a[++i]=d;
n=n/2;
}
printf("Binary equivalent of %d=",m);
for(j=i;j>=1;j--)
printf("%d",a[j]);
getch();
}
31./*Binary
#include<stdio.h>
#include<math.h>
main()
{
long int n,m;
int i=0,sum=0,d,j;
clrscr();
printf("Enter binary number:");
scanf("%ld",&n);
m=n;
while(n!=0)
{
d=n%10;
sum=sum+(d*pow(2,i));
n=n/10;
i++;
}
printf("Decimal equivalent of %ld=%d",m,sum);
getch();
}
32.
#include<stdio.h>
main()
{
int a,b,res;
char ch;
printf("\n Enter values for a and b:");
scanf("%d%d",&a,&b);
printf("\nEnter choice:");
scanf("%s",&ch);
switch(ch)
{
case '+': res=a+b; break;
case '-': res=a-b; break;
case '*': res=a*b; break;
case '/': res=a/b; break;
case '%': res=a%b; break;
default: printf("\n Enter correct choice:");
}
printf("Result=%d",res);
getch();
}
33. /*Menu driven program having the following options 1. Factorial of a number 2.
int n,f,np,a,count=0,ch,i;
do
{
printf("/n1.Factorial of a Number");
s|
Contact Usmain()
{
int i;
clrscr();
printf("Number/tSquare");
for(i=1;i<=10;i++)
{
printf("/n%d/t%d",i,i*i);
}
}
o/p:
Number
1
2
3
4
5
6
7
8
9
10
Square
1
4
9
16
25
36
49
64
81
100
35. /*Printing Squares And Cubes Of Even Numbers Using For Loop */
#include
main()
{
long int i;
clrscr();
printf("Number/tSquare/tCube");
for(i=1;i<=50;i++)
{
if(i%2==0)
{
printf("/n%ld/t/t%ld/t/t%ld",i,i*i,i*i*i);
}
}
}
36. /*Printing Format Using For Loop */
main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%2d",i);
}
printf("/n");
}
getch();
}
o/p:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
37. /*Printing Format Using For Loop */
main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%2d",j);
}
printf("/n");
}
getch();
}
o/p:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
38. /*Printing Format Using For Loop */
main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
printf(" ");
for(j=i;j<=5;j++)
{
printf("%d",j);
}
printf("/n");
}
}
o/p:
12345
2345
345
45
5
1
main()
{
int i,j,n;
clrscr();
printf("Enter number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=i;j>=1;j--)
printf("%3d",j);
printf("\n");
}
getch();
}
o/p:
Enter number:9
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
7 6 5 4 3 2 1
8 7 6 5 4 3 2 1
9 8 7 6 5 4 3 2 1
o/p:
Enter number:5
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
3
1
1 */
main()
{
int i,j,n,m;
clrscr();
printf("Enter number:");
scanf("%d",&n);
m=n;
for(i=1;i<=m;i++)
{
for(j=n;j>=1;j--)
printf("%3d",j);
printf("\n");
n=n-1;
}
getch();
}
7
8
1 */
main()
{
int i,j,n,m;
clrscr();
printf("Enter number:");
scanf("%d",&n);
m=n;
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
printf("%3d",j);
printf("\n");
n=n-1;
}
getch();
}
o/p:
Enter number:9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
}
o/p:
Enter the range:5
5 4 3 2 1 0 1 2 3 4 5
t=n*i;
printf("%2dX%2d=%d/n",n,i,t);
}
getch();
}
o/p:
Enter a table:5
5 X 1= 5
5 X 2=10
5 X 3=15
5 X 4=20
5 X 5=25
5 X 6=30
5 X 7=35
5 X 8=40
5 X 9=45
5 X10=50
46. /*Printing Table Using For Loop */
main()
{
int n,i,count=0;
clrscr();
printf("Enter number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
count++;
}
if(count==2)
printf("%d-Prime",n);
else
printf("%d-Not Prime",n);
}
o/p:
Enter number:958
958-Not Prime
Enter number:83
83-Prime
47. /*Printing Prime Numbers From 1 t0 100 Using For Loop */
main()
{
int i,j,count=0;
clrscr();
for(i=1;i<=100;i++)
{
for(j=1;j<=i;j++)
{
if(i%j==0)
count++;
}
if(count==2)
printf("%5d",i);
count=0;
}
}
o/p:
2
41
97.
3
43
5
47
7
53
11
59
13
61
17
67
19
71
23
73
29
79
31
83
37
89
else
{
printf("\nEnter non -ve exponent:");
scanf("%d",&n);
goto xy;
}
xy:
printf("\nGeometric Progression:\n");
for(i=0;i<=n;i++)
{
t=pow(x,i);
sum=sum+t;
printf("%d+",t);
}
printf("=%d",sum);
getch();
}
50. /*Finding Sum Of Array Elements*/
#include
main()
{
int a[10],i,s=0;
clrscr();
printf("Enter elements into array:");
for(i=0;i<=4;i++)
{
scanf("%d",&a[i]);
s=s+a[i];
}
printf("Sum of array elements=%d",s);
}
o/p:
Enter elements into array: 4 7 2 9 1
Sum of array elements=23
51. /*Finding Large And Small elements in Array*/
main()
{
int n,i,l=0,s=999,a[10];
clrscr();
printf("Enter Range:");
scanf("%d",&n);
printf("/n Enter elements:");
for(i=0;i<=n-1;i++)
{
scanf("%d",&a[i]);
if(l< a[i])
l=a[i];
else
s=a[i];
}
printf("Large=%d",l);
printf("/n Small=%d",s);
o/p:
Enter Range:5
Enter elements:52 58 0 -8 6
Large=58
Small=-8
52. /*Finding Sum of even and odd Nos in Array*/
main()
{
int n,i,ds=0,es=0,a[10];
clrscr();
printf("Enter Range:");
scanf("%d",&n);
printf("/n Enter elements:");
for(i=0;i<=n-1;i++)
{
scanf("%d",&a[i]);
if(a[i]%2==0)
es=es+a[i];
else
ds=ds+a[i];
}
printf("Even Nos Sum =%d",es);
printf("/nOdd Nos Sum=%d",ds);
}
o/p:
Enter Range:5
Enter elements:
1
2
3
4
5
Even Nos Sum =6
Odd Nos Sum=9
53. /*Sorting Array Elements*/
main()
{
static int a[10],n,i,j,temp;
clrscr();
printf("/n Enter no.of elements:");
scanf("%d",&n);
printf("/nEnter elements:");
for(i=0;i<=n-1;i++)
scanf("%d",&a[i]);
for(i=0;i<=n-1;i++)
{
for(j=i+1;j<=n-1;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
printf("/nElements after sorting:");
for(i=0;i<=n-1;i++)
printf("%5d",a[i]);
getch();
}
o/p:
Enter no.of elements:5
Enter elements:9
8
0
-3
2
Elements after sorting:
-3
}
55.
/*Subtraction Of Matrices Using Arrays*/
main()
{
int i,j,r,c;
static int a[20][20],b[20][20],sum[20][20];
clrscr();
printf("Enter no.of rows:");
scanf("%d",&r);
printf("Enter no.of columns:");
scanf("%d",&c);
printf("Entet Matrix A:");
for(i=0;i<=r-1;i++)
{
for(j=0;j<=c-1;j++)
scanf("%d",&a[i][j]);
}
printf("Enter Matrix B:");
for(i=0;i<=r-1;i++)
{
for(j=0;j<=c-1;j++)
scanf("%d",&b[i][j]);
}
printf("Subtraction of two matrices=/n");
for(i=0;i<=r-1;i++)
{
for(j=0;j<=c-1;j++)
{
sum[i][j]=a[i][j]-b[i][j];
printf("%4d",sum[i][j]);
}
printf("/n");
}
}
56. /*Multiplication Of Matrices Using Arrays*/
main()
{
int i,j,rA,cA,rB,cB,k;
static int a[20][20],b[20][20],mul[20][20];
clrscr();
printf("Enter no.of rows for Matrix A:");
scanf("%d",&rA);
printf("Enter no.of columns:");
scanf("%d",&cA);
printf("Enter no.of rows for Matrix B:");
scanf("%d",&rB);
printf("Enter no.of columns for Matrix B:");
scanf("%d",&cB);
if(cA!=rB)
printf("Matrix Multiplication Not Possible");
else
{
printf("Enter Matrix A:");
for(i=0;i<=rA-1;i++)
{
for(j=0;j<=cA-1;j++)
scanf("%d",&a[i][j]);
}
for(i=0;i<=r-1;i++)
cs[j]=cs[j]+a[i][j];
printf("/n%d Column Sum=%d",j+1,cs[j]);
}
}
o/p:
Enter No.of rows:2
Enter No.of columns:2
1 2
3 4
1 Row sum=3
2 Row sum=7
1 Column Sum=4
2 Column Sum=6
58./*Greatest Row Sum in the Array*/
main()
{
static int a[10][10],b[10][10],rs[10];
static int i,j,r,c,l;
clrscr();
printf("/nEnter No.of rows:");
scanf("%d",&r);
printf("/nEnter No.of columns:");
scanf("%d",&c);
for(i=0;i<=r-1;i++)
{
for(j=0;j<=c-1;j++)
scanf("%d",&a[i][j]);
}
for(i=0;i<=r-1;i++)
{
for(j=0;j<=c-1;j++)
rs[i]=rs[i]+a[i][j];
if(rs[i]>l)
l=rs[i];
}
printf("/n Greatest row sum=%d",l);
}
o/p:
Enter No.of rows:2
Enter No.of columns:2
1 2
3 4
Greatest row sum=7
main()
{
static int a[10][10];
static int i,j,r,c;
clrscr();
printf("/nEnter No.of rows:");
printf("/nEnter No.of columns:");
printf("/nEnter elements:");
for(i=0;i<=r-1;i++)
{
for(j=0;j<=c-1;j++)
scanf("%d",&a[i][j]);
}
for(i=0;i<=r-1;i++)
{
for(j=0;j<=c-1;j++)
scanf("%d",&r);
scanf("%d",&c);
if(i==j)
a[i][j]=0;
printf("%2d",a[i][j]);
}
printf("/n");
}
o/p:
Enter No.of rows:3
Enter No.of columns:3
Enter elements:
1 2 3
4 5 6
7 8 9
0 2 3
4 0 6
7 8 0
60. /*Identity Matrix Using Arrays*/
main()
{
static int a[10][10],b[10][10];
static int i,j,r,c;
printf("/nEnter No.of rows:");
scanf("%d",&r);
printf("/nEnter No.of columns:");
scanf("%d",&c);
printf("/nEnter elements:");
for(i=0;i<=r-1;i++)
{
for(j=0;j<=c-1;j++)
scanf("%d",&a[i][j]);
}
for(i=0;i<=r-1;i++)
{
for(j=0;j<=c-1;j++)
{
if(i==j)
a[i][j]=1;
else
a[i][j]=0;
printf("%2d",a[i][j]);
}
printf("/n");
}
}
61. /*Transpose Of A Matrix*/
main()
{
static int a[10][10],b[10][10];
static int i,j,r,c;
clrscr();
printf("/nEnter No.of rows:");
scanf("%d",&r);
63. /*Program to count vowels and consonents and the special characters in the text*/
#include<ctype.h>
main()
{
char s[20];
static int i,v,c,sc;
clrscr();
printf("/nEnter the string:");
gets(s);
for(i=0;s[i]!='/0';i++)
{
if(isalpha(s[i]))
{
if(s[i]=='a' || s[i]=='e' || s[i]=='i'||
s[i]=='o' || s[i]=='u')
v++;
else
c++;
}
esle
sc++;
}
printf("/nVowels=%d",v);
printf("/nConsonants=%d",c);
printf("/nSpecial Characters=%d",sc);
getch();
}
o/p:
Enter the string:this is the computer lab
Vowels=7
64. /*Program to count Number of lines, Words and characters in the text*/
#include<stdio.h>
main()
{
char str[81], ctr;
int i=0,c=0,words= 0,lines = 0;
clrscr();
printf("Enter the string:");
while(1)
{
while((ctr=getchar()) != '\n')
str[i++] = ctr;
str[i] = '\0';
if(str[0] == '\0')
break ;
else
{
words++;
for(i=0; str[i] != '\0';i++)
{
c++;
if(str[i] == ' ' || str[i] == '\t')
words++;
}
}
lines = lines +1;
}
printf("\nNumber of lines = %d\n", lines);
67. strlen()
68.strrev()
69.strcpy()
70.strcat()
71.strcmp()
72.strupr()
73.strlwr()
74. /*Finding String Length Without Using Library Function */
main()
{
char s[20];
static int i,len;
clrscr();
printf("/nEnter string:");
gets(s);
for(i=0;s[i]!='/0';i++)
len++;
printf("Length of the string=%d",len);
getch();
}
o/p:
Enter string:this is c programming
Length of the string=21
75. /*String Reverse Without Using Library Function */
main()
{
char s[100],c[100];
static int i,len,j;
clrscr();
printf("/nEnter string:");
gets(s);
len=strlen(s);
for(i=len-1;i>=0;i--)
{
c[j++]=s[i];
}
printf("Length of the string=%s",c);
getch();
}
o/p:
Enter string:this is c programming
Length of the string=gnimmargorp c si siht
len=strlen(s);
for(i=len-1;i>=0;i--)
c[j++]=s[i];
/*reversing two strings*/
c[i]='/0';
for(i=0;s[i]!='/0';i++)
{
if(s[i]==c[i])
/*Comparision of two strings*/
count++;
}
if(len==count)
printf("palindrome");
else
printf("Not palindrome");
getch();
}
o/p:
Enter first string:madam
palindrome
Enter first string:college
Not palindrome
79. /*Program to convert given string into uppercase*/
main()
{
char s[100],c[100];
static int i,j,len,count;
clrscr();
printf("/nEnter first string:");
gets(s);
for(i=0;s[i]!='/0';i++)
{
c[i]=(s[i]>='a' && s[i]<='z')?('A'+s[i]-'a'):s[i];
}
c[i]='/0';
printf("/nstring in uppercase:");
puts(c);
getch();
}
o/p:
Enter first string:this is c programming
string in uppercase:THIS IS C PROGRAMMING
80. /*Program to convert given string into lowercase*/
main()
{
char s[100],c[100];
static int i,j,len,count;
clrscr();
if(s[i]!=' ')
{
s[i]=s[j++];
}
else
{
s[i]=s[i+1];
s[i]=s[j++];
}
}
s[i]='\0';
printf("\nString after %d characters deletion
from %d position:",n,p);
puts(s);
getch();
}
o/p:
Enter string: This is beatiful world
Enter no.of characters to delete: 4
Enter the position:5
string after 4 characters deletion from 5 position:
thisbeatiful world.
83. Sorting Strings
main()
{
char s[20][100],temp[20];
static int i,j,n;
printf("/nEnter no.of strings:");
scanf("%d",&n);
for(;i<=n;i++)
gets(s[i]);
for(i=0;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(strcmp(s[i],s[j])>0)
{
strcpy(temp,s[i]);
strcpy(s[i],s[j]);
strcpy(s[j],temp);
}
}
}
printf("/nstrings after sorting:");
for(i=0;i<=n;i++)
puts(s[i]);
}
84. Addition And Multiplication of Complex Numbers
#include<stdio.h>
struct complex
{
int real,img;
}comp,comp1;
main()
{
clrscr();
compread();
compwrite();
compadd();
compmul();
getch();
}
compread()
{
printf("\nEnter Complex No1:");
scanf("%d%d",&comp.real,&comp.img);
printf("\nEnter Complex No2:");
scanf("%d%d",&comp1.real,&comp1.img);
}
compwrite()
{
printf("\nFirst Complex Number:");
printf("%di%d\n",comp.real,comp.img);
printf("\nSecond Complex Number:");
printf("%di%d\n",comp1.real,comp1.img);
}
compadd()
{
struct complex cadd;
cadd.real=comp.real+comp1.real;
cadd.img=comp.img+comp1.img;
printf("\nAddtion of two complex nos:");
printf("%di%d",cadd.real,cadd.img);
}
compmul()
{
struct complex cmul;
cmul.real=(comp.real*comp1.real)-(comp.img*comp1.img);
cmul.img=(comp.real*comp1.img)+(comp.img*comp1.real);
printf("\nMultiplication of two complex nos:");
printf("%di%d",cmul.real,cmul.img);
}
85. /*Calculating Power Using Functions*/
main()
{
int b,p;
long int res;
clrscr();
printf("/nEnter base,power:");
scanf("%d%d",&b,&p);
res=power(b,p);
printf("/n%d^%d=%d",b,p,res);
getch();
}
/*power function*/
power(int b,int p)
{
long int i,res=1;
for(i=1;i<=p;i++)
res=res*b;
return res;
}
86. /*Amstrong Number Using Functions*/
main()
{
long int n;
clrscr();
printf("/nenter no:");
scanf("%ld",&n);
amstrom(n);
/*we are passing 'n' value as the argument
to the function amstrom and 'n' is the
actual argument*/
getch();
}
amstrom(a)
/*'a' is the formal argument and it gets the
value of 'n' */
int a;
{
int c,s=0,d;
c=a;
while(a!=0)
{
d=a%10;
s=s+(d*d*d);
a=a/10;
}
if(s==c)
printf("/nAmstrom number");
else
printf("/nNot amstrom number");
}
87. /*Finding Factorial Of A Number*/
main()
{
int n,f;
clrscr();
printf("/nEnter number:");
scanf("%d",&n);
f=fact(n);
printf("/n%d!=%d",n,f);
getch();
}
int fact(n)
int n;
{
int i,f=1;
for(i=n;i>=1;i--)
f=f*i;
return f;
}
88. /* Finding LCM of two numbers*/
#include<stdio.h>
main()
int a,b,l;
clrscr();
printf("/nEnter nos:");
scanf("%d%d",&a,&b);
l=lcm(a,b);
printf("Lcm=%d",l);
getch();
}
lcm(int a,int b)
{
int i=1;
while(1)
{
if(i%a==0 && i%b==0)
return i;
i++;
}
}
o/p:
Enter nos:35 50
Lcm=350
89. /*Finding GCD of two numbers*/
#include<stdio.h>
main()
{
int a,b,l;
clrscr();
printf("/nEnter nos:");
scanf("%d%d",&a,&b);
l=gcd(a,b);
printf("GCD=%d",l);
getch();
}
gcd(int a,int b)
{
int c=0;
while(1)
{
c=a%b;
if(c==0)
return b;
a=b;
b=c;
}
}
o/p:
Enter nos:200 800
GCD=200
90. /*Evaluating expression*/
#include<stdio.h>
#include<math.h>
main()
int x;
double sum=0;
clrscr();
printf("\nEnter x value:");
scanf("%d",&x);
sum=1-pow(x,2)/fact(2)+pow(x,4)/fact(4)-pow(x,6)/fact(6)
+pow(x,8)/fact(8)-pow(x,10)/fact(10);
printf("%f",sum);
getch();
/*fact function*/
fact(int f)
{
long int res=1,i;
for(i=1;i<=f;i++)
res=res*i;
return(res);
}
91. /*Matrices Addition Using Functions*/
#include<stdio.h>
main()
{
int i,j,r,c;
static int a[20][20],b[20][20];
clrscr();
printf("Enter no.of rows:");
scanf("%d",&r);
printf("Enter no.of columns:");
scanf("%d",&c);
printf("Enter Matrix A:");
for(i=0;i<=r-1;i++)
{
for(j=0;j<=c-1;j++)
scanf("%d",&a[i][j]);
}
printf("Enter Matrix B:");
for(i=0;i<=r-1;i++)
{
for(j=0;j<=c-1;j++)
scanf("%d",&b[i][j]);
}
printf("\nSum of two matrices=\n");
matadd(a,b,c,r);
getch();
}
/*Addition function*/
int matadd(int a[20][20],int b[20][20],int c,int r)
{
static int sum[20][20],i,j;
for(i=0;i<=r-1;i++)
{
for(j=0;j<=c-1;j++)
{
sum[i][j]=a[i][j]+b[i][j];
printf("%4d",sum[i][j]);
}
printf("\n");
}
92. /*Matrices Multiplication Using Functions*/
#include<stdio.h>
main()
{
int ra,ca,rb,cb;
clrscr();
printf("Enter no.of rows for Matrix A:");
scanf("%d",&ra);
printf("Enter no.of columns:");
scanf("%d",&ca);
printf("Enter no.of rows for Matrix B:");
scanf("%d",&rb);
printf("Enter no.of columns for Matrix B:");
scanf("%d",&cb);
if(ca!=rb)
printf("Matrix Multiplication Not Possible");
else
matmul(ra,ca,rb,cb);
getch();
}
/*Matmul() function*/
matmul(ra,ca,rb,cb)
int ra,ca,rb,cb;
{
static int i,j,k,b[20][20],a[20][20],mul[20][20];
printf("Enter Matrix A:");
for(i=0;i<=ra-1;i++)
{
for(j=0;j<=ca-1;j++)
scanf("%d",&a[i][j]);
}
printf("Enter Matrix B:");
for(i=0;i<=rb-1;i++)
{
for(j=0;j<=cb-1;j++)
scanf("%d",&b[i][j]);
}
printf("Multiplication of two matrices=\n");
for(i=0;i<=ra-1;i++)
{
for(j=0;j<=cb-1;j++)
{
for(k=0;k<=rb-1;k++)
mul[i][j]=mul[i][j]+(a[i][k]*b[k][j]);
printf("%4d",mul[i][j]);
}
printf("\n");
}
}
93. /*Passing Array As An Argument Of A Function*/
main()
int n,i;
float a[10],res;
clrscr();
printf("/n enter no.of values");
scanf("%d",&n);
printf("/nenter values for array a:");
for(i=0;i<=n-1;i++)
scanf("%f",&a[i]);
res=sum(a,n);
printf("/nSum=%f",res);
getch();
}
sum(a,n)
float a[];
int n;
{
int j;
float s=0;
for(j=0;j<=n-1;j++)
s=s+a[i];
return s;
}
o/p:
enter no.of values5
enter values for array a:
2.5
6.2
2.3
1.2
5.8
Sum=18.000000
94./*What Happens When Formal Arguments Are More Than Actual Arguments*/
main()
{
int x,y,c;
clrscr();
printf("/nenter 2 nos:");
scanf("%d%d",&x,&y);
c=add(x,y);
printf("/nsum=%d",c);
getch();
}
int add(int a,int b,int c)
{
int s=0;
s=a+b;
printf("a=%d/tb=%d/tc=%d",a,b,c);
return s;
}
95. /*Finding Factorial Using Recurssion*/main()
{
int n;
clrscr();
printf("/nEnter number:");
scanf("%d",&n);
printf("/n%d!=%d",n,fact(n));
getch();
}
int fact(n)
int n;
{
if(n==0 || n==1)
return 1;
else
return (n*fact(n-1));
}
o/p:
Enter number:6
6!=720
96. /*Generating Fibnacci Series Using Recurssion*/
main()
{
int n,f,i;
clrscr();
printf("/nEnter no.of terms:");
scanf("%d",&n);
for(i=0;i<=n-1;i++)
printf("%5d",fibnacci(i));
getch();
}
/*fibnacci series*/
fibnacci(int n)
{
if(n==0 || n==1)
return 1;
else
return(fibnacci(n-1)+fibnacci(n-2));
}
o/p:
Enter no.of terms:12
1
1
2
3
13
21
34
55
89
144
int r,f;
r=x-(x/y * y);
if(r==0)
return(y);
else
f=hcf(y,r);
return f;
enter elements 2 3 5 6 0
Elements in the reverse order
0
6
5
3
o/p:
enter elements
2
3
6
-6
-3
smallest element in the array-6
103. /*program To Reverse The String Using Pointors*/
#include<stdio.h>
main()
{
char *s,*c;
static int i=0,j=0,len;
clrscr();
printf("/nEnter string:");
gets(s);
len=strlen(s);
for(i=len-1;i>=0;i--)
{
*(c+j)=*(s+i);
j++;
}
*(c+j)='/0';
printf("/nReverse string:");
puts(c);
getch();
}
o/p:
Enter string:abc
Reverse string:cba