Tuesday, February 8, 2011

c-program

cprogramms

11. WRITE A C PROGRAM TO ARRANGE ALL CHARACTERS IN A STRING
IN ASCENDING ORDER.
*/

#include
#include
#define MAX 30

void main()
{
char str[MAX],*fr,*re,temp;
int i,j;
clrscr();
printf("\n Enter a String : ");
gets(str);
for(fr=str;(*fr)!='\0';fr++)
{
for(re=fr+1;(*re)!='\0';re++)
{
if((int)*re < (int)*fr)
{
temp=*re;
*re=*fr;
*fr=temp;
}
}
}

puts("\n String in Ascending Order:- ");
puts(str);
getch();
}

/*
=====OUT PUT=====

1. Enter a String : KAUNDINYA

String in Ascending Order:-
AADIKNNUY

2. Enter a String : BHARATH

String in Ascending Order:-
AABHHRT

3. Enter a String : ABCDEFGH

String in Ascending Order:-
ABCDEFGH

*/

/* 12. WRITE A C PROGRAM TO FIND IF THE GIVEN STRING IS A
PALLINDROME */

#include
#include

#define MAX 30

void main()
{
char str[MAX],*fr,*re,flag=0;
int len;
clrscr();
printf("Enter a String : ");
gets(str);
for(len=0;str[len]!='\0';len++);
fr=str; /* same as fr=&str[0];*/
re=&(str[len-1]); /* re is pointing to last element */
while(fr < re)
{
if((*fr)!=(*re))
flag=1;
fr++,re--;
}
if(!flag)
printf("The \"%s\" is a pallindrome",str);
else
printf("The \"%s\" is a not a pallindrome",str);
getch();

}

/*
=====OUT PUT=====

1.Enter a String : BHARATH
The "BHARATH" is a not a pallindrome

2.Enter a String : LIRIL
The "LIRIL" is a pallindrome

3.Enter a String : 12321
The "12321" is a pallindrome

*/


/* 13. WRITE A C PROGRAM TO INPUT STRINGS S1 AND S2 OF LENGTHS N AND
M RESPECTIVELY SUCH THAT M < N. FIND THE POSITION OF FIRST
OCCURENCE OF S2 IN S1
*/
#include
char Ms[50],Ss[30];
main()
{
char *ptr1,*ptr2;
int pos;
clrscr();
ptr1=Ms;
printf("\n ENTER THE FIRST STRING \n");
string_read(ptr1);
ptr2=Ss;
printf("\n ENTER THE SECOND STRING \n");
string_read(ptr2);
pos=string_compare(Ms,Ss);
if(pos!=0)
{
printf("\n \"%s\" is found at \"%s\" in the position %d.",Ss,Ms,pos);
}
else
printf("\n %s is not found in %s",Ss,Ms);
getch();
}

/* FUNCTION TO ACCEPT A STRING */
string_read(char *ptr)
{
while((*ptr=getchar())!='\n')
ptr++;
*ptr='\0';
}
/* FUNCTION TO COMPARE THE STRING */
string_compare(char Ms[], char Ss[])
{
int i,j,k;
for(i=0;Ms[i]!='\0';i++)
{
for(j=i,k=0;Ss[k]&&Ms[j]==Ss[k];j++,k++);
if(Ss[k]=='\0' && k>0)
return(i+1);
}
return(0);
}
/*
=====OUT PUT=====

1. ENTER THE FIRST STRING
bharath

ENTER THE SECOND STRING
rath

"rath" is found at "bharath" in the position 4.

2. ENTER THE FIRST STRING
JAYANTH

ENTER THE SECOND STRING
R

R is not found in JAYANTH

*/











/* 14. ACCEPT N WORDS (WHICH FORM A SENTENCE), ONE WORD AT A TIME.
OUTPUT IT ON THE SCREEN AS A SENTENCE WITH BLANKS BETWEEN WORDS, AND A FULLSTOP AT THE END.
*/
#include
#include

#define ROW 20
#define COL 30

