1.Write a program to search for an element form an array input from the user.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("How many element in your array : ");
scanf("%d",&n);
printf("\nEnter elements :");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEnter a elemnt for search :");
scanf("%d",&p);
printf("\n\nsearching...");
for(i=0;i<n;i++)
{
if(a[i]==p)
{
flag = 1;
break;
}
}
if(flag==1)
printf("\n\n%dth element match",i+1);
else
printf("\n\nValue not found");
getch();
}
2,Inserting an element into a position of an array.The element and the insertion point are inputs from the user.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[40],i,n,e,p,j;
clrscr();
printf("How many element in your array : ");
scanf("%d",&n);
printf("\nEnter value of elements :");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("You insert\n");
for(i=0;i<n;i++)
printf("%3d",a[i]);
printf("\n\nEnter an elemnt for insertion :");
scanf("%d",&e);
printf("\n\nEnter the position for insertion :");
scanf("%d",&p);
for(j=n;j>=p-1;j--)
{
a[j]=a[j-1];
}
a[p-1]=e;
printf("After insertion :\n\n");
for(i=0;i<n+1;i++)
printf("%3d",a[i]);
getch();
}
3.Inserting a number into the proper position of an array which is stored in ascending/descending order.
Ascending order
---------------------------
#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],i,j,n,temp,p;
clrscr();
printf("Enter how many elements in your array? :");
scanf("%d",&n);
printf("Enter the elements :");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\n\nEnter a number for insert : ");
scanf("%d",&p);
printf("\n\nAfter insertion array in assending order : ");
for(i=0;i<n;i++)
{
if(a[i]>p)
break;
}
for(j=n;j>=i;j--)
a[j]=a[j-1];
a[i]=p;
for(i=0;i<n+1;i++)
printf("%3d",a[i]);
getch();
}
Descending order
-----------------------
#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],i,j,n,temp,p;
clrscr();
printf("Enter how many elements in your array? :");
scanf("%d",&n);
printf("Enter the elements :");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\n\nEnter a number for insert in the array : ");
scanf("%d",&p);
printf("\n\nAfter insertion array in descending order : ");
for(i=0;i<n;i++)
{
if(a[i]<p)
break;
}
for(j=n;j>=i;j--)
a[j]=a[j-1];
a[i]=p;
for(i=0;i<n+1;i++)
printf("%3d",a[i]);
getch();
}
4.Deleting an element form an array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[40],i,n,flag,p,j;
clrscr();
printf("How many element in your array : ");
scanf("%d",&n);
printf("\nEnter value of elements :");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n\nEnter a elemnt for delete :");
scanf("%d",&p);
for(i=0;i<n;i++)
{
if(a[i]==p)
{
flag = 1;
break;
}
}
if(flag==0)
printf("\n\nNot found");
else
for(j=i;j<n;j++)
a[j]=a[j+1];
for(i=0;i<n-1;i++)
printf("%d ",a[i]);
getch();
}
5.Write a program to find out the maximum,minimum,medien mode of an array of numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],i,j,n,temp,x,y,sum=0;
float aver;
clrscr();
printf("Enter how many elements in your array? :");
scanf("%d",&n);
printf("\n\nEnter the elements :\n\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
for(i=0;i<n;i++)
printf("%3d",a[i]);
printf("\n\nMaximum is %d",a[n-1]);
printf("\n\nMinimum is %d",a[0]);
if(n%2!=0)
{
x=((n-1)/2)+1;
printf("\n\nMedian is %d",a[x-1]);
}
else
{
x=(n/2);
y=(n/2)+1;
printf("\n\nMedian is %d & %d ",a[x-1],a[y-1]);
}
for(i=0;i<n;i++)
sum=sum+a[i];
aver=sum/(n-1);
printf("\n\nMode is %.3f",aver);
getch();
}
6.Find k-th maximum and k-th minimum from an array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],i,j,n,temp,k;
clrscr();
printf("Enter how many elements in your array? :");
scanf("%d",&n);
printf("Enter the elements :");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0;i<n;i++)
printf("%3d",a[i]);
printf("\n\nEnter kth maximum & kth minimum :");
scanf("%d",&k);
printf("\n\n%dth maximum is %d",k,a[n-k]);
printf("\n\n%dth minimum is %d",k,a[k-1]);
getch();
}
7.Write a program for deleting duplicate elements from array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[40],i,n,p,j,k,count=0;
clrscr();
printf("How many element in your array : ");
scanf("%d",&n);
printf("\n\nEnter value of elements :\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n\nYour array :\n\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
p=n;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if((a[i]==a[j]) && (i!=j))
{
for(k=i;k<p;k++)
{
a[k]=a[k+1];
}
p=p-1;
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if((a[i]==a[j]) && (i!=j))
{
count=count+1;
}
printf("\n\nDeleting duplicate elements :\n\n");
for(i=0;i<n-count;i++)
printf("%d ",a[i]);
getch();
}
8.Write program to find out the common elements from an array.
#include<stdio.h>
#include<conio.h>
void main()
{
int x[150],y[150],i,j,m,n;
clrscr();
printf("Number of elements in first Array");
scanf("%d",&a);
printf("Number of elements in second Array ");
scanf("%d",&b);
printf("First Array \n");
for(i=0;i<a;i++)
scanf("%d",&x[i]);
printf("Second Array\n");
for(j=0;j<b;j++)
scanf("%d",&y[j]);
for(i=0;i<a;i++)
for(j=0;j<b;j++)
if(x[i]==y[j])
printf("Common charater is %d\n",x[i]);
getch();
}
12.Write a program to marge two sorted arrays.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[40],b[40],c[80],i,n,j,m,d,temp;
clrscr();
printf("How many element in your array : ");
scanf("%d",&n);
printf("\n\nEnter value of elements :\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n\nYour array1 :\n\n");
for(i=0;i<n;i++)
printf("%3d",a[i]);
printf("\n\nHow many element in your array : ");
scanf("%d",&m);
printf("\n\nEnter value of elements :\n");
for(i=0;i<m;i++)
scanf("%d",&b[i]);
printf("\n\nYour array2 :\n\n");
for(i=0;i<m;i++)
printf("%3d",b[i]);
printf("\n\nMarge array\n\n");
for(i=0;i<n;i++)
c[i]=a[i];
j=m-1;
for(i=(m+n-1);i>=n;i--)
{
c[i]=b[j];
j=j-1;
}
for(i=0;i<(n+m);i++)
printf("%3d",c[i]);
d=m+n;
for(i=0;i<d-1;i++)
for(j=i+1;j<d;j++)
if(c[i]>c[j])
{
temp=c[i];
c[i]=c[j];
c[j]=temp;
}
printf("\n\nArray in assending order\n\n");
for(i=0;i<d;i++)
printf("%3d",c[i]);
getch();
}
14.Write a program to count the number of letters within a text.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char i,ch[100];
int len;
clrscr();
gets(ch);
len=strlen(ch);
printf("%d chracter .",len);
getch();
}
15.Write a program which will search for a substring within a string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char i,*st1,*st2,*ptr;
int len;
clrscr();
printf("Enter string1:\n");
gets(st1);
printf("\nEnter substring for search in string1:\n");
gets(st2);
ptr=strstr(st1,st2);
if(ptr!="")
printf("\n\n%s is a part of the string %s .",st2,st1);
else
printf("\n\n%s is not a part of the string %s.",st2,st1);
getch();
}
16.Take n numbers as input from the user. Find out their GCD and LCM .
GCD
#include<stdio.h>
#include<conio.h>
void main()
{
int m[25],n,i,j,temp;
clrscr();
printf("How many number for 'GCD' :\n");
scanf("%d",&n);
printf("Enter %d positive numbers :",n);
for(i=0;i<n;i++)
scanf("%d",&m[i]);
for(i=0;i<n-1;i++)
for(j=0;j<n;j++)
while(m[j+1]!=0)
{
temp=m[j]%m[j+1];
m[j]=m[j+1];
m[j+1]=temp;
}
printf("The Greatest common Factor is :");
printf("%4d",m[0]);
getch();
}
LCM
#include<stdio.h>
#include<conio.h>
void main()
{
int m[25],a,b,n,i,temp;
clrscr();
printf("How many number for 'LCM' :\n");
scanf("%d",&n);
printf("Enter %d positive numbers :",n);
for(i=0;i<n;i++)
scanf("%d",&m[i]);
for(i=0;i<n-1;i++)
{
a=m[i];
b=m[i+1];
while(b!=0)
{
temp=a%b;
a=b;
b=temp;
}
m[i+1]=a*m[i]/a*m[i+1]/a;
}
printf("The Lowest common Factor is :");
printf("%d",m[n-1]);
getch();
}
17.Write a program to Add / Substract two different Matrix,input from the user.
(i)ADD
#include<stdio.h>
#include<conio.h>
void main()
{
int m1[3][3],m2[3][3],m3[3][3],n,i,j;
clrscr();
printf("Enter element for Matric1(3x3) :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&m1[i][j]);
}
printf("Enter element for Matric2(3x3) :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&m2[i][j]);
}
printf("\nMatrix1\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%3d",m1[i][j]);
printf("\n");
}
printf("Matrix2\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%3d",m2[i][j]);
printf("\n");
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
m3[i][j]=m1[i][j]+m2[i][j];
}
printf("adding two Matrix\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%4d",m3[i][j]);
printf("\n");
}
getch();
}
(ii)Substruct
#include<stdio.h>
#include<conio.h>
void main()
{
int m1[3][3],m2[3][3],m3[3][3],n,i,j;
clrscr();
printf("Enter element for Matric1(3x3) :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&m1[i][j]);
}
printf("Enter element for Matric2(3x3) :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&m2[i][j]);
}
printf("\nMatrix1\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%3d",m1[i][j]);
printf("\n");
}
printf("Matrix2\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%3d",m2[i][j]);
printf("\n");
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
m3[i][j]=m1[i][j]-m2[i][j];
}
printf("Substruction of two Matrix\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%4d",m3[i][j]);
printf("\n");
}
getch();
}
1.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[40],i,n,k,j,m=0,temp;
clrscr();
printf("How many element in your array : ");
scanf("%d",&n);
printf("\n\nEnter value of elements :\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n\nYour array :\n\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
for(i=0;i<n;++i)
{
for(j=i+1;j<(n-m);++j)
{
if(a[i]==a[j])
{
++temp;
break;
}
}
for(k=j;k<=n;++k)
a[k]=a[k+1];
++m;
}
for(i=0;i<(n-temp);i++)
printf("%d ",a[i]);
getch();
}
2.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char i,ch[100],ch1[100],*c;
int value;
clrscr();
gets(ch);
strcpy(ch1,ch);
strrev(ch);
value=strcmp(ch,ch1);
if(value==0)
printf("Palindrom");
else
printf("Not Palindrom");
getch();
}
3.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[40],b[40],c[80],i,n,j,m,d,temp;
clrscr();
printf("How many element in your array : ");
scanf("%d",&n);
printf("\n\nEnter value of elements :\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n\nYour array1 :\n\n");
for(i=0;i<n;i++)
printf("%3d",a[i]);
printf("\n\nHow many element in your array : ");
scanf("%d",&m);
printf("\n\nEnter value of elements :\n");
for(i=0;i<m;i++)
scanf("%d",&b[i]);
printf("\n\nYour array2 :\n\n");
for(i=0;i<m;i++)
printf("%3d",b[i]);
printf("\n\nMarge array\n\n");
for(i=0;i<n;i++)
c[i]=a[i];
j=m-1;
for(i=(m+n-1);i>=n;i--)
{
c[i]=b[j];
j=j-1;
}
for(i=0;i<(n+m);i++)
printf("%3d",c[i]);
d=m+n;
for(i=0;i<d-1;i++)
for(j=i+1;j<d;j++)
if(c[i]>c[j])
{
temp=c[i];
c[i]=c[j];
c[j]=temp;
}
printf("\n\nArray in assending order\n\n");
for(i=0;i<d;i++)
printf("%3d",c[i]);
getch();
}
4.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
void scanl(char line[],int *l,int *d,int *w,int *o);
void main()
{
char line[150];
int l=0,d=0,w=0,o=0;
clrscr();
printf("\nEnter a line :\n ");
scanf("%[\n]",&line);
scanl(line,&l,&d,&w,&o);
printf("\nNo of letter : %d",l);
printf("\nNo of digit : %d",d);
printf("\nNo of Blankspace : %d",w);
printf("\nNo of other : %d",o);
getch();
}
void scanl(char line[],int *l,int *d,int *w,int *o)
{
int ch;
int count=0;
while(line[count])!='\0' )
{
ch=toupper(line[count]);
if(ch>='A' && ch<='z')
++*l;
else if(ch>='0' && ch<='9')
++*d;
else if(ch==' ' || ch=='\t')
++*w;
else
++*o;
++count;
}
return;
}
কোন মন্তব্য নেই:
একটি মন্তব্য পোস্ট করুন