Académique Documents
Professionnel Documents
Culture Documents
#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 rijin
$ gcc sort.c
$ ./a.out
Enter the value of n
3
Enter 3 names n
islam
zeejah
pp
Given names are:
islam
pp
zeejah
printf("%d",c[i][j]);
printf("\t");
}
printf("\n");
}
}
}
OUTPUT
$ cd rijin
$ 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 rijin
$ 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:
}
OUTPUT
$ cd amjad
$ 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 amjad
$ 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 amjad
$ 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
/*FILE*/
#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 amjad
$ gcc file.c
$ ./a.out
No. of char=242
No. of blanks= 1
No. of tabs=0
No. of lines=11
$