void main()
{
char words[ROW][COL];
int i,j,n;
clrscr();
printf("Enter number of words : ");
scanf("%d",&n);

for(i=0;i {
printf("Enter %d word : ",i+1);
scanf("%s",&words[i]);
}
printf("\n The sentence is :\n\t\t");
for(i=0;i {
printf(" ");
printf("%s",words[i]);
}
printf(".");
getch();
}

/*
=====OUT OUT=====

1. Enter number of words : 3
Enter 1 word : I
Enter 2 word : LOVE
Enter 3 word : INDIA

The sentence is :
I LOVE INDIA.

*/





/* 15. INPUT A STRING AND FIND THE NUMBER OF NUMERIC CHARACTERS,
LOWERCASE AND UPPERCASE ALPHABETIC CHARACTERS RESPECTIVELY
AND ANY OTHER THAN THE ABOVE
*/
#include
#include
#include

#define MAX 50

#define ASCII_0 48
#define ASCII_9 57
#define ASCII_A 65
#define ASCII_Z 90
#define ASCII_a 97
#define ASCII_z 122

void main()
{
char str[MAX];
int i,num=0,low=0,up=0,other=0;
clrscr();
printf("Enter a String : ");
gets(str);
for(i=0;i {
if( (int)str[i]>=ASCII_0 && (int)str[i]<=ASCII_9 )
num++;
else if( (int)str[i]>=ASCII_A && (int)str[i]<=ASCII_Z )
up++;
else if( (int)str[i]>=ASCII_a && (int)str[i]<=ASCII_z )
low++;
else
other++;
}

printf("\nThe Number of NUMERIC characters is : %d ",num);
printf("\nThe Number of LOWERCASE characters is : %d ",low);
printf("\nThe Number of UPPERCASE characters is : %d ",up);
printf("\nThe Number of OTHER characters is : %d ",other);
getch();
}








/*
=====OUT PUT=====

Enter a String : KAUNDINYA is born in the year 1981.

The Number of NUMERIC characters is : 4
The Number of LOWERCASE characters is : 15
The Number of UPPERCASE characters is : 9
The Number of OTHER characters is : 7
*/

/* 16. INPUT A STRING AND FIND THE NUMBER OF TIMES, EACH OF THE FIVE
VOWELS APPEAR IN THE GIVEN STRING
*/
#include
#include
#include

#define MAX 50

void main()
{
char str[MAX];
int a=0,e=0,i=0,o=0,u=0,loop;
clrscr();
printf("Enter a string : ");
gets(str);
for(loop=0;loop {
switch(str[loop])
{
case 'a':
case 'A': a++; break;
case 'e':
case 'E': e++; break;
case 'i':
case 'I': i++; break;
case 'o':
case 'O': o++; break;
case 'u':
case 'U': u++; break;
}
}
printf("\n The number of a's in the string : %d ",a);
printf("\n The number of e's in the string : %d ",e);
printf("\n The number of i's in the string : %d ",i);
printf("\n The number of o's in the string : %d ",o);
printf("\n The number of u's in the string : %d ",u);
getch();
}
/*
=====OUT PUT=====

Enter a string : BHARATH AND KAUNDINYA did programing.

The number of a's in the string : 6
The number of e's in the string : 0
The number of i's in the string : 3
The number of o's in the string : 1
The number of u's in the string : 1

*/

/* 17. WRITE A C PROGRAM TO DELETE ALL THE OCCURENCES OF A
CHARACTER IN A STRING.
*/
#include
#include

#define MAX 50

void main()
{
char str[MAX],str2[MAX],ch;
int i,j;
clrscr();
printf("Enter a string : ");
gets(str);
printf("Enter a chracter to delete it's occurences : ");
scanf("%c",&ch);
for(i=0,j=0;str[i]!='\0';)
{
if(str[i]!=ch)
str2[j++]=str[i];
i++;
}
str2[j]='\0';

printf("The string after deleting \'%c\' is \n\t\t \"%s\" ",ch,str2);
getch();
}

/*
=====OUT PUT=====

Enter a string : BHARATH
Enter a chracter to delete it's occurences : H
The string after deleting 'H' is
"BARAT"
*/

/* 18. WRITE A C PROGRAM TO COPY THE CONTENTS OF ONE STRING TO
ANOTHER.
*/
#include
#include

#define MAX 50

void main()
{
char str1[MAX],str2[MAX];
int i,len;

clrscr();
printf("Enter a string : ");
gets(str1);

for(len=0;str1[len]!='\0';len++);

for(i=0;i str2[i]=str1[i];
str2[i]='\0';

printf("The string is : \"%s\" ",str2);
getch();
}

/*
=====OUT PUT=====

Enter a string : KAUNDINYA
The string is : "KAUNDINYA"

*/















/* 19. WRITE A C PROGRAM TO COUNT THE NUMBER OF WORDS IN A
SENTENCE.
*/
#include
#include

#define MAX 100

void main()
{
char str[MAX];
int words=0,i;
clrscr();

printf("Enter a sentence : ");
gets(str);

for(i=0;str[i]!='\0';i++)
if(str[i]==' ')
words++;

printf("The number of words in the sentence is %d",words+1);
getch();
}

/*

=====OUT PUT=====

Enter a sentence : BHARATH KAUNDUNYA SRIKANTH ARE GOOD FRIENDS.
The number of words in the sentence is 6

*/

















/* 20. WRITE A C PROGRAM TO COUNT THE NUMBER OF CHARACTERS,
WORDS AND LINES IN A TEXT.
*/
#include
#include

#define ROW 50
#define COL 100
#define CTRL_Q 17

void main()
{
char str[ROW][COL];
int ch=0,words=0,lines=0,i=0,j;
clrscr();

printf("Enter Text and press CTRL+q to end text\n\n");
while(i < ROW)
{
gets(str[i]);
if(str[i][0]==CTRL_Q)
break;
i++;
}
lines=i;

for(i=0;i {
for(j=0;j {
if(str[i][j]==' ')
words++;
else
ch++;
}
words++;
}

printf("\nThe Number of CHARACTERS is : %d", ch);
printf("\nThe Number of WORDS is : %d", words);
printf("\nThe Number of LINES is : %d", lines);
getch();
}







/*
=====OUT PUT=====

Enter Text and press CTRL+q to end text

ALL THE PROGRAMS ARE DONE BY BHARATH AND
KAUNDINYA. BHARATH DID MAXIMUM NUMBER OF
PROGRAMS AND KAUNDINYA HELPED HIM.

THEIR E-ids are
bha_pal@indiatimes.com
kaun_ignou@yahoo.com
^Q

The Number of CHARACTERS is : 153
The Number of WORDS is : 27
The Number of LINES is : 7
*/

/* 21. WRITE A FUNCTION TO FIND THE LENGTH OF A STRING. */

#include
#include

#define MAX 50

int length(char *p)
{
int len;
for(len=0 ; *p!='\0' ; p++,len++);
return len;
}

void main()
{
char str[MAX];
clrscr();
printf("Enter a string : ");
gets(str);
printf("\n\tThe length of the string is %d ",length(str));
getch();
}








/*
=====OUT PUT=====

Enter a string : KAUNDINYA

The length of the string is 9
*/

/* 22. WRITE A FUNCTION TO THE GCD OF TWO NUMBERS. USING GCD
FUNCTION,WRITE A MAIN PROGRAM TO FIND THE LCM OF FOUR
NUMBERS.
*/
#include
#include

int gcd(int x,int y)
{
int temp,rem;
if(x>y)
{
temp=x;
x=y;
y=temp;
}
while(rem!=0)
{
rem=y%x, y=x, x=rem;
}
return y;
}

void main()
{
int a,b,c,d;
int lcm1,lcm2,lcm;
clrscr();
printf("Enter four numbers : ");
scanf("%d %d %d %d",&a,&b,&c,&d);
lcm1=(a*b)/gcd(a,b);
lcm2=(c*d)/gcd(c,d);
lcm=(lcm1*lcm2)/gcd(lcm1,lcm2);
printf("The lcm of %d,%d,%d,%d is : %d ",a,b,c,d,lcm);
getch();
}






/*
LOGIC :
(a*b)
-> LCM(a,b) = ------------
GCD(a,b)
LCM(a,b) * LCM(c,d)
-> LCM of four numbers(a,b,c,d) = -----------------------------------
GCD( LCM(a,b),LCM(c,d) )
*/

/*
=====OUT PUT=====

Enter four numbers : 2 4 6 8
The lcm of 2,4,6,8 is : 24
*/

/* 23. WRITE A FUNCTION TO FIND THE FACTORIAL OF A NUMBER. USING
THIS FUNCTION FIND nCr.
n!
nCr = -----------
(n-r)! * r!
*/

#include
#include

long int fact(long int a)
{
if(a==0)
return 1;
else
return(a*fact(a-1));
}
void main()
{
long int ans,n,r;
clrscr();
printf("Enter n and r to calculate nCr : ");
scanf("%ld %ld",&n,&r);
if(n>=r)
{
ans=fact(n) / ( fact(n-r) * fact(r));
printf("\t\t%ld c %ld is : %ld",n,r,ans);
}
else
printf("\nERROR!!!\n\t %ld<%ld so nCr cannot be calculated",n,r);
getch();
}

/*
=====OUT PUT=====

1.Enter n and r to calculate nCr : 3 4

ERROR!!!
3<4 data-blogger-escaped-be="be" data-blogger-escaped-br="br" data-blogger-escaped-calculated="calculated" data-blogger-escaped-cannot="cannot" data-blogger-escaped-ncr="ncr" data-blogger-escaped-so="so">
2.Enter n and r to calculate nCr : 9 3
9 c 3 is : 84
*/

/* 24. WRITE A PROGRAM TO CHECK WHETHER A GIVEN ARRAY IS SORTED.
(ASCENDING,DESCENDING OR UNSORTED)
*/
#include
#include

#define MAX 50

void main()
{
int arr[MAX],asc=0,des=0,i,n;
clrscr();
printf("Enter the size of array : ");
scanf("%d",&n);
printf("Enter array elements : ");
for(i=0;i scanf("%d",&arr[i]);
for(i=0;i {
if(arr[i] asc++;
if(arr[i]>arr[i+1])
des++;
if(asc!=0 && des!=0)
break;
}

if(asc==n-1)
printf("The array is sorted in ASCENDING Order");
else if(des==n-1)
printf("The array is sorted in DESCENDING Order");
else
printf("This is an unsorted array");
getch();
}



/*
=====OUT PUT=====

1.Enter the size of array : 5
Enter array elements : 1 2 3 4 5
The array is sorted in ASCENDING Order

2.Enter the size of array : 5
Enter array elements : 9 8 7 6 5
The array is sorted in DESCENDING Order

3.Enter the size of array : 5
Enter array elements : 1 5 2 7 3 4
This is an unsorted array
*/

/* 25. WRITE A PROGRAM TO FIND THE LARGEST ELEMENT IN AN ARRAY AND
POSITION OF IT'S OCCURENCE
*/
#include
#include
#define MAX 50

void main()
{
int arr[MAX],pos=0,large=0,n,i;
clrscr();
printf("Enter the length of array : ");
scanf("%d",&n);
printf("Enter the elements of array : ");
for(i=0;i scanf("%d",&arr[i]);
for(i=0,large=arr[i],pos=i;i {
if(arr[i]>large)
large=arr[i],pos=i;
}
printf("\nThe largest number is %d",large);
printf("\n\t\tit occurs at position %d",pos+1);
getch();
}
/*
=====OUT PUT=====
Enter the length of array : 5
Enter the elements of array : 23 11 67 183 1

The largest number is 183
it occurs at position 4

*/
/* 26B. WRITE A PROGRAM TO SEARCH AN ITEM IN A LIST USING
BINARY SEARCH
*/
#include
#include

#define MAX 50

int binsrch(int arr[],int key,int n)
{
int high,low,mid,i;
for(low=0,high=n-1;low<=high;)
{
mid=(low+high)/2;
if(arr[mid]==key)
return(mid+1);
else if(arr[mid] low=mid+1;
else
high=mid-1;
}
return 0;
}

void main()
{
int arr[MAX],key,flag=0,i,j,temp,n;
clrscr();
printf("Enter the number of elements : ");
scanf("%d",&n);
printf("Enter the elements\n");
for(i=0;i scanf("%d",&arr[i]);
printf("Enter the element to search : ");
scanf("%d",&key);
/* Sorting in ascending order before binsrch*/
for(i=0;i {
for(j=i+1;j {
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}


flag=binsrch(arr,key,n);
clrscr();
printf("THE SORTED ARRAY IS...\n");
for(i=0;i printf("%d\n",arr[i]);
if(flag)
printf("\nKEY FOUND at POSITION : %d.",flag);
else
printf("\nKEY NOT FOUND!");
getch();
}

/*
=====OUT PUT=====

1.Enter the number of elements : 5
Enter the elements
12 2 14 78 9
Enter the element to search : 0
THE SORTED ARRAY IS...
2
9
12
14
78

KEY NOT FOUND!

2.Enter the number of elements : 5
Enter the elements
2 4 7 8 45
Enter the element to search : 4
THE SORTED ARRAY IS...
2
4
7
8
45

KEY FOUND at POSITION : 2.

*/








/* 27. INPUT A NxN MATRIX, DISPLAY IT IN MATRIX FORM AND DETERMINE IF
IT IS A LOWER TRIANGULAR MATRIX. (LOWER TRIANGULAR MATRIX IS A
SQUARE MATRIX IN WHICH ALL THE ELEMENTS ABOVE THE PRINCIPAL
DIAGONAL ARE ZEROES
*/
#include
#include

#define ROW 25
#define COL 25

int check(int mat[][COL],int n)
{
int i,j;
for(i=0;i {
for(j=i+1;j {
if(mat[i][j]!=0)
return 0;
}
}
return 1;
}

void main()
{
int mat[ROW][COL],i,j,n,flag=1;
clrscr();
printf("Enter the order of matrix [NxN] N = ");
scanf("%d",&n);
printf("\nEnter the elements of matrix\n");
for(i=0;i for(j=0;j scanf("%d",&mat[i][j]);

printf("\nThe matrix is :\n\n");
for(i=0;i {
for(j=0;j printf("%d\t",mat[i][j]);
printf("\n");
}
flag=check(mat,n);
if(flag)
printf("\n\tThis is a Lower triangular matrix");
else
printf("\n\tThis is NOT a Lower triangular matrix");
getch();
}
/*
=====OUT PUT=====

1.Enter the order of matrix [NxN] N = 3

Enter the elements of matrix
2 3 4
4 3 6
7 4 2

The matrix is :

2 3 4
4 3 6
7 4 2

This is NOT a Lower triangular matrix


2.Enter the order of matrix [NxN] N = 3

Enter the elements of matrix
2 0 0
6 8 0
3 4 1

The matrix is :

2 0 0
6 8 0
3 4 1

This is a Lower triangular matrix

*/















/* 28. INPUT A NxN MATRIX, DISPLAY IT IN MATRIX FORM AND DETERMINE IF
IT IS A UPPER TRIANGULAR MATRIX. (UPPER TRIANGULAR MATRIX IS A
SQUARE MATRIX IN WHICH ALL THE ELEMENTS BELOW THE PRINCIPAL
DIAGONAL ARE ZEROES
*/

#include
#include

#define ROW 25
#define COL 25

/* Function to check upper triangular matrix */
int check(int mat[][COL],int n)
{
int i,j;
for(i=0;i {
for(j=0;j {
if(mat[i][j]!=0)
return 0;
}
}
return 1;
}
void main()
{
int mat[ROW][COL],i,j,n,flag=1;
clrscr();
printf("Enter the order of matrix [NxN] N = ");
scanf("%d",&n);
printf("\nEnter the elements of matrix\n");
for(i=0;i for(j=0;j scanf("%d",&mat[i][j]);
printf("\nThe matrix is :\n\n");
for(i=0;i {
for(j=0;j printf("%d\t",mat[i][j]);
printf("\n");
}
flag=check(mat,n);
if(flag)
printf("\n\tThis is a Upper triangular matrix");
else
printf("\n\tThis is NOT a Upper triangular matrix");
getch();
}
/*
=====OUT PUT=====

1.Enter the order of matrix [NxN] N = 2

Enter the elements of matrix
1 0
4 0

The matrix is :

1 0
4 0

This is NOT a Upper triangular matrix

2.Enter the order of matrix [NxN] N = 2

Enter the elements of matrix
1 2
0 2

The matrix is :

1 2
0 2

This is a Upper triangular matrix

*/

/* 29. INPUT A NxN MATRIX, DISPLAY IT IN MATRIX FORM AND DETERMINE
IF IT IS AN IDENTITY MATRIX.
*/

#include
#include

#define ROW 25
#define COL 25

int fun(int mat[][COL],int n)
{
int i,j,flag;
for(i=0;i {
for(j=0;j {
if(i!=j)
{
if(mat[i][j]!=0)
return 0;
}
else
if(mat[i][j]!=1)
return 0;
}
}
return 1;
}

void main()
{
int mat[ROW][COL],i,j,n,flag=1;
clrscr();
printf("Enter the order of matrix [NxN] N = ");
scanf("%d",&n);
printf("\nEnter the elements of matrix\n");
for(i=0;i for(j=0;j scanf("%d",&mat[i][j]);

printf("\nThe matrix is :\n\n");
for(i=0;i {
for(j=0;j printf("%d\t",mat[i][j]);
printf("\n");
}

flag=fun(mat,n);

if(flag)
printf("\n\tIdentity Matrix");
else
printf("\n\tNot an Identity Matrix");
getch();
}












/*
=====OUT PUT=====

1.Enter the order of matrix [NxN] N = 3

Enter the elements of matrix
1 0 1
0 1 0
0 0 1

The matrix is :

1 0 1
0 1 0
0 0 1

Not an Identity Matrix

2.Enter the order of matrix [NxN] N = 3

Enter the elements of matrix
1 0 0
0 1 0
0 0 1

The matrix is :

1 0 0
0 1 0
0 0 1

Identity Matrix
*/

/* 30. INPUT TWO NxN MATRICES A AND B, DISPLAY A,B AND A*B
(CREATE THREE FUNCTIONS TO READ, DISPALY AND CALCULATE A*B)
*/
#include
#include

#define ROW 25
#define COL 25

void read(int mat[][COL],int m,int n)
{
int i,j;
for(i=0;i for(j=0;j scanf("%d",&mat[i][j]);
}

void disp(int mat[][COL],int m,int n)
{
int i,j;
for(i=0;i {
for(j=0;j printf("%d\t",mat[i][j]);
printf("\n");
}
}

void matmul(int A[][COL],int B[][COL],int C[][COL],
int m1,int n1,int n2)
{
int i,j,k,ans;
for(i=0;i {
for(j=0;j {
ans=0;
for(k=0;k {
ans+=A[i][k]*B[k][j];
}
C[i][j]=ans;
}
}
}

void main()
{
int A[ROW][COL],B[ROW][COL],C[ROW][COL];
int i,j,m1,n1,m2,n2;
clrscr();
printf("Enter the order of matrix A [MxN] = ");
scanf("%d %d",&m1,&n1);
printf("Enter the order of matrix B [MxN] = ");
scanf("%d %d",&m2,&n2);
if(n1!=m2)
printf("\nMultiplication is not possible");
else
{
printf("\nEnter the elements of matrix A :\n");
read(A,m1,n1);
printf("\nEnter the elements of matrix B :\n");
read(B,m2,n2);
matmul(A,B,C,m1,n1,n2);
printf("----------------------------------");
printf("\nThe order of A is %d x %d",m1,n1);
printf("\nThe matrix A is :\n\n");
disp(A,m1,n1);
printf("----------------------------------");
printf("\nThe order of B is %d x %d",m2,n2);
printf("\nThe matrix B is :\n\n");
disp(B,m2,n2);
printf("----------------------------------");
printf("\nThe order of A*B is %d x %d",m1,n2);
printf("\nThe matrix A*B is :\n\n");
disp(C,m1,n2);
}
getch();
}

/*
====OUT PUT====

1. Enter the order of matrix A [MxN] = 3 3
Enter the order of matrix B [MxN] = 2 3

Multiplication is not possible

2. Enter the order of matrix A [MxN] = 2 3
Enter the order of matrix B [MxN] = 3 3

Enter the elements of matrix A :
2 3 10 4 8 5

Enter the elements of matrix B :
5 8 2 1 0 9 12 6 4
----------------------------------
The order of A is 2 x 3
The matrix A is :

2 3 10
4 8 5
----------------------------------
The order of B is 3 x 3
The matrix B is :

5 8 2
1 0 9
12 6 4
----------------------------------
The order of A*B is 2 x 3
The matrix A*B is :

133 76 71
88 62 100
*/
http://rajkumardownloads.blogspot.com/

0 comments:

Post a Comment