This is default featured slide 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 3 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 5 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

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/

ADVANCED LAB C-PROGRAMMING





/* 1. CHECK MAXIMUM VALUE THAT CAN BE REPRESENTED IN ALL THE
STORAGE TYPES. [ int(short,long,signed,unsigned), char, double, float]
*/
#include
#include
#include
void main()
{
int size;
clrscr();
size=sizeof(short int);
printf("max value in short int is ------>%lf\n",pow(2,size*8-1)-1);
size=sizeof(long int);
printf("max value in long int is ------->%lf\n",pow(2,size*8-1)-1);
size=sizeof(int);
printf("max value in signed int is ----->%lf\n",pow(2,size*8-1)-1);
size=sizeof(unsigned int);
printf("max value in unsigned int is --->%lf\n",pow(2,size*8));
size=sizeof(char);
printf("max value in char is ----------->%lf\n",pow(2,size*8-1)-1);
size=sizeof(double);
printf("max value in double is --------->%lf\n",pow(2,size*8-1)-1);
size=sizeof(float);
printf("max value in float is ---------->%lf\n",pow(2,size*8-1)-1);
getch();
}
/*
=====OUT PUT=====
max value in short int is ------>32767.000000
max value in long int is ------->2147483647.000000
max value in signed int is ----->32767.000000
max value in unsigned int is --->65536.000000
max value in char is ----------->127.000000
max value in double is --------->9223372036854775810.000000
max value in float is ---------->2147483647.000000
*/
/* 2. WRITE A C PROGRAM TO REVERSE A NUMBER */
#include
#include
#include
#define MAX 25
void main()
{
char arr[MAX],temp;
char *f,*r;
clrscr();
printf("Enter a Number To Reverse : ");
gets(arr);
f=arr;
r=&arr[strlen(arr)-1];
while(f
{
temp= *f;
*f=*r;
*r=temp;
f++;
r--;
}
printf("Reversed string is %s",arr);
getch();
}
/*
=====OUT PUT=====
1. Enter a Number To Reverse : 0987654321
Reversed string is 1234567890
------------------------------------------
2. Enter a Number To Reverse : 123321
Reversed string is 123321
*/
/* 3A. WRITE A C PROGRAM TO GENERATE 'N' PRIME NUMBERS. */
#include
#include
void main()
{
int n,m,flag,i,count=0;
clrscr();
printf("\n Enter The Value for 'N' : ");
scanf("%d",&n);
printf("\n The %d Prime No. are...\n",n);
for(i=2;count
{
flag=prime(i);
if(flag==0)
{
printf(" %d \n",i);
count++;
}
}
getch();
}
/* FUNCTION TO FIND PRIME NO. */
prime(int i)
{
int j,f;
f=0;
for(j=2;j<=i/2;j++)
if(i%j==0)
f=1;
return(f);
}
/*
=====OUT PUT=====
1. Enter The Value for 'N' : 3
The 3 Prime No. are...
2
3
5
-------------------------------
2. Enter The Value for 'N' : 9
The 9 Prime No. are...
2
3
5
7
11
13
17
19
23
*/
/* 3B. INPUT TWO INTEGERS 'N' AND 'M'.FIND THE NO. OF PRIME NUMBERS
B/W 'N' AND 'M'(CREATE A FUNCTION WHICH FINDS IF THE GIVEN NO. IS
PRIME.THIS FUNCTION CAN BE CALLED REPEATEDLY).
*/
#include
#include
main()
{
int n,m,flag,i,count=0;
clrscr();
printf("\n Enter The Value for 'N' : ");
scanf("%d",&n);
printf("\n Enter the Value for 'M'[M>N] : ");
scanf("%d",&m);
if((n1))
{
printf("\n The Prime No. are...\n");
for(i=n;i<=m;i++)
{
flag=prime(i);
if(flag==0)
{
printf("%d \n",i);
count++;
}
}
printf("\n There are \'%d\' PRIME no. B/W %d and %d.",count,n,m);
}
else
printf("\n Invalid Condition... ");
getch();
}
/* FUNCTION TO FIND PRIME NO. */
prime(int i)
{
int j,f;
f=0;
for(j=2;j<=i/2;j++)
if(i%j==0)
f=1;
return(f);
}
/*
=====OUT PUT=====
1. Enter The Value for 'N' : 9
Enter the Value for 'M'[M>N] : 2
Invalid Condition...
2. Enter The Value for 'N' : 2
Enter the Value for 'M'[M>N] : 25
The Prime No. are...
2
3
5
7
11
13
17
19
23
There are '9' PRIME no. B/W 2 and 25.
*/
/* 4A. WRITE A C PROGRAM TO GENERATE N FIBONACCI NUMBERS */
#include
#include
void main()
{
int a=0,b=1,c,n,i;
clrscr();
printf("Enter the value of N : ");
scanf("%d",&n);
printf("\n%d\n%d",a,b);
for(c=a+b,i=3;i<=n;c=a+b,a=b,b=c,i++)
printf("\n%d",c);
getch();
}
/*
=====OUTPUT=====
Enter the value of N : 10
The 10 Fibonacci Numbers Are...
0
1
1
1
2
3
5
8
13
21
*/
/* 4B. WRITE A C PROGRAM TO GENERATE FIBONACCI SERIES UPTO A GIVEN

NUMBER */

#include
#include
void main()
{
int a=0,b=1,c,n;
clrscr();
printf("Enter the limit to Generate Fibonacci No.s : ");
scanf("%d",&n);
printf("\n%d\n%d",a,b);
for(c=a+b;c<=n;c=a+b,a=b,b=c)
printf("\n%d",c);
getch();
}
/*
=====OUT PUT=====
Enter the limit to Generate Fibonacci No.s : 25
0
1
1
1
2
3
5
8
13
21
*/
/* 4C. WRITE A C PROGRAM TO GENERATE 3 DIGITS FIBONACCI SERIES */
#include
#include
void main()
{
int a=0,b=1,c;
clrscr();
printf("THREE DIGITS FIBONACCI SERIES :\n");
for(c=a+b;c<100 data-blogger-escaped-c="a+b,a=b,b=c);</span">
for(;c<1000 data-blogger-escaped-c="a+b,a=b,b=c)</span">
printf("\n%d",c);
getch();
}
/*
=====OUT PUT=====
THREE DIGITS FIBONACCI SERIES :
144
233
377
610
987
*/
/* PR5.WRITE A C PROGRAM TO CONVERT THE FOLLOWING :
a. DECIMAL TO BINARY NUMBER
b. BINARY TO DECIMAL NUMBER
c. REAL DECIMAL NUMBER TO HEXADECIMAL NUMBER
d. REAL DECIMAL TO OCTAL NUMBER
*/
#include
#include
#include /* isdigit */
#include /* atoi */
#include
#define MAX 25
void dectobin()
{
int bin,dec,n,r,k;
printf("Enter a Decimal number \t");
scanf("%d",&dec);
n=dec; bin=0;k=1;
while(n>0)
{
r=n%2;
n=n/2;
bin=bin+r*k;
k=k*10;
}
printf("\n Decimal = %d \n Binary = %d",dec,bin);
}
void bintodec()
{
int bin,dec,n,r,k;
printf("Enter a Binary number \t");
scanf("%d",&bin);
n=bin; dec=0;k=1;
while(n>0)
{
r=n%10;
if(r>1)
{
printf("\t ERROR!! Not a Binary number");
return;
}
n=n/10;
dec=dec+r*k;
k=k*2;
}
printf("\n Binary = %d \n Decimal = %d",bin,dec);
}
void rdectohex()
{
char str[MAX],str1[MAX],str2[MAX],a;
int i,j,n,r,len,temp,sign=1;
printf("Enter a Real Decimal Number\t");
scanf("%s",str);
for(j=0,i=0 ; str[i]!='.' && str[i]!='\0' ; i++)
{
if(str[i]=='-')
sign=-1;
else if(str[i]=='+')
sign=1;
else if(str[i]>='0' && str[i]<='9')
str1[j++]=str[i];
else
{
printf("ERROR!! Not a Real Decimal Number");
return;
}
}
str1[j]='\0';
i++;
for(j=0 ; str[i]!='\0' ; i++)
{
if(str[i]>='0' && str[i]<='9')
str2[j++]=str[i];
else
{
printf("ERROR!! Not a Real Decimal Number");
return;
}
}
str2[j]='\0';
/* before decimal */
i=0;
n=atoi(str1);
while(n>0)
{
r=n%16;
if(r>9)
{
switch(r)
{
case 10:a='A';break;
case 11:a='B';break;
case 12:a='C';break;
case 13:a='D';break;
case 14:a='E';break;
case 15:a='F';break;
}
}
else
a=(char)r+'0';
n=n/16;
str1[i++]=a;
}
str1[i]='\0';
/* after decimal */
i=0;
j=0;
n=atoi(str2);
len=strlen(str2);
while(j<5 data-blogger-escaped-span="span">
{
temp=pow(10,len);
r=(n*16) / temp; /* r=quotient */
n=(n*16) % temp;
if(r>9)
{
switch(r)
{
case 10:a='A';break;
case 11:a='B';break;
case 12:a='C';break;
case 13:a='D';break;
case 14:a='E';break;
case 15:a='F';break;
}
}
else
a=(char)r+'0';
str2[i++]=a;
j++;
}
str2[i]='\0';
/* To Display */
strrev(str1);
if(sign==-1)
printf("\nHexadecimal : -%s.%s",str1,str2);
else
printf("\nHexadecimal : %s.%s",str1,str2);
}
void rdectooct()
{
char str[MAX],str1[MAX],str2[MAX];
int i,j,n,r,k,temp,len,sign=1,oct1,oct2;
printf("Enter a Real Decimal Number\t");
scanf("%s",str);
for(j=0,i=0 ; str[i]!='.' && str[i]!='\0' ; i++)
{
if(str[i]=='-')
sign=-1;
else if(str[i]=='+')
sign=1;
else if(str[i]>='0' && str[i]<='9')
str1[j++]=str[i];
else
{
printf("ERROR!! Not a Real Decimal Number");
return;
}
}
str1[j]='\0';
i++;
for(j=0 ; str[i]!='\0' ; i++)
{
if(str[i]>='0' && str[i]<='9')
str2[j++]=str[i];
else
{
printf("ERROR!! Not a Real Decimal Number");
return;
}
}
str2[j]='\0';
/* before decimal */
i=0;
n=atoi(str1);
oct1=0;k=1;
while(n>0)
{
r=n%8;
if(r>7)
{
printf("\t ERROR!! Not a Octal number");
return;
}
n=n/8;
oct1=oct1+r*k;
k=k*10;
}
/* After decimal */
oct2=0;
j=0;
n=atoi(str2);
len=strlen(str2);
temp=pow(10,len);
while(j<4 data-blogger-escaped-span="span">
{
r=(n*8)/temp; /* r is Quotient and generated
carry is indicated by it*/
n=(n*8)%temp;
oct2=(oct2*10)+r;
j++;
}
/* To Display */
if(sign==-1)
printf("\nOctal : -%d.%d",oct1,oct2);
else
printf("\nOctal : %d.%d",oct1,oct2);
}
void main()
{
int flag=1,choice=0;
while(flag!=0)
{
clrscr();
printf("1. Decimal to Binary\n");
printf("2. Binary to Decimal\n");
printf("3. Real Decimal to Hexadecimal\n");
printf("4. Real Decimal to Octal\n");
printf("5. Exit\n");
printf("Enter choice -->\t");
scanf("%d",&choice);
switch(choice)
{
case 1: dectobin();flag=1;getch();break;
case 2: bintodec();flag=1;getch();break;
case 3: rdectohex();flag=1;getch();break;
case 4: rdectooct();flag=1;getch();break;
default: flag=0;break;
}
choice=0;
}
}
/*
=====OUT PUT=====
1. Decimal to Binary
2. Binary to Decimal
3. Real Decimal to Hexadecimal
4. Real Decimal to Octal
5. Exit
Enter choice --> 1
Enter a Decimal number 10
Decimal = 10
Binary = 1010
1. Decimal to Binary
2. Binary to Decimal
3. Real Decimal to Hexadecimal
4. Real Decimal to Octal
5. Exit
Enter choice --> 2
Enter a Binary number 1010
Binary = 1010
Decimal = 10
1. Decimal to Binary
2. Binary to Decimal
3. Real Decimal to Hexadecimal
4. Real Decimal to Octal
5. Exit
Enter choice --> 3
Enter a Real Decimal Number 12.87
Hexadecimal : C.DEB85
1. Decimal to Binary
2. Binary to Decimal
3. Real Decimal to Hexadecimal
4. Real Decimal to Octal
5. Exit
Enter choice --> 4
Enter a Real Decimal Number 25.895
Octal : 31.7121
*/
/* 6A. WRITE A C PROGRAM TO SUM THE SERIES:
x^3 x^5 x^7
SIN(x)= x - ----- + ----- + ----- + ...
3! 5! 7!
*/
#include
#include
#include
#define PI 3.143
long int fact(int n) /* To find factorial */
{
if(n==0)
return 1;
else
return(n*fact(n-1));
}
void main()
{
int i,n,term=1;
float x,y,sum;
clrscr();
printf("\n SIN Series...");
printf("\n Enter No. of Terms N [1-12] : ");
scanf("%ld",&n);
if(n>=1 && n<=12)
{
printf("\n Enter the Value of 'X' : ");
scanf("%f",&y);
x=y*(PI/180.00); /* Converting degrees to radians */
sum=x;
for(i=3;term<=n;i+=2)
{
if(i==3)
sum-=pow(x,i)/(float)fact(i);
else
sum+=pow(x,i)/(float)fact(i);
term++;
}
printf("\n Sin(%f) = %f",y,sum);
}
else
printf("Wrong entry!!!");
getch();
}
/* =====OUT PUT=====
SIN Series...
1. Enter No. of Terms N [1-12] : 9
Enter the Value of 'X' : 45
Sin(45.000000) = 0.707429
2. SIN Series...
Enter No. of Terms N [1-12] : 5
Enter the Value of 'X' : 0
Sin(0.000000) = 0.000000
3. SIN Series...
Enter No. of Terms N [1-12] : 15
Wrong entry!!!
*/
/* 6B. WRITE A C PROGRAM TO SUM THE SERIES:
x^2 x^4 x^6
COS(x)= 1 - ----- + ----- - ----- + ...
2! 4! 6!
*/
#include
#include
#include
#define PI 3.143
long int fact(int n) /* To find factorial */
{
if(n==0)
return 1;
else
return(n*fact(n-1));
}
void main()
{
int i,n,term=1;
float x,y,sum1,sum2;
clrscr();
printf("\n COS Series...");
printf("\n Enter No. of Terms N [1-12] : ");
scanf("%ld",&n);
if(n>=1 && n<=12)
{
printf("\n Enter the Value of 'X' : ");
scanf("%f",&y);
x=y*(PI/180.00); /* Converting degrees to radians */
sum1=1;
sum2=0;
for(i=2;term<=n;i+=2)
{
if(i%4==0)/* To add positive terms */
sum1+=pow(x,i)/(float)fact(i);
else /* To add Negative terms */
sum2+=pow(x,i)/(float)fact(i);
term++;
}
printf("\n Cos(%.2f) = %f",y,sum1-sum2);
}
else
printf("Wrong entry!!!");
getch();
}
/* =====OUT PUT=====
1. COS Series...
Enter No. of Terms N [1-12] : 9
Enter the Value of 'X' : 90
Cos(90.00) = -0.000700
2. COS Series...
Enter No. of Terms N [1-12] : 11
Enter the Value of 'X' : 0
Cos(0.00) = 1.000000
3. COS Series...
Enter No. of Terms N [1-12] : 0
Wrong entry!!!
*/
/* 6C. WRITE A C PROGRAM TO SUM THE SERIES:
x x^2 x^3
e^x = 1 + ----- + ----- + ----- + ...
1! 2! 3! */
#include
#include
#include
#define PI 3.143
long int fact(int n) /* To find factorial */
{
if(n==0)
return 1;
else
return(n*fact(n-1));
}
void main()
{
int i,n,term=1;
float x,y,sum;
clrscr();
printf("\n Enter No. of Terms N [1-12] : ");
scanf("%ld",&n);
if(n>=1 && n<=12)
{
printf("\n Enter the Value of 'X' in degrees : ");
scanf("%f",&y);
x=y*(PI/180.00); /* Converting degrees to radians */
sum=1;
for(i=1;term<=n;i++)
{
sum+=pow(x,i)/(float)fact(i);
term++;
}
printf("\n e^%.2f = %f",y,sum);
}
else
printf("Wrong entry!!!");
getch();
}
/* =====OUT PUT=====
1.Enter No. of Terms N [1-12] : 8
Enter the Value of 'X' in degrees : 60
e^60.00 = 2.850986
2.Enter No. of Terms N [1-12] : 12
Enter the Value of 'X' in degrees : 90
e^90.00 = 4.813863
3.Enter No. of Terms N [1-12] : 9
Enter the Value of 'X' in degrees : 0
e^0.00 = 1.000000
*/
/* 7. THREE POSITIVE INTEGERS a,b,c FORM A TRIPLET OF a^2 + b^2 = c^2.

WRITE A C PROGRAM TO FIND ALL PYTHOGOREAN TRIPLETS a,b,c WHERE

a,b <>
*/
#include
#include
#include
void main()
{
int a,b,c2,c;
clrscr();
printf("Pythogorean Triplets\n\n");
for(a=1;a<120 data-blogger-escaped-a="a" data-blogger-escaped-span="span">
{
for(b=a;b<120 data-blogger-escaped-b="b" data-blogger-escaped-span="span">
{
c2 = a*a + b*b;
c=(int)sqrt(c2);
if(c*c == c2)
{
printf("%d %d %d,\t",a,b,c);
}
}
}
getch();
}
/*
=====OUT PUT=====
Pythogorean Triplets
3 4 5, 5 12 13, 6 8 10, 7 24 25, 8 15 17,
9 12 15, 9 40 41, 10 24 26, 11 60 61, 12 16 20,
12 35 37, 13 84 85, 14 48 50, 15 20 25, 15 36 39,
15 112 113, 16 30 34, 16 63 65, 18 24 30, 18 80 82,
20 21 29, 20 48 52, 20 99 101, 21 28 35, 21 72 75,
24 32 40, 24 45 51, 24 70 74, 25 60 65, 27 36 45,
28 45 53, 28 96 100, 30 40 50, 30 72 78, 32 60 68,
33 44 55, 33 56 65, 35 84 91, 36 48 60, 36 77 85,
36 105 111, 39 52 65, 39 80 89, 40 42 58, 40 75 85,
40 96 104, 42 56 70, 44 117 125, 45 60 75, 45 108 117,
48 55 73, 48 64 80, 48 90 102, 51 68 85, 54 72 90,
56 90 106, 56 105 119, 57 76 95, 60 63 87, 60 80 100,
60 91 109, 63 84 105, 65 72 97, 66 88 110, 66 112 130,
69 92 115, 72 96 120, 75 100 125, 78 104 130, 80 84 116,
81 108 135, 84 112 140, 87 116 145, 88 105 137, 96 110 146,
100 105 145,
*/
/* 8. INPUT AN INTEGER N, FIND OUT THE POSITIONS OF OCCURRENCE OF
DIGITS 0,1,2,3,4,...9 IN THIS NUMBER. (CREATE A FUNCTION WHICH FINDS
THE POSITIONS OF OCCURRENCE OF A PARTICULAR DIGIT. THIS FUNCTION
CAN BE CALLED REPEATEDLY).
*/
#include
#include
#define MAX 25
void main()
{
char arr[MAX],posof;
int i,present;
clrscr();
printf("Enter an integer");
scanf("%s",arr);
for(i=0;i
{
if( !(arr[i]>='0' && arr[i]<='9') )
printf("The number is not an integer");
}
for(posof='0';posof<='9';posof++)
{
printf("The position of %c -->",posof);
for(i=0,present=0;i
{
if(arr[i]==posof)
{
printf("%d ",i+1);
present++;
}
}
if(present==0)
printf("Not present");
printf("\n");
}
getch();
}
/*
=====OUT PUT=====
1.Enter an integer : 234876521
The position of 0 -->Not present
The position of 1 -->9
The position of 2 -->1 8
The position of 3 -->2
The position of 4 -->3
The position of 5 -->7
The position of 6 -->6
The position of 7 -->5
The position of 8 -->4
The position of 9 -->Not present
2.Enter an integer : xyz
The number is not an integer
The position of 0 -->Not present
The position of 1 -->Not present
The position of 2 -->Not present
The position of 3 -->Not present
The position of 4 -->Not present
The position of 5 -->Not present
The position of 6 -->Not present
The position of 7 -->Not present
The position of 8 -->Not present
The position of 9 -->Not present
*/
/* 9a.WRITE A CPROGRAM TO GENERATE THE FOLLOWING OUT PUT:
a) 1
2 3
4 5 6
7 8 9 10
*/
#include
#include
#define MAXLINE 4
void main()
{
int linenum=1,count=1,i=1;
clrscr();
while(linenum <= MAXLINE)
{
for(count=1 ; count<=linenum ; i++)
{
printf("%d\t",i);
count++;
}
printf("\n");
linenum++;
}
getch();
}
/*
=====OUT PUT=====
1
2 3
4 5 6
7 8 9 10
*/
/* 9b.WRITE A CPROGRAM TO GENERATE THE FOLLOWING OUT PUT:
b) 1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
*/
#include
#include
#define MAXLINE 5
void main()
{
int curlin,i;
clrscr();
curlin=1;
while(curlin <= MAXLINE)
{
for(i=0 ; i < (MAXLINE - curlin) ; i++)
printf(" "); /* 3 spaces */
for(i=1;i<=curlin;i++)
{
if(i<=9)
printf("%d ",i); /* 1 digit followed by 2 spaces */
else
printf("%d ",i); /* 2 digits followed by 1 space */
}
i-=2;
for( ; i>=1 ; i--)
{
if(i<=9)
printf("%d ",i); /* 1 digit followed by 2 spaces */
else
printf("%d ",i); /* 2 digits followed by 1 space */
}
printf("\n");
curlin++;
}
getch();
}
/*
=====OUT PUT=====
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
*/
/* 10. IN A GIVEN STRING, FIND THE FREQUENCY OF OCCURRENCE OF

A PARTICULAR LIST

*/
#include
#include
#include
#define MAX 30
void main()
{
char str[MAX],str1[MAX];
int i,j,len1,len2,flag,freq;
clrscr();
printf(" \n Enter a String : ");
gets(str);
printf("\n Enter a List : ");
gets(str1);
len1=strlen(str);
len2=strlen(str1);
freq=0;
for(i=0;i
{
flag=0;
for(j=0;j
{
if(str[i]==str1[j])
{
flag++;
i++,j++;
}
else
{
i++;
break;
}
}
if(flag==len2)
freq++;
}
printf("\n The Frequency of occurence of \n\t\"%s\" in \"%s\" is %d",str1,str,freq);
getch();
}
/*
=====OUT PUT=====
1. Enter a String : KAUNDINYA
Enter a List : KAUN
The Frequency of occurence of
"KAUN" in "KAUNDINYA" is 1
2. Enter a String : bharath
Enter a List : kaun
The Frequency of occurence of
"kaun" in "bharath" is 0
*/