21. Given the two 1-D arrays A and B, which are sorted in ascending order.
Shared By : diz (SVICS - KADI) Show/Hide Program
#include<stdio.h>
#include<conio.h>
int main()
{
int a[10],b[10],c[20],n1,n2,i,j,temp,k=0;
clrscr();
printf(" Enter the no. of element for 1st array : ");
scanf("%d",&n1);
for(i=0;i<n1;i++,k++)
{
printf(" Enter element [%d] : ",i+1);
scanf("%d",&a[i]);
c[k]=a[i];
}
for(i=0;i<n1;i++)
{
for(j=i+1;j<n1;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\n After sorting 1st array : ");
for(i=0;i<n1;i++)
{
printf("\n Element [%d] = %d",i+1,a[i]);
}
printf("\n\n Enter the no. of element for 2nd array : ");
scanf("%d",&n2);
for(i=0;i<n2;i++,k++)
{
printf(" Enter element [%d] : ",i+1);
scanf("%d",&b[i]);
c[k]=b[i];
}
for(i=0;i<n2;i++)
{
for(j=i+1;j<n2;j++)
{
if(b[i]>b[j])
{
temp=b[i];
b[i]=b[j];
b[j]=temp;
}
}
}
printf("\n After sorting 2nd array : ");
for(i=0;i<n2;i++)
{
printf("\n Element [%d] = %d",i+1,b[i]);
}
for(i=0;i<n1+n2;i++)
{
for(j=i+1;j<n1+n2;j++)
{
if(c[i]>c[j])
{
temp=c[i];
c[i]=c[j];
c[j]=temp;
}
}
}
printf("\n\n\n After combined and sorted both array :- ");
for(i=0;i<n1+n2;i++)
{
printf("\n Element [%d] = %d",i+1,c[i]);
}
getch();
return 0;
}
Output:
Enter the no. of element for 1st array : 5
Enter element [1] : 20
Enter element [2] : 18
Enter element [3] : 6
Enter element [4] : 12
Enter element [5] : 4
After sorting 1st array :
Element [1] = 4
Element [2] = 6
Element [3] = 12
Element [4] = 18
Element [5] = 20
Enter the no. of element for 2nd array : 3
Enter element [1] : 6
Enter element [2] : 2
Enter element [3] : 3
After sorting 2nd array :
Element [1] = 2
Element [2] = 3
Element [3] = 6
After combined and sorted both array :-
Element [1] = 2
Element [2] = 3
Element [3] = 4
Element [4] = 6
Element [5] = 6
Element [6] = 12
Element [7] = 18
Element [8] = 20
22. Write a program to add two matrices.
#include<stdio.h>
#include<conio.h>
int main()
{
int a[5][5],b[5][5],c[5][5],i,j,m,n;
clrscr();
printf("Enter the no of rows:");
scanf("%d",&m);
printf("Enter the no of columns:");
scanf("%d",&n);
printf("Enter First matrix\n");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
scanf("%d",&a[i][j]);
}
printf("Enter Second matrix\n");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
scanf("%d",&b[i][j]);
}
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
c[i][j]=a[i][j]+b[i][j];
}
printf("Addition of matrix\n");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
printf("%d ",c[i][j]);
printf("\n");
}
getch();
return 0;
}
Output:
Enter the no of rows:3
Enter the no of columns:3
Enter First matrix
5 9 3
7 2 5
9 3 1
Enter Second matrix
4 1 2
6 6 9
5 2 9
Addition of matrix
9 10 5
13 8 14
14 5 10
23. Write a program to find string length.
#include<stdio.h>
#include<conio.h>
int main()
{
char str[100];
int l;
clrscr();
printf("Enter the string:");
gets(str);
for(l=0; str[l]; l++);
printf("The length of %s is -> %d",str,l);
getch();
return 0;
}
Output:
Enter the string:Diz
The length of Diz is -> 6
24. Write a program to print size of int, float, double variable.
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
printf("Size of int is -> %d\n",sizeof(int));
printf("Size of long is -> %d\n",sizeof(float));
printf("Size of double is -> %d\n",sizeof(double));
getch();
return 0;
}
Output:
Size of int is -> 2
Size of long is -> 4
Size of double is -> 8
25. Write a program that will read a text and count all occurrences of a particular word.
#include<stdio.h>
#include<conio.h>
int main()
{
char str[100],find[100];
int i,j,cnt=0,flag;
clrscr();
printf("Enter the string :");
gets(str);
printf("Enter the srting that you want to find : ");
gets(find);
for(i=0;str[i];i++)
{
flag=0;
if(str[i]==find[0])
{
flag=1;
for(j=1;find[j];j++)
{
if(str[i+j]!=find[j])
{
flag=0;
break;
}
}
if(flag==1)
cnt++;
}
else
{
}
}
printf("%s occurs in %s %d times\n",find,str,cnt);
getch();
return 0;
}
Enter the string :The cat & The dog
Enter the srting that you want to find : The
The occurs in The cat & The dog 2 times
26. Write a program that will read a string and rewrite it in the alphabetical order.
#include<stdio.h>
#include<conio.h>
int main()
{
char str[100],temp;
int i,j;
clrscr();
printf("Enter the string :");
gets(str);
printf("%s in ascending order is -> ",str);
for(i=0;str[i];i++)
{
for(j=i+1;str[j];j++)
{
if(str[j]<str[i])
{
temp=str[j];
str[j]=str[i];
str[i]=temp;
}
}
}
printf("%s\n",str);
getch();
return 0;
}
Output:
Enter the string :Diz
Diz in ascending order is ->
27. Write a program that appends the one string to another string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char str[100],app[20];
clrscr();
printf("Enter the string :");
gets(str);
printf("Enter the srting that you want to append : ");
gets(app);
printf("The string is : %s\n",str);
printf("The string you want to append is : %s\n",app);
printf("The string after append is : %s\n",strcat(str,app));
getch();
return 0;
}
28. Write a program that finds a given word in a string.
#include<stdio.h>
#include<conio.h>
int main()
{
char str[100],find[100];
int i,j,flag;
clrscr();
printf("Enter the string :");
gets(str);
printf("Enter the srting that you want to find : ");
gets(find);
for(i=0;str[i];i++)
{
if(str[i]==find[0])
{
flag=1;
for(j=1;find[j];j++)
{
if(str[i+j]!=find[j])
{
flag=0;
break;
}
}
if(flag==1)
break;
}
}
if(flag==1)
printf("%s is found in %s at %d position",find,str,i+1);
else
printf("%s is not found in %s",find,str);
getch();
return 0;
}
Output:
Enter the string :The Cat & The Dog
Enter the srting that you want to find : Dog
Dog is found in The Cat & The Dog at 15 position
29. Use recursive calls to evaluate
#include<stdio.h>
#include<conio.h>
int main()
{
float series(float,int),x;
int n;
clrscr();
printf("\nEnter X:");
scanf("%f",&x);
printf("\nEnter n:");
scanf("%d",&n);
printf("\nAns %f",series(x,n));
getch();
return 0;
}
float series(float x,int n)
{
long factorial(int);
float power(float,int);
float sum=0;
int i,s=1;
for(i=1;i<=n;i+=2)
{
sum+=(power(x,i)/factorial(i))*s;
s*=-1;
}
return sum;
}
float power(float x, int y)
{
float p=1;
int i;
for(i=1;i<=y;i++)p*=x;
return p;
}
long factorial(int p)
{
long f=1;
int i;
for(i=1;i<=p;i++)f*=i;
return f;
}
/*
******Output******
Enter X:1.2
Enter n:5
Ans 0.932736
30. Write a function prime that returns 1 if its argument is a prime no. and returns 0otherwise.
#include<stdio.h>
#include<conio.h>
int prime(int);
int main()
{
int n,p;
clrscr();
printf("Enter a number : ");
scanf("%d",&n);
p=prime(n);
if(p==1)
printf("%d is prime\n",n);
else
printf("%d is not prime\n",n);
getch();
return 0;
}
int prime(int n)
{
int i;
for(i=2;i<n;i++)
{
if(n%i==0)
return 0;
}
return 1;
}
Output:
Enter a number : 18
18 is not prime
Enter a number : 5
5 is prime