Sei sulla pagina 1di 10

/* CHARACTER SORT*/

#include <stdio.h>
#include <string.h>
void main()
{
char name[10][8], temp[8];
int i, j, n;
printf("Enter the value of n \n");
scanf("%d", &n);
printf("Enter %d names n\n",n);
for (i = 0; i < n; i++)
{
scanf("%s", name[i]);
}
for (i = 0; i < n - 1 ; i++)
{
for (j = i + 1; j < n; j++)
{
if (strcmp(name[i], name[j]) > 0)
{
strcpy(temp, name[i]);
strcpy(name[i], name[j]);
strcpy(name[j], temp);
}
}
}
printf("Given names are:\n");
for(i = 0; i < n; i++)
{
printf("%s\n", name[i]);
}
}
OUTPUT
$ cd razi
$ gcc sort.c
$ ./a.out
Enter the value of n
3
Enter 3 names n
islam
zeejah
pp
Given names are:
islam
pp
zeejah

/*MATRIX SUM AND DIFFERENCE*/


#include<stdio.h>
void main()
{
int a[10][10],b[10][10],e[10][10],d[10][10],r,c,i,j;
printf("Enter the row nd column size of the matrix:\n");
scanf("%d %d",&r,&c);
printf("Enter the elemnts of the 1st matirx:\n");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&a[i][j]);
printf("Enter the elemnts of the 2nd matrix:\n");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&b[i][j]);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
{
d[i][j]=0;
e[i][j]=0;
e[i][j]=a[i][j]+b[i][j];
d[i][j]=a[i][j]-b[i][j];
}
printf("Sum:\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d",e[i][j]);
printf("\t");
}
printf("\n");
}
printf("Difference:\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d",d[i][j]);
printf("\t");
}
printf("\n");
}
}
OUTPUT
$ cd amjad
$ gcc sdmatrix.c
$ ./a.out
Enter the row nd column size of the matrix:
2

2
Enter the elemnts of the 1st matirx:
2

7
8
Enter the elemnts of the 2nd matrix:
1

Sum:
3

13
12
Difference:
1

/*MATRIX MULTIPLICATION*/
#include<stdio.h>
void main()
{
int a[20][20],b[20][20],c[20][20],k,i,j,r1,r2,c1,c2;
printf("Enter the row nd column size of first matrix:\n");
scanf("%d %d",&r1,&c1);
printf("Enter the row nd column size of 2nd matrix:\n");
scanf("%d %d",&r2,&c2);
if(c1!=r1&&c2!=r2)
printf("MULTPILCATION NOT POSSIBLE \n");
else
{
printf("Enter the elements of first matrix:\n");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
printf("Enter the elememts of second matrix:\n");
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
scanf("%d",&b[i][j]);
for(i=0;i<r1;i++)
{

for(j=0;j<c2;j++)
{
c[i][j]=0;
for(k=0;k<c1;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("Product :\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
printf("%d",c[i][j]);
printf("\t");
}
printf("\n");
}
}
}
OUTPUT
$ cd razi
$ gcc matmul.c
$ ./a.out
Enter the row nd column size of first matrix:
2
2
Enter the row nd column size of 2nd matrix:
2
2
Enter the elements of first matrix:
1
2
3
4
Enter the elememts of second matrix:
5
6
7
8
Product :
19
22
43
50
$

/*BINARY SEARCH*/
#include<stdio.h>
void main()
{
int a[10],low,mid,high,i,key,n,c=0;
printf("Enter the no. of elemnts:\n");
scanf("%d",&n);
printf("Enter the elemnts in ascendng order:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the key to be searched:\n");
scanf("%d",&key);
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(key==a[mid])
{
c=1;
break;
}
else if(key<a[mid])
high=mid-1;
else
low=mid+1;
}
if(c==0)
{
printf("No. not found:\n");
}
else
printf("No. %d is found at position %d:\n",key,mid+1);
}
OUTPUT
$ cd razi
$ gcc bisrch.c
$ ./a.out
Enter the no. of elemnts:
5
Enter the elemnts in ascendng order:
1
2
3
4
5
Enter the key to be searched:
3
No. 3 is found at position 3

/*SUM OF NOS USING POINTER*/


#include<stdio.h>
void main()
{
int a[20],i,n,sum=0;
int *ptr;
printf("Enter the no. of elements:\n");
scanf("%d",&n);
printf("Enter the nos.:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(ptr=a;ptr<a+n;ptr++)
{
sum=sum+*ptr;
}
printf("Sum= %d",sum);
}
OUTPUT
$ cd razi
$ gcc supntr.c
$ ./a.out
Enter the no. of elements:
5
Enter the nos.:
1
2
3
4
5
Sum= 15$
/*SAWP USING POINTER*/
#include<stdio.h>
void main()
{
int swap(int *x,int *y);
int a,b;
printf("Enter the two elemnts:\n");
scanf("%d %d",&a,&b);
printf("Nos before swapping are %d %d\n",a,b);
swap(&a,&b);
printf("Nos aftr swapping are %d %d\n ",a,b);
}
int swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;

}
OUTPUT
$ cd razi
$ gcc swapptr.c
$ ./a.out
Enter the two elemnts:
1
6
Nos before swapping are 2 6
Nos aftr swapping are 6 2
$

/*STRUCTURE*/
#include<stdio.h>
struct student
{
char name[30];
int sub1,sub2,sub3,total;
float percent;
};
int main()
{
int i,n;
struct student s[20];
printf("Enter the no. of students:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the student name:\n");
scanf("%s",&s[i].name);
printf("Enter the mark of sub1,2,3\n");
scanf("%d %d %d",&s[i].sub1,&s[i].sub2,&s[i].sub3);
}
printf("List of student scored more than 60%\n");
for(i=0;i<n;i++)
{
s[i].total=s[i].sub1+s[i].sub2+s[i].sub3;
s[i].percent=(s[i].total/3);

if(s[i].percent>=60)
printf("%s \t %f\t",s[i].name,s[i].percent);
}
}
OUTPUT
$ cd razi
$ gcc struct.c
$ ./a.out
Enter the no. of students:
3
Enter the student name:
zeejah
Enter the mark of sub1,2,3
100
100
100
Enter the student name:
pp
Enter the mark of sub1,2,3
50
20
60
Enter the student name:
razi
Enter the mark of sub1,2,3
80
82
78
List of student scored more than 60%
zeejah 100.000000 islam

/*FILE*/

80.000000

#include<stdio.h>
void main()
{
FILE *fp;
char ch;
int c=0,l=0,t=0,b=0;
fp=fopen("leapyr.c","r");
while(1)
{
ch=fgetc(fp);
if(ch==EOF)
break;
c++;
if(ch=='1')
b++;
if(ch=='\n')
l++;
if(ch=='\t')
t++;
}
fclose(fp);
printf("No. of char=%d\n",c);
printf("No. of blanks= %d \n",b);
printf("No. of tabs=%d \n",t);
printf("No. of lines=%d \n",l);
}
OUTPUT
$ cd razi
$ gcc file.c
$ ./a.out

No. of char=242
No. of blanks= 1
No. of tabs=0
No. of lines=11
$
s

Potrebbero piacerti anche