#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<string.h>
int main ()
{char str1[100];
char str2[100];
printf("entr a srting \n");
gets(str1);
strcpy(str2,str1);
strrev(str2);
if(strcmp(str1,str2)==0)
printf("it is a palindrome");
else
printf("not a palindrome");
getch();
return 0;
}
Monday, October 24, 2011
Sunday, October 23, 2011
C program code to convert decimal to hexadecimal
#include<stdio.h>
#include<conio.h>
#include<math.h>
void dtoh(int d);
main()
{
int d;
printf("Enter a no. in decimal system:- ");
scanf("%d",&d);
dtoh(d);
getch();
}
void dtoh(int d)
{
int b,c=0,a[5],i=0;
b=d;
while (b>15)
{
a[i]=b%16;
b=b/16;
i++;
c++;
}
a[i]=b;
printf("Its hexadecimal equivalent is ");
for (i=c;i>=0;--i)
{
if (a[i]==10)
printf("A");
else if (a[i]==11)
printf("B");
else if (a[i]==12)
printf("C");
else if (a[i]==13)
printf("D");
else if (a[i]==14)
printf("E");
else if (a[i]==15)
printf("F");
else
printf("%d",a[i]);
}
return o;
}
#include<conio.h>
#include<math.h>
void dtoh(int d);
main()
{
int d;
printf("Enter a no. in decimal system:- ");
scanf("%d",&d);
dtoh(d);
getch();
}
void dtoh(int d)
{
int b,c=0,a[5],i=0;
b=d;
while (b>15)
{
a[i]=b%16;
b=b/16;
i++;
c++;
}
a[i]=b;
printf("Its hexadecimal equivalent is ");
for (i=c;i>=0;--i)
{
if (a[i]==10)
printf("A");
else if (a[i]==11)
printf("B");
else if (a[i]==12)
printf("C");
else if (a[i]==13)
printf("D");
else if (a[i]==14)
printf("E");
else if (a[i]==15)
printf("F");
else
printf("%d",a[i]);
}
return o;
}
Saturday, October 22, 2011
TO COUNT NUMBER OF WORDS IN A SENTENCE
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<string.h>
int main ()
{char str1[100];
int i,x,c;
gets(str1);
c=0;
for(i=0;str1[i]!='\0';++i)
{
if(str1[i]==' ')
++c;
}
x=c+1;
printf("no of word is %d",x);
getch();
return 0;
}
#include<conio.h>
#include<math.h>
#include<string.h>
int main ()
{char str1[100];
int i,x,c;
gets(str1);
c=0;
for(i=0;str1[i]!='\0';++i)
{
if(str1[i]==' ')
++c;
}
x=c+1;
printf("no of word is %d",x);
getch();
return 0;
}
TO FIND THE DETERMINANT OF THE MATRIX
#include<stdio.h>
#include<math.h>
float detrm(float[][],float);
void cofact(float[][],float);
void trans(float[][],float[][],float);
main()
{
float a[25][25],k,d;
int i,j;
printf("ENTER THE ORDER OF THE MATRIX:\n");
scanf("%f",&k);
printf("ENTER THE ELEMENTS OF THE MATRIX:\n");
for(i=0;i<k;i++)
{
for(j=0;j<k;j++)
{
scanf("%f",&a[i][j]);
}
}
d=detrm(a,k);
printf("THE DETERMINANT IS=%f",d);
if(d==0)
printf("\nMATRIX IS NOT INVERSIBLE\n");
else
cofact(a,k);
}
/******************FUNCTION TO FIND THE DETERMINANT OF THE MATRIX************************/
float detrm(float a[25][25],float k)
{
float s=1,det=0,b[25][25];
int i,j,m,n,c;
if(k==1)
{
return(a[0][0]);
}
else
{
det=0;
for(c=0;c<k;c++)
{
m=0;
n=0;
for(i=0;i<k;i++)
{
for(j=0;j<k;j++)
{
b[i][j]=0;
if(i!=0&&j!=c)
{
b[m][n]=a[i][j];
if(n<(k-2))
n++;
else
{
n=0;
m++;
}
}
}
}
det=det+s*(a[0][c]*detrm(b,k-1));
s=-1*s;
}
}
return(det);
}
/*******************FUNCTION TO FIND COFACTOR*********************************/
void cofact(float num[25][25],float f)
{
float b[25][25],fac[25][25];
int p,q,m,n,i,j;
for(q=0;q<f;q++)
{
for(p=0;p<f;p++)
{
m=0;
n=0;
for(i=0;i<f;i++)
{
for(j=0;j<f;j++)
{
b[i][j]=0;
if(i!=q&&j!=p)
{
b[m][n]=num[i][j];
if(n<(f-2))
n++;
else
{
n=0;
m++;
}
}
}
}
fac[q][p]=pow(-1,q+p)*detrm(b,f-1);
}
}
trans(num,fac,f);
}
/*************FUNCTION TO FIND TRANSPOSE AND INVERSE OF A MATRIX**************************/
void trans(float num[25][25],float fac[25][25],float r)
{
int i,j;
float b[25][25],inv[25][25],d;
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
b[i][j]=fac[j][i];
}
}
d=detrm(num,r);
inv[i][j]=0;
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
inv[i][j]=b[i][j]/d;
}
}
printf("\nTHE INVERSE OF THE MATRIX:\n");
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
printf("\t%f",inv[i][j]);
}
printf("\n");
}
}
#include<math.h>
float detrm(float[][],float);
void cofact(float[][],float);
void trans(float[][],float[][],float);
main()
{
float a[25][25],k,d;
int i,j;
printf("ENTER THE ORDER OF THE MATRIX:\n");
scanf("%f",&k);
printf("ENTER THE ELEMENTS OF THE MATRIX:\n");
for(i=0;i<k;i++)
{
for(j=0;j<k;j++)
{
scanf("%f",&a[i][j]);
}
}
d=detrm(a,k);
printf("THE DETERMINANT IS=%f",d);
if(d==0)
printf("\nMATRIX IS NOT INVERSIBLE\n");
else
cofact(a,k);
}
/******************FUNCTION TO FIND THE DETERMINANT OF THE MATRIX************************/
float detrm(float a[25][25],float k)
{
float s=1,det=0,b[25][25];
int i,j,m,n,c;
if(k==1)
{
return(a[0][0]);
}
else
{
det=0;
for(c=0;c<k;c++)
{
m=0;
n=0;
for(i=0;i<k;i++)
{
for(j=0;j<k;j++)
{
b[i][j]=0;
if(i!=0&&j!=c)
{
b[m][n]=a[i][j];
if(n<(k-2))
n++;
else
{
n=0;
m++;
}
}
}
}
det=det+s*(a[0][c]*detrm(b,k-1));
s=-1*s;
}
}
return(det);
}
/*******************FUNCTION TO FIND COFACTOR*********************************/
void cofact(float num[25][25],float f)
{
float b[25][25],fac[25][25];
int p,q,m,n,i,j;
for(q=0;q<f;q++)
{
for(p=0;p<f;p++)
{
m=0;
n=0;
for(i=0;i<f;i++)
{
for(j=0;j<f;j++)
{
b[i][j]=0;
if(i!=q&&j!=p)
{
b[m][n]=num[i][j];
if(n<(f-2))
n++;
else
{
n=0;
m++;
}
}
}
}
fac[q][p]=pow(-1,q+p)*detrm(b,f-1);
}
}
trans(num,fac,f);
}
/*************FUNCTION TO FIND TRANSPOSE AND INVERSE OF A MATRIX**************************/
void trans(float num[25][25],float fac[25][25],float r)
{
int i,j;
float b[25][25],inv[25][25],d;
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
b[i][j]=fac[j][i];
}
}
d=detrm(num,r);
inv[i][j]=0;
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
inv[i][j]=b[i][j]/d;
}
}
printf("\nTHE INVERSE OF THE MATRIX:\n");
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
printf("\t%f",inv[i][j]);
}
printf("\n");
}
}
solution of balagurusamy of chapter 8.15
#include<stdio.h>
int main()
{
int i,j;
char str[]="123456789",*p;
p=str;
for(i=0;i<5;i++)
{
printf("%5.*s",i+1,p+i);
for(j=i-1;j>=0;j--)
printf("%c",*(p+i+j));
printf("\n");
}
return 0;
}
int main()
{
int i,j;
char str[]="123456789",*p;
p=str;
for(i=0;i<5;i++)
{
printf("%5.*s",i+1,p+i);
for(j=i-1;j>=0;j--)
printf("%c",*(p+i+j));
printf("\n");
}
return 0;
}
solution of balagurusamy of chapter 8.14
#include<stdio.h>
#include<string.h>
#define MAX 50
int s;
char names[MAX+1][20];
void find_roll (char str[]);
int main()
{
int roll,n;
char str[20];
printf("how many students?:");
scanf("%d",&s);
printf("enter rollwise student name:\n");
for(roll=1;roll<=s;roll++)
{
printf("%d.",roll);
scanf("%s",&names[roll]);
}
printf("search options:\n");
printf("1.enter roll\n");
printf("2.enter name\nchoice:");
scanf("%d",&n);
if(n==1)
{
printf("enter roll:");
scanf("%d",&roll);
printf("name: %s",names[roll]);
}
else
{
printf("enter name:");
scanf("%s",str);
find_roll(str);
}
return 0;
}
void find_roll(char str[20])
{
int i;
for(i=1;i<=s;i++)
if(strcmp(names[i],str)==0)
{
printf("roll no: %d",i);
break;
}
return;
}
#include<string.h>
#define MAX 50
int s;
char names[MAX+1][20];
void find_roll (char str[]);
int main()
{
int roll,n;
char str[20];
printf("how many students?:");
scanf("%d",&s);
printf("enter rollwise student name:\n");
for(roll=1;roll<=s;roll++)
{
printf("%d.",roll);
scanf("%s",&names[roll]);
}
printf("search options:\n");
printf("1.enter roll\n");
printf("2.enter name\nchoice:");
scanf("%d",&n);
if(n==1)
{
printf("enter roll:");
scanf("%d",&roll);
printf("name: %s",names[roll]);
}
else
{
printf("enter name:");
scanf("%s",str);
find_roll(str);
}
return 0;
}
void find_roll(char str[20])
{
int i;
for(i=1;i<=s;i++)
if(strcmp(names[i],str)==0)
{
printf("roll no: %d",i);
break;
}
return;
}
solution of balagurusamy of chapter 8.11
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char s1[40],s2[40];
printf("enter string 1:\n");
scanf("%s",s1);
printf("enter string 2:\n");
scanf("%s",s2);
if(strcmp(s1,s2)==0)
printf("They are equal.");
else if(strcmp(s1,s2)<0)
printf("s1 is less than s2");
else
printf("s1 is greater than s2");
getch();
return 0;
}
#include<conio.h>
#include<string.h>
int main()
{
char s1[40],s2[40];
printf("enter string 1:\n");
scanf("%s",s1);
printf("enter string 2:\n");
scanf("%s",s2);
if(strcmp(s1,s2)==0)
printf("They are equal.");
else if(strcmp(s1,s2)<0)
printf("s1 is less than s2");
else
printf("s1 is greater than s2");
getch();
return 0;
}
solution of balagurusamy of chapter 8.9
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
void num_to_words(long int, char[]);
char one[20][10]={" ","ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE",
"TEN","ELEVEN","TWELVE","THIRTEEN","FORTEEN","FIFTEEN","SIXTEEN",
"SEVENTEEN","EIGHTEEN","NINETEEN"};
char ten[10][8]={" "," ","TWENTY","THIRTY","FORTY","FIFTY","SIXTY","SEVENTY","EIGHTY","NINETY"};
int main()
{
int a,b=0,i,m;
char n[8],*p;
printf("Please enter the amount of money:");
scanf("%s", n);
a=atoi(n);
p=strchr(n,'.');
if(p!=NULL)
b=atoi(p+1);
printf("\nIn words:\n");
for(i=1;i<=2;i++)
{
m=(i<2)?a:b;
num_to_words(((m/1000)%100),"THOUSAND");
num_to_words(((m/100)%10),"HUNDRED");
num_to_words((m%100),"\0");
if(i%2&&b)
printf("AND PAISE");
}
return 0;
}
void num_to_words(long int n,char ch[10])
{
if(n>19)
printf(" %s %s",ten[n/10],one[n%10]);
else if(n)
printf(" %s",one[n]);
if(n)
printf(" %s",ch);
}
#include<stdlib.h>
#include<string.h>
#include<conio.h>
void num_to_words(long int, char[]);
char one[20][10]={" ","ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE",
"TEN","ELEVEN","TWELVE","THIRTEEN","FORTEEN","FIFTEEN","SIXTEEN",
"SEVENTEEN","EIGHTEEN","NINETEEN"};
char ten[10][8]={" "," ","TWENTY","THIRTY","FORTY","FIFTY","SIXTY","SEVENTY","EIGHTY","NINETY"};
int main()
{
int a,b=0,i,m;
char n[8],*p;
printf("Please enter the amount of money:");
scanf("%s", n);
a=atoi(n);
p=strchr(n,'.');
if(p!=NULL)
b=atoi(p+1);
printf("\nIn words:\n");
for(i=1;i<=2;i++)
{
m=(i<2)?a:b;
num_to_words(((m/1000)%100),"THOUSAND");
num_to_words(((m/100)%10),"HUNDRED");
num_to_words((m%100),"\0");
if(i%2&&b)
printf("AND PAISE");
}
return 0;
}
void num_to_words(long int n,char ch[10])
{
if(n>19)
printf(" %s %s",ten[n/10],one[n%10]);
else if(n)
printf(" %s",one[n]);
if(n)
printf(" %s",ch);
}
solution of balagurusamy of chapter 8.6
#include<stdio.h>
#include<string.h>
int main()
{
int i=0;
char s[300],w1[10],w2[10],s2[400],*p,*p1;
printf("enter a string:\n");
scanf("%[^\n]s",s);
printf("enter the word to replace:\n");
scanf("%s",w1);
printf("enter the new word:\n");
scanf("%s",w2);
p=strstr(s,w1);
p1=s;
s2[0]='\0';
while(p!=NULL)
{
*p='\0';
strcat(s2,p1);
strcat(s2,w2);
p1=p+strlen(w1);
p++;
p=strstr(p,w1);
}
strcat(s2,p1);
printf("new string:\n%s",s2);
return 0;
}
#include<string.h>
int main()
{
int i=0;
char s[300],w1[10],w2[10],s2[400],*p,*p1;
printf("enter a string:\n");
scanf("%[^\n]s",s);
printf("enter the word to replace:\n");
scanf("%s",w1);
printf("enter the new word:\n");
scanf("%s",w2);
p=strstr(s,w1);
p1=s;
s2[0]='\0';
while(p!=NULL)
{
*p='\0';
strcat(s2,p1);
strcat(s2,w2);
p1=p+strlen(w1);
p++;
p=strstr(p,w1);
}
strcat(s2,p1);
printf("new string:\n%s",s2);
return 0;
}
solution of balagurusamy of chapter 8.5
#include<stdio.h>
#include<string.h>
int main()
{
int i,j,n;
char s[20],temp;
scanf("%s",s);
n=strlen(s)-1;
/*---BUBBLE SORT---*/
for(i=0; i<n; i++)
for(j=0;j<n-i;j++)
if(s[j]>=s[j+1])
{
temp = s[j];
s[j] = s[j+1];
s[j+1] = temp;
}
printf("%s",s);
return 0;
}
#include<string.h>
int main()
{
int i,j,n;
char s[20],temp;
scanf("%s",s);
n=strlen(s)-1;
/*---BUBBLE SORT---*/
for(i=0; i<n; i++)
for(j=0;j<n-i;j++)
if(s[j]>=s[j+1])
{
temp = s[j];
s[j] = s[j+1];
s[j+1] = temp;
}
printf("%s",s);
return 0;
}
solution of balagurusamy of chapter 8.4
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
int i,a=0;
char *p1,*p2,c[10],ch[30];
printf("Enter the string:\n");
p1=gets(ch);
printf("Enter the word:\n");
p2=gets(c);
i=0;
while(ch[i]!='\0')
{
if(p2==p1)
{ a+=1; }
++i;
}
printf("No. of occurance= %d",a);
getch();
}
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
int i,a=0;
char *p1,*p2,c[10],ch[30];
printf("Enter the string:\n");
p1=gets(ch);
printf("Enter the word:\n");
p2=gets(c);
i=0;
while(ch[i]!='\0')
{
if(p2==p1)
{ a+=1; }
++i;
}
printf("No. of occurance= %d",a);
getch();
}
solution of balagurusamy of chapter 8.3
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char a[30], b[30];
int i,j,k,l=0;
gets(a);
printf("Enter the range:\n");
scanf("%d %d",&j,&k);
for(i=j-1;i<=k-1;++i)
{
b[l]=a[i];
++l;
}
b[l]='\0';
printf("Output:\n");
puts(b);
getch();
}
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char a[30], b[30];
int i,j,k,l=0;
gets(a);
printf("Enter the range:\n");
scanf("%d %d",&j,&k);
for(i=j-1;i<=k-1;++i)
{
b[l]=a[i];
++l;
}
b[l]='\0';
printf("Output:\n");
puts(b);
getch();
}
solution of balagurusamy of chapter 8.2
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{ //Dennis Ritchie
clrscr();
int i;
char in[20];
printf("Who is the inventor of C ?\n");
i=0;
while(i<=2)
{
gets(in);
if(strcmp("Dennis Ritchie",in)==0)
{
printf("Good");
break;
}
else
{
printf("\nTry again");
++i;
}
}
if(i==3)
printf("\nDennis Ritchie");
getch();
}
#include<conio.h>
#include<string.h>
void main()
{ //Dennis Ritchie
clrscr();
int i;
char in[20];
printf("Who is the inventor of C ?\n");
i=0;
while(i<=2)
{
gets(in);
if(strcmp("Dennis Ritchie",in)==0)
{
printf("Good");
break;
}
else
{
printf("\nTry again");
++i;
}
}
if(i==3)
printf("\nDennis Ritchie");
getch();
}
solution of balagurusamy of chapter 8.1
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
int i;
char nm[30];
printf("Enter the string:\n");
gets(nm);
for(i=0;nm[i]!='\0';++i)
{
printf("%c= %d\n",nm[i],nm[i]);
}
getch(); }
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
int i;
char nm[30];
printf("Enter the string:\n");
gets(nm);
for(i=0;nm[i]!='\0';++i)
{
printf("%c= %d\n",nm[i],nm[i]);
}
getch(); }
solution of balagurusamy of chapter 7.15
#include<stdio.h>
#include<conio.h>
main()
{
int a[20][20],b[20][20],c[20][20],d[20][20],i,j,n;
printf("Enter the value of n= ");
scanf("%d",&n);
printf("Matrix A:\n");
for(i=1;i<=n;++i)
{
for(j=1;j<=n;++j)
{
printf("%d %d= ",i,j);
scanf("%d",&a[i][j]);
}
}
printf("Matrix B:\n");
for(i=1;i<=n;++i)
{
for(j=1;j<=n;++j)
{
printf("%d %d= ",i,j);
scanf("%d",&b[i][j]);
}
}
printf("Matrix A+B:\n");
for(i=1;i<=n;++i)
{
for(j=1;j<=n;++j)
{
c[i][j]=a[i][j]+b[i][j];
printf("%d ",c[i][j]);
}
printf("\n");
}
printf("Matrix A-B:\n");
for(i=1;i<=n;++i)
{
for(j=1;j<=n;++j)
{
d[i][j]=a[i][j]-b[i][j];
printf("%d ",d[i][j]);
}
printf("\n");
}
getch();
return 0;
}
#include<conio.h>
main()
{
int a[20][20],b[20][20],c[20][20],d[20][20],i,j,n;
printf("Enter the value of n= ");
scanf("%d",&n);
printf("Matrix A:\n");
for(i=1;i<=n;++i)
{
for(j=1;j<=n;++j)
{
printf("%d %d= ",i,j);
scanf("%d",&a[i][j]);
}
}
printf("Matrix B:\n");
for(i=1;i<=n;++i)
{
for(j=1;j<=n;++j)
{
printf("%d %d= ",i,j);
scanf("%d",&b[i][j]);
}
}
printf("Matrix A+B:\n");
for(i=1;i<=n;++i)
{
for(j=1;j<=n;++j)
{
c[i][j]=a[i][j]+b[i][j];
printf("%d ",c[i][j]);
}
printf("\n");
}
printf("Matrix A-B:\n");
for(i=1;i<=n;++i)
{
for(j=1;j<=n;++j)
{
d[i][j]=a[i][j]-b[i][j];
printf("%d ",d[i][j]);
}
printf("\n");
}
getch();
return 0;
}
solution of balagurusamy of chapter 7.14
#include<stdio.h>
main()
{
char ch[14],i,s=0,d=1;
printf("Enter the ISBN number:\n");
scanf("%s",&ch);
for(i=0;i<=12;++i)
{
if(ch[i]<=9)
{
s+=ch[i]*d;
d+=1;
}
}
if(s%11==ch[13])
{
printf("Valid ISBN");
}
else
{
printf("Invalid ISBN");
}
return 0;
}
main()
{
char ch[14],i,s=0,d=1;
printf("Enter the ISBN number:\n");
scanf("%s",&ch);
for(i=0;i<=12;++i)
{
if(ch[i]<=9)
{
s+=ch[i]*d;
d+=1;
}
}
if(s%11==ch[13])
{
printf("Valid ISBN");
}
else
{
printf("Invalid ISBN");
}
return 0;
}
solution of balagurusamy of chapter 7.13
#include<stdio.h>
main()
{
int a[10][10],i,j,r,c;
printf("Enter the row number: ");
scanf("%d",&r);
printf("Enter the coloumn number: ");
scanf("%d",&c);
printf("Enter Matrix:\n");
for(i=1;i<=r;++i)
{
for(j=1;j<=c;++j)
{
printf("%d %d= ",i,j);
scanf("%d",&a[i][j]);
}
}
printf("Transpose of the Matrix:\n");
for(j=1;j<=c;++j)
{
for(i=1;i<=r;++i)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
return 0;
}
main()
{
int a[10][10],i,j,r,c;
printf("Enter the row number: ");
scanf("%d",&r);
printf("Enter the coloumn number: ");
scanf("%d",&c);
printf("Enter Matrix:\n");
for(i=1;i<=r;++i)
{
for(j=1;j<=c;++j)
{
printf("%d %d= ",i,j);
scanf("%d",&a[i][j]);
}
}
printf("Transpose of the Matrix:\n");
for(j=1;j<=c;++j)
{
for(i=1;i<=r;++i)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
return 0;
}
solution of balagurusamy of chapter 7.12
#include<stdio.h>
#include<conio.h>
#include<STRING.H>
main()
{
char ch[30],ca,i,c=0;
printf("Enter the text:\n");
gets(ch);
printf("Enter the character:\n");
ca=getchar();
for(i=0;ch!='\0';++i)
{
if(ch[i]==ca)
{
c+=1;
}
}
printf("Number of occurances= %d",c);
return 0;
}
#include<conio.h>
#include<STRING.H>
main()
{
char ch[30],ca,i,c=0;
printf("Enter the text:\n");
gets(ch);
printf("Enter the character:\n");
ca=getchar();
for(i=0;ch!='\0';++i)
{
if(ch[i]==ca)
{
c+=1;
}
}
printf("Number of occurances= %d",c);
return 0;
}
solution of balagurusamy of chapter 7.11
#include<stdio.h>
#include<STRING.H>
main()
{
char c[25];
int i,s=0;
printf("Enter the text:\n");
gets(c);
for(i=0;c[i]!='\0';++i)
{
s+=1;
}
printf("Length of the string= %d",s);
return 0;
}
#include<STRING.H>
main()
{
char c[25];
int i,s=0;
printf("Enter the text:\n");
gets(c);
for(i=0;c[i]!='\0';++i)
{
s+=1;
}
printf("Length of the string= %d",s);
return 0;
}
solution of balagurusamy of chapter 7.9
#include<stdio.h>
main()
{
int i,j,n;
long unsigned a[1000],t;
printf("Enter the number of terms= ");
scanf("%d",&n);
printf("\n");
for(i=1;i<=n;++i)
{
printf("Enter number %d= ",i);
scanf("%lu",&a[i]);
}
for(i=1;i<=n-1;++i)
{
for(j=1;j<=n-i;++j)
{
if(a[i]>a[i+j])
{
t=a[i+j];
a[i+j]=a[i];
a[i]=t;
}
}
}
printf("\nAscending order:\n");
for(i=1;i<=n;++i)
{
printf("%lu ",a[i]);
}
return 0;
}
main()
{
int i,j,n;
long unsigned a[1000],t;
printf("Enter the number of terms= ");
scanf("%d",&n);
printf("\n");
for(i=1;i<=n;++i)
{
printf("Enter number %d= ",i);
scanf("%lu",&a[i]);
}
for(i=1;i<=n-1;++i)
{
for(j=1;j<=n-i;++j)
{
if(a[i]>a[i+j])
{
t=a[i+j];
a[i+j]=a[i];
a[i]=t;
}
}
}
printf("\nAscending order:\n");
for(i=1;i<=n;++i)
{
printf("%lu ",a[i]);
}
return 0;
}
solution of balagurusamy of chapter 7.8
#include<stdio.h>
main()
{
int a[6][6],i,j;
for(i=1;i<=5;++i)
{
for(j=1;j<=5;++j)
{
if(i+j<6)
a[i][j]=1;
else if(i+j==6)
a[i][j]=0;
else
a[i][j]=-1;
}
}
for(i=1;i<=5;++i)
{
for(j=1;j<=5;++j)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
return 0;
}
main()
{
int a[6][6],i,j;
for(i=1;i<=5;++i)
{
for(j=1;j<=5;++j)
{
if(i+j<6)
a[i][j]=1;
else if(i+j==6)
a[i][j]=0;
else
a[i][j]=-1;
}
}
for(i=1;i<=5;++i)
{
for(j=1;j<=5;++j)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
return 0;
}
solution of balagurusamy of chapter 7.7
#include<stdio.h>
main()
{
int i,j,k,n,a[11][11],b[11][11],c[11][11]={{0}};
printf("Enter the value of n: \n");
scanf("%d",&n);
printf("Matrix A:\n");
for(i=1;i<=n;++i)
{
for(j=1;j<=n;++j)
{
scanf("%d",&a[i][j]);
}
}
printf("Matrix B:\n");
for(i=1;i<=n;++i)
{
for(j=1;j<=n;++j)
{
scanf("%d",&b[i][j]);
}
}
printf("Multiplied matrix\n");
for(i=1;i<=n;++i)
{
for(j=1;j<=n;++j)
{
for(k=1;k<=n;++k)
{
c[i][k]+=a[i][j]*b[j][k];
}
}
}
for(i=1;i<=n;++i)
{
for(j=1;j<=n;++j)
{
printf("%d ",c[i][j]);
}
printf("\n");
}
return 0;
}
main()
{
int i,j,k,n,a[11][11],b[11][11],c[11][11]={{0}};
printf("Enter the value of n: \n");
scanf("%d",&n);
printf("Matrix A:\n");
for(i=1;i<=n;++i)
{
for(j=1;j<=n;++j)
{
scanf("%d",&a[i][j]);
}
}
printf("Matrix B:\n");
for(i=1;i<=n;++i)
{
for(j=1;j<=n;++j)
{
scanf("%d",&b[i][j]);
}
}
printf("Multiplied matrix\n");
for(i=1;i<=n;++i)
{
for(j=1;j<=n;++j)
{
for(k=1;k<=n;++k)
{
c[i][k]+=a[i][j]*b[j][k];
}
}
}
for(i=1;i<=n;++i)
{
for(j=1;j<=n;++j)
{
printf("%d ",c[i][j]);
}
printf("\n");
}
return 0;
}
solution of balagurusamy of chapter 7.6
#include<stdio.h>
main()
{
int a[21],b[21],c[21],i,j,n,d=1,e=0;
printf("Enter n= ");
scanf("%d",&n);
printf("Array a:\n");
for(i=1;i<=n;++i)
{
printf("a[%d]= ",i);
scanf("%d",&a[i]);
}
printf("Array b:\n");
for(i=1;i<=n;++i)
{
printf("b[%d]= ",i);
scanf("%d",&b[i]);
}
for(i=1;i<=n;++i)
{
for(j=d;j<=n;++j)
{
e+=1;
if(a[i]<=b[j])
{
c[e]=a[i];
break;
}
else
{
c[e]=b[j];
i-=1;
d+=1;
break;
}
}
}
e+=1;
if(a[n]<b[n])
c[2*n]=b[n];
else
c[2*n]=a[n];
printf("Merged sorted array:\n");
for(i=1;i<=e;++i)
{
printf("%d\n",c[i]);
}
return 0;
}
main()
{
int a[21],b[21],c[21],i,j,n,d=1,e=0;
printf("Enter n= ");
scanf("%d",&n);
printf("Array a:\n");
for(i=1;i<=n;++i)
{
printf("a[%d]= ",i);
scanf("%d",&a[i]);
}
printf("Array b:\n");
for(i=1;i<=n;++i)
{
printf("b[%d]= ",i);
scanf("%d",&b[i]);
}
for(i=1;i<=n;++i)
{
for(j=d;j<=n;++j)
{
e+=1;
if(a[i]<=b[j])
{
c[e]=a[i];
break;
}
else
{
c[e]=b[j];
i-=1;
d+=1;
break;
}
}
}
e+=1;
if(a[n]<b[n])
c[2*n]=b[n];
else
c[2*n]=a[n];
printf("Merged sorted array:\n");
for(i=1;i<=e;++i)
{
printf("%d\n",c[i]);
}
return 0;
}
solution of balagurusamy of chapter 7.4
#include<stdio.h>
#include<conio.h>
main()
{
int a[20][20],b[20][20],c[20][20],d[20][20],i,j,n;
printf("Enter the value of n= ");
scanf("%d",&n);
printf("Matrix A:\n");
for(i=1;i<=n;++i)
{
for(j=1;j<=n;++j)
{
printf("%d %d= ",i,j);
scanf("%d",&a[i][j]);
}
}
printf("Matrix B:\n");
for(i=1;i<=n;++i)
{
for(j=1;j<=n;++j)
{
printf("%d %d= ",i,j);
scanf("%d",&b[i][j]);
}
}
printf("Matrix A+B:\n");
for(i=1;i<=n;++i)
{
for(j=1;j<=n;++j)
{
c[i][j]=a[i][j]+b[i][j];
printf("%d ",c[i][j]);
}
printf("\n");
}
printf("Matrix A-B:\n");
for(i=1;i<=n;++i)
{
for(j=1;j<=n;++j)
{
d[i][j]=a[i][j]-b[i][j];
printf("%d ",d[i][j]);
}
printf("\n");
}
getch();
return 0;
}
#include<conio.h>
main()
{
int a[20][20],b[20][20],c[20][20],d[20][20],i,j,n;
printf("Enter the value of n= ");
scanf("%d",&n);
printf("Matrix A:\n");
for(i=1;i<=n;++i)
{
for(j=1;j<=n;++j)
{
printf("%d %d= ",i,j);
scanf("%d",&a[i][j]);
}
}
printf("Matrix B:\n");
for(i=1;i<=n;++i)
{
for(j=1;j<=n;++j)
{
printf("%d %d= ",i,j);
scanf("%d",&b[i][j]);
}
}
printf("Matrix A+B:\n");
for(i=1;i<=n;++i)
{
for(j=1;j<=n;++j)
{
c[i][j]=a[i][j]+b[i][j];
printf("%d ",c[i][j]);
}
printf("\n");
}
printf("Matrix A-B:\n");
for(i=1;i<=n;++i)
{
for(j=1;j<=n;++j)
{
d[i][j]=a[i][j]-b[i][j];
printf("%d ",d[i][j]);
}
printf("\n");
}
getch();
return 0;
}
solution of balagurusamy of chapter 7.1
#include<stdio.h>
void main(void)
{
int n,i;
float m,c,x[100],y[100],a=0,b=0,d=0,e=0,f;
printf("Enter the number of co-ordinates: ");
scanf("%d",&n);
for(i=1;i<=n;++i)
{
printf("x (%d)= ",i);
scanf("%f",&x[i]);
printf("y (%d)= ",i);
scanf("%f",&y[i]);
}
for(i=1;i<=n;++i)
{
a+=x[i]*y[i];
b+=x[i];
d+=y[i];
e+=x[i]*x[i];
}
f=b*b;
m=(n*a-b*d)/(float)(n*e-f);
c=(1/(float)n)*(d-m*b);
printf("\n");
printf("\nEquation of the straight line:-\n\n");
printf("y=%.1fx + %.1f",m,c);
}
void main(void)
{
int n,i;
float m,c,x[100],y[100],a=0,b=0,d=0,e=0,f;
printf("Enter the number of co-ordinates: ");
scanf("%d",&n);
for(i=1;i<=n;++i)
{
printf("x (%d)= ",i);
scanf("%f",&x[i]);
printf("y (%d)= ",i);
scanf("%f",&y[i]);
}
for(i=1;i<=n;++i)
{
a+=x[i]*y[i];
b+=x[i];
d+=y[i];
e+=x[i]*x[i];
}
f=b*b;
m=(n*a-b*d)/(float)(n*e-f);
c=(1/(float)n)*(d-m*b);
printf("\n");
printf("\nEquation of the straight line:-\n\n");
printf("y=%.1fx + %.1f",m,c);
}
solution of cosine series
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main(void)
{
int i,j,n,factorial,sign;
double a,sum=0,x,term;
printf("Enter the value of x in degrees:=\n\t\t");
scanf("%lf",&x);
x=3.142*x/180.0;
printf("Enter the no of terms:= ");
scanf("%d",&n);
for(i=1;i<=n;++i)
{
for(factorial=1,j=1;j<=2*(i-1);++j)
factorial*=j;
sign=pow((-1),i+1);
a=pow(x,2*(i-1))/(double)factorial;
if(a<=.0001)
break;
term=sign*a;
sum+=term;
}
printf("cos(%.4lf)=%lf",x,sum);
getch();
}
find root of a equation using newton raphson method
/*This program finds the root of the equations (x^5)+(x^4)+(x^3)+(x^2)+(x^1)+5=0
and 5(x^2)+2exp(-2x)=0 numerically using the Newton-Raphson method*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
float NRiter(float);
float func(float);
float diff(float);
int c=1, count=0;
int main()
{
float x, r;
printf("For the equation (x^5)+(x^4)+(x^3)+(x^2)+(x^1)+5=0 \n\n");
start:
printf("x0 = ");
scanf("%f", &x);
r = NRiter(x);
if(r==0) printf("This equation does not have a real solution");
else printf("The root is %f\n\n\n", r);
if(c==1)
{
printf("For the equation 5(x^2)+2exp(-2x)=0 \n\n");
c=2;
goto start;
}
getch();
return 0;
}
float NRiter(float x)
{
float t, r, d;
++count;
r = x - func(x)/diff(x);
t = fabs(func(r));
if(count>100) return 0;
if(t<0.001) return r;
else
{
d = NRiter(r);
return d;
}
}
float func(float x)
{
float y;
if(c==1) y = pow(x,5)+pow(x,4)+pow(x,3)+pow(x,2)+pow(x,1)+5;
else y = 5*pow(x,2)+2*exp(-2*x);
return y;
}
float diff(float x)
{
float d;
if(c==1) d = 5*pow(x,4)+4*pow(x,3)+3*pow(x,2)+2*pow(x,1)+1*pow(x,0);
else d = 10*x-4*exp(-2*x);
return d;
}
and 5(x^2)+2exp(-2x)=0 numerically using the Newton-Raphson method*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
float NRiter(float);
float func(float);
float diff(float);
int c=1, count=0;
int main()
{
float x, r;
printf("For the equation (x^5)+(x^4)+(x^3)+(x^2)+(x^1)+5=0 \n\n");
start:
printf("x0 = ");
scanf("%f", &x);
r = NRiter(x);
if(r==0) printf("This equation does not have a real solution");
else printf("The root is %f\n\n\n", r);
if(c==1)
{
printf("For the equation 5(x^2)+2exp(-2x)=0 \n\n");
c=2;
goto start;
}
getch();
return 0;
}
float NRiter(float x)
{
float t, r, d;
++count;
r = x - func(x)/diff(x);
t = fabs(func(r));
if(count>100) return 0;
if(t<0.001) return r;
else
{
d = NRiter(r);
return d;
}
}
float func(float x)
{
float y;
if(c==1) y = pow(x,5)+pow(x,4)+pow(x,3)+pow(x,2)+pow(x,1)+5;
else y = 5*pow(x,2)+2*exp(-2*x);
return y;
}
float diff(float x)
{
float d;
if(c==1) d = 5*pow(x,4)+4*pow(x,3)+3*pow(x,2)+2*pow(x,1)+1*pow(x,0);
else d = 10*x-4*exp(-2*x);
return d;
}
find area of a given curve usinh trapezoidal rule
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define e 2.71828
float func(float);
int main ()
{
float n,a,b,i;
float sum=0,term,area,h;
printf("entr no of trp ,up lim,low lim") ;
scanf("%d %d %d",&n,&b,&a);
for(i=2;i<=n;++i)
{term=func(i);
sum=sum+term;}
h=(b-a)/n;
area=0.5*h*(func(1)+func(n+1)+2*sum);
printf("%lf",area);
getch();
return 0;
}
float func(float x)
{float y;
y=4*pow(e,-x);
return y;
}
#include <conio.h>
#include <math.h>
#define e 2.71828
float func(float);
int main ()
{
float n,a,b,i;
float sum=0,term,area,h;
printf("entr no of trp ,up lim,low lim") ;
scanf("%d %d %d",&n,&b,&a);
for(i=2;i<=n;++i)
{term=func(i);
sum=sum+term;}
h=(b-a)/n;
area=0.5*h*(func(1)+func(n+1)+2*sum);
printf("%lf",area);
getch();
return 0;
}
float func(float x)
{float y;
y=4*pow(e,-x);
return y;
}
c program for sine inverse
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define LIMIT 55
main(){
double i,up=1,down=2,nm,dm,x,sum,term;
printf("Enter the value of x\t");
scanf("%lf",&x);
sum=x;
for(i=3;i<=LIMIT;i=i+2){
nm=pow(x,i);
dm=i;
term=(up*nm)/(down*dm);
sum=sum+term;
up=up+2;
down=down+2;
}
sum=sum*180/3.1416;
printf("sin inverse(%lf)=%f",x,sum);
getch();
}
#include<conio.h>
#include<math.h>
#define LIMIT 55
main(){
double i,up=1,down=2,nm,dm,x,sum,term;
printf("Enter the value of x\t");
scanf("%lf",&x);
sum=x;
for(i=3;i<=LIMIT;i=i+2){
nm=pow(x,i);
dm=i;
term=(up*nm)/(down*dm);
sum=sum+term;
up=up+2;
down=down+2;
}
sum=sum*180/3.1416;
printf("sin inverse(%lf)=%f",x,sum);
getch();
}
solution of sine series
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
float t, x, nu, sum = 0;
int y, n, a, b, c, de=1;
printf("Enter the value of x and n: \n");
scanf("%f %d", &x, &n);
for(a=1; a<=n; ++a) {
y = 2*a-1;
nu = pow(x, y);
for(de=1, b=1; b<=2*a-1; ++b) de*=b;
t = nu*pow(-1, a+1)/de;
sum+=t;
}
printf("%f", sum);
getch();
return 0;
}
#include <conio.h>
#include <math.h>
int main()
{
float t, x, nu, sum = 0;
int y, n, a, b, c, de=1;
printf("Enter the value of x and n: \n");
scanf("%f %d", &x, &n);
for(a=1; a<=n; ++a) {
y = 2*a-1;
nu = pow(x, y);
for(de=1, b=1; b<=2*a-1; ++b) de*=b;
t = nu*pow(-1, a+1)/de;
sum+=t;
}
printf("%f", sum);
getch();
return 0;
}
a c program to reverse number
#include<stdio.h>
#include<conio.h>
#include<math.h>
main(){
long int num,i,a,b;
printf("Enter an integer:\n");
scanf("%ld",&num);
b=num;
for(i=1; ;++i){
a=b%10;
printf("%ld",a);
b=b/10;
if(b==0)
break;
}
getch();
}
#include<conio.h>
#include<math.h>
main(){
long int num,i,a,b;
printf("Enter an integer:\n");
scanf("%ld",&num);
b=num;
for(i=1; ;++i){
a=b%10;
printf("%ld",a);
b=b/10;
if(b==0)
break;
}
getch();
}
a c program for prime factor
#include<stdio.h>
#include<conio.h>
#include<math.h>
main(){
int num;
printf("Enter a number:\t");
scanf("%d",&num);
for(int i=2;i<=num;i++){
if(num%i!=0){continue;}
printf("%d ",i);
num=num/i;
i=i-1;
}
getch();
}
#include<conio.h>
#include<math.h>
main(){
int num;
printf("Enter a number:\t");
scanf("%d",&num);
for(int i=2;i<=num;i++){
if(num%i!=0){continue;}
printf("%d ",i);
num=num/i;
i=i-1;
}
getch();
}
a c program for prime determination
#include<stdio.h>
#include<conio.h>
main(){
int i,j,num;
printf("enter a number:");
scanf("%d",&num);
for(i=2;i<=num/2;i++){
if(num%i==0){
printf("not a prime number");
goto skip;
}
}
printf("prime number");
skip:
getch();
}
#include<conio.h>
main(){
int i,j,num;
printf("enter a number:");
scanf("%d",&num);
for(i=2;i<=num/2;i++){
if(num%i==0){
printf("not a prime number");
goto skip;
}
}
printf("prime number");
skip:
getch();
}
a c program for prime count
#include<stdio.h>
#include<conio.h>
#include<math.h>
main(){
int num;
int n=2,div,k;
printf("utpo how much do you want to count?__");
scanf("%d",&num);
start:
for(n=n;n<=num;n++){
for(div=2;div<n;div++){
if(n%div==0){
n=n+1;
goto start;
}
}
printf("%d, ",n);
}
getch();
}
#include<conio.h>
#include<math.h>
main(){
int num;
int n=2,div,k;
printf("utpo how much do you want to count?__");
scanf("%d",&num);
start:
for(n=n;n<=num;n++){
for(div=2;div<n;div++){
if(n%div==0){
n=n+1;
goto start;
}
}
printf("%d, ",n);
}
getch();
}
a c program for polynomial
#include <stdio.h>
#include <conio.h>
int main()
{
int n=9, x, i, j, p ;
printf("Enter the value of x: ");
scanf("%d", &x);
int co[n];
printf("Enter the coefficients \n");
for(i=0; i<n; ++i)
{
scanf("%d", &co[i]);
}
p = co[n-1];
for(j=n-1; j>0; --j)
{
p = co[j-1] + x*p;
}
printf("%d", p);
getch();
return 0;
}
#include <conio.h>
int main()
{
int n=9, x, i, j, p ;
printf("Enter the value of x: ");
scanf("%d", &x);
int co[n];
printf("Enter the coefficients \n");
for(i=0; i<n; ++i)
{
scanf("%d", &co[i]);
}
p = co[n-1];
for(j=n-1; j>0; --j)
{
p = co[j-1] + x*p;
}
printf("%d", p);
getch();
return 0;
}
parallelogram
#include<stdio.h>
#include<conio.h>
#include<math.h>
main( )
{
int i,j,k,l,lin;
printf("Plz enter number of lines:\n");
scanf("%d",&lin);
for(i=1;i<=lin;i++){
for(j=1;j<=lin-i;j++){
printf(" ");
}
printf("/");
for(k=1;k<=(lin*5/2);k++){
printf("*");
}
printf("/");
printf("\n");
}
#include<conio.h>
#include<math.h>
main( )
{
int i,j,k,l,lin;
printf("Plz enter number of lines:\n");
scanf("%d",&lin);
for(i=1;i<=lin;i++){
for(j=1;j<=lin-i;j++){
printf(" ");
}
printf("/");
for(k=1;k<=(lin*5/2);k++){
printf("*");
}
printf("/");
printf("\n");
}
floyd's triangle of 0 & 1
#include<stdio.h>
#include<conio.h>
#include<math.h>
main(){
int i,j,k=1,m=0;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
if(i%2==1){
printf("%d ",k);
k++;
if(k>1)
{
k=0;
}
}
if(i%2==0)
{
printf("%d ",m);
m++;
if(m>1)
{
m=0;
}
}
}
k=1; /*in exam, i missed this part*/
m=0;
printf("\n");
}
getch();
}
#include<conio.h>
#include<math.h>
main(){
int i,j,k=1,m=0;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
if(i%2==1){
printf("%d ",k);
k++;
if(k>1)
{
k=0;
}
}
if(i%2==0)
{
printf("%d ",m);
m++;
if(m>1)
{
m=0;
}
}
}
k=1; /*in exam, i missed this part*/
m=0;
printf("\n");
}
getch();
}
c program for fibonacci
#include<stdio.h>
#include<conio.h>
#include<math.h>
main(){
int a,lim,i,F1=1,F2=1,F3;
printf("Enter the limiting step:\t");
scanf("%d",&lim);
if(a==1)
printf("%d",a);
for(i=1;i<=lim;i++){
F3=F1+F2;
printf("%d ",F3);
F1=F2;
F2=F3;
}
#include<conio.h>
#include<math.h>
main(){
int a,lim,i,F1=1,F2=1,F3;
printf("Enter the limiting step:\t");
scanf("%d",&lim);
if(a==1)
printf("%d",a);
for(i=1;i<=lim;i++){
F3=F1+F2;
printf("%d ",F3);
F1=F2;
F2=F3;
}
c program to incorporate the elements of two one-dimensional arrays (A and B) into a single one-dimensional array (C) and arranges them in ascending order (using bubble-sort)*
#include <stdio.h>
#include <conio.h>
int main()
{
int A[5]={1,2,3,4,5}, B[5]={2,4,6,8,10};
int C[10];
int a, b, c, t, p;
for(a=0; a<=4; ++a)
{
C[a]=A[a];
C[a+5]=B[a];
}
for(b=1; b<=9; ++b)
{
for(c=1; c<=10-b; ++c)
{
if(C[c]<C[c-1])
{ t=C[c-1];
C[c-1]=C[c];
C[c]=t;
}
}
}
for(p=0; p<10; ++p) printf("%d ", C[p]);
getch();
return 0;
}
#include <conio.h>
int main()
{
int A[5]={1,2,3,4,5}, B[5]={2,4,6,8,10};
int C[10];
int a, b, c, t, p;
for(a=0; a<=4; ++a)
{
C[a]=A[a];
C[a+5]=B[a];
}
for(b=1; b<=9; ++b)
{
for(c=1; c<=10-b; ++c)
{
if(C[c]<C[c-1])
{ t=C[c-1];
C[c-1]=C[c];
C[c]=t;
}
}
}
for(p=0; p<10; ++p) printf("%d ", C[p]);
getch();
return 0;
}
factorial using recursive function
#include<stdio.h>
#include<conio.h>
int fact(int);
int main()
{
int a,b;
scanf("%d",&a);
b=fact(a);
printf("%d",b);
getch();
}
int fact(int x)
{
int fac;
if(x==1)
return 1;
else
fac=x*fact(x-1);
return(fac);
}
#include<conio.h>
int fact(int);
int main()
{
int a,b;
scanf("%d",&a);
b=fact(a);
printf("%d",b);
getch();
}
int fact(int x)
{
int fac;
if(x==1)
return 1;
else
fac=x*fact(x-1);
return(fac);
}
a c program to calculate interest using stm ,sym ,ddm method
#include<stdio.h>
#include<conio.h>
void stm(float,int);
void ddm(float,int);
void sym(float,int);
void out_dis(float,float,int);
int main()
{
int c,n;
float p;
printf("Press 1 for stm");
scanf("%d",&c);
printf("enter principle amount and year");
scanf("%f%d",&p,&n);
switch(c)
{
case 1:
printf("Stm method");
stm(p,n);
break;
}
getch();
}
void stm(float x,int y)
{
int i;
float dep=x/y;
for(i=1;i<=y;i++)
{x=x-(dep);
out_dis(x,dep,i);
}
}
void out_dis(float x,float dep,int y)
{
printf("\ny:%d red:%f cur :%f\n",y,dep,x);
}
#include<conio.h>
void stm(float,int);
void ddm(float,int);
void sym(float,int);
void out_dis(float,float,int);
int main()
{
int c,n;
float p;
printf("Press 1 for stm");
scanf("%d",&c);
printf("enter principle amount and year");
scanf("%f%d",&p,&n);
switch(c)
{
case 1:
printf("Stm method");
stm(p,n);
break;
}
getch();
}
void stm(float x,int y)
{
int i;
float dep=x/y;
for(i=1;i<=y;i++)
{x=x-(dep);
out_dis(x,dep,i);
}
}
void out_dis(float x,float dep,int y)
{
printf("\ny:%d red:%f cur :%f\n",y,dep,x);
}
a c program for reverse of an array
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
int i,j,temp;
int a[5]={0};
for(i=0;i<=5;i++)
{
scanf("%d",&a[i]);
}
for(i=0,j=5;i<=2;i++)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
j--;
}
printf("the resultant reverse: \n");
for(i=0;i<=5;i++)
{
printf(" %d ",a[i]);
}
getch();
return 0;
}
#include <conio.h>
#include <math.h>
int main()
{
int i,j,temp;
int a[5]={0};
for(i=0;i<=5;i++)
{
scanf("%d",&a[i]);
}
for(i=0,j=5;i<=2;i++)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
j--;
}
printf("the resultant reverse: \n");
for(i=0;i<=5;i++)
{
printf(" %d ",a[i]);
}
getch();
return 0;
}
a c program to generate pascal using array
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
int i,j,k,rows,columns;
int a[100][100]={0};
printf("Enter the value rows :");
scanf("%d",&rows);
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;j++)
{
if(j==1||j==i)
{a[i][j]=1;
}
else
{a[i][j]=a[i-1][j-1]+a[i-1][j];}
}
}
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;j++)
{printf("%d ",a[i][j]);
}
printf("\n");
}
getch();
return 0;
}
#include <conio.h>
#include <math.h>
int main()
{
int i,j,k,rows,columns;
int a[100][100]={0};
printf("Enter the value rows :");
scanf("%d",&rows);
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;j++)
{
if(j==1||j==i)
{a[i][j]=1;
}
else
{a[i][j]=a[i-1][j-1]+a[i-1][j];}
}
}
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;j++)
{printf("%d ",a[i][j]);
}
printf("\n");
}
getch();
return 0;
}
a c program for array polynomial
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define LIMIT 55
main(){
double i,up=1,down=2,nm,dm,x,sum,term;
printf("Enter the value of x\t");
scanf("%lf",&x);
sum=x;
for(i=3;i<=LIMIT;i=i+2){
nm=pow(x,i);
dm=i;
term=(up*nm)/(down*dm);
sum=sum+term;
up=up+2;
down=down+2;
}
sum=sum*180/3.1416;
printf("sin inverse(%lf)=%f",x,sum);
getch();
}
#include<conio.h>
#include<math.h>
#define LIMIT 55
main(){
double i,up=1,down=2,nm,dm,x,sum,term;
printf("Enter the value of x\t");
scanf("%lf",&x);
sum=x;
for(i=3;i<=LIMIT;i=i+2){
nm=pow(x,i);
dm=i;
term=(up*nm)/(down*dm);
sum=sum+term;
up=up+2;
down=down+2;
}
sum=sum*180/3.1416;
printf("sin inverse(%lf)=%f",x,sum);
getch();
}
Write a program in c to sort an unsorted array using bubble sort method?
#include<stdio.h>
#include<conio.h>
main()
{
int arr[50],temp,i,j,n;
clrscr();
printf("\nEnter any Value less Than 50");
scanf("%d",&n);
printf("\n\tEnter The Values into ARRAY ");
for(i=0;i<n;i++)
{
printf("\n\n Enter Element no %d: ",i+1);
scanf("%d",&arr[i]);
}
for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(arr[j] >arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf("\n\n-- Sorted Series --");
for(i=0;i<n;i++)
{
printf("\n \n \t %d",arr[i]);
}
getch();
}
#include<conio.h>
main()
{
int arr[50],temp,i,j,n;
clrscr();
printf("\nEnter any Value less Than 50");
scanf("%d",&n);
printf("\n\tEnter The Values into ARRAY ");
for(i=0;i<n;i++)
{
printf("\n\n Enter Element no %d: ",i+1);
scanf("%d",&arr[i]);
}
for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(arr[j] >arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf("\n\n-- Sorted Series --");
for(i=0;i<n;i++)
{
printf("\n \n \t %d",arr[i]);
}
getch();
}
a program in c to sort an unsorted array using selection sort?
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n,i,j,temp,a[50];
printf("\nEnter how many elements=");
scanf("%d",&n);
printf("\nEnter %d elements",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[j];
a[j]=a[i];
a[i]=temp;
}
}
}
printf("\nSorted Array:\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
getch();
}
#include<conio.h>
void main()
{
clrscr();
int n,i,j,temp,a[50];
printf("\nEnter how many elements=");
scanf("%d",&n);
printf("\nEnter %d elements",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[j];
a[j]=a[i];
a[i]=temp;
}
}
}
printf("\nSorted Array:\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
getch();
}
C program to merge two arrays?
#include<stdio.h>
int main()
{
int A[50],B[50],C[50],MN=0,M,N,a,b,c;
printf("enter size of 1st array\n");
scanf("%d",&M);
printf("enter size of 2nd array\n");
scanf("%d",&N);
MN=M+N;
printf("enter 1st arrays elements (asending)\n");
for(i=0;i<N,i++)
scanf("%d",&A[i]);
printf("enter 2nd array elements (desending)\n");
for(i=0;i<n;i++)
scanf("%d",&B[i]);
for(a=0;b=N-1;c=0;a<M && b>=0)
{
if[A[a]<=B[b]],c[c+1]=A[a+1];
else
c[c+i]=B(b--);
}
if(a<M)
{
while(a<M),c[c++]=A[a++];
}
else
{
while(b>=0)
c[c++]=B[b--];
}
return o;
}
int main()
{
int A[50],B[50],C[50],MN=0,M,N,a,b,c;
printf("enter size of 1st array\n");
scanf("%d",&M);
printf("enter size of 2nd array\n");
scanf("%d",&N);
MN=M+N;
printf("enter 1st arrays elements (asending)\n");
for(i=0;i<N,i++)
scanf("%d",&A[i]);
printf("enter 2nd array elements (desending)\n");
for(i=0;i<n;i++)
scanf("%d",&B[i]);
for(a=0;b=N-1;c=0;a<M && b>=0)
{
if[A[a]<=B[b]],c[c+1]=A[a+1];
else
c[c+i]=B(b--);
}
if(a<M)
{
while(a<M),c[c++]=A[a++];
}
else
{
while(b>=0)
c[c++]=B[b--];
}
return o;
}
a c program to find the determinant ,cofactor,transpose and inverse of a matrix
#include<stdio.h>
#include<math.h>
float detrm(float[][],float);
void cofact(float[][],float);
void trans(float[][],float[][],float);
main()
{
float a[25][25],k,d;
int i,j;
printf("ENTER THE ORDER OF THE MATRIX:\n");
scanf("%f",&k);
printf("ENTER THE ELEMENTS OF THE MATRIX:\n");
for(i=0;i<k;i++)
{
for(j=0;j<k;j++)
{
scanf("%f",&a[i][j]);
}
}
d=detrm(a,k);
printf("THE DETERMINANT IS=%f",d);
if(d==0)
printf("\nMATRIX IS NOT INVERSIBLE\n");
else
cofact(a,k);
}
/******************FUNCTION TO FIND THE DETERMINANT OF THE MATRIX************************/
float detrm(float a[25][25],float k)
{
float s=1,det=0,b[25][25];
int i,j,m,n,c;
if(k==1)
{
return(a[0][0]);
}
else
{
det=0;
for(c=0;c<k;c++)
{
m=0;
n=0;
for(i=0;i<k;i++)
{
for(j=0;j<k;j++)
{
b[i][j]=0;
if(i!=0&&j!=c)
{
b[m][n]=a[i][j];
if(n<(k-2))
n++;
else
{
n=0;
m++;
}
}
}
}
det=det+s*(a[0][c]*detrm(b,k-1));
s=-1*s;
}
}
return(det);
}
/*******************FUNCTION TO FIND COFACTOR*********************************/
void cofact(float num[25][25],float f)
{
float b[25][25],fac[25][25];
int p,q,m,n,i,j;
for(q=0;q<f;q++)
{
for(p=0;p<f;p++)
{
m=0;
n=0;
for(i=0;i<f;i++)
{
for(j=0;j<f;j++)
{
b[i][j]=0;
if(i!=q&&j!=p)
{
b[m][n]=num[i][j];
if(n<(f-2))
n++;
else
{
n=0;
m++;
}
}
}
}
fac[q][p]=pow(-1,q+p)*detrm(b,f-1);
}
}
trans(num,fac,f);
}
/*************FUNCTION TO FIND TRANSPOSE AND INVERSE OF A MATRIX**************************/
void trans(float num[25][25],float fac[25][25],float r)
{
int i,j;
float b[25][25],inv[25][25],d;
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
b[i][j]=fac[j][i];
}
}
d=detrm(num,r);
inv[i][j]=0;
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
inv[i][j]=b[i][j]/d;
}
}
printf("\nTHE INVERSE OF THE MATRIX:\n");
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
printf("\t%f",inv[i][j]);
}
printf("\n");
}
}
#include<math.h>
float detrm(float[][],float);
void cofact(float[][],float);
void trans(float[][],float[][],float);
main()
{
float a[25][25],k,d;
int i,j;
printf("ENTER THE ORDER OF THE MATRIX:\n");
scanf("%f",&k);
printf("ENTER THE ELEMENTS OF THE MATRIX:\n");
for(i=0;i<k;i++)
{
for(j=0;j<k;j++)
{
scanf("%f",&a[i][j]);
}
}
d=detrm(a,k);
printf("THE DETERMINANT IS=%f",d);
if(d==0)
printf("\nMATRIX IS NOT INVERSIBLE\n");
else
cofact(a,k);
}
/******************FUNCTION TO FIND THE DETERMINANT OF THE MATRIX************************/
float detrm(float a[25][25],float k)
{
float s=1,det=0,b[25][25];
int i,j,m,n,c;
if(k==1)
{
return(a[0][0]);
}
else
{
det=0;
for(c=0;c<k;c++)
{
m=0;
n=0;
for(i=0;i<k;i++)
{
for(j=0;j<k;j++)
{
b[i][j]=0;
if(i!=0&&j!=c)
{
b[m][n]=a[i][j];
if(n<(k-2))
n++;
else
{
n=0;
m++;
}
}
}
}
det=det+s*(a[0][c]*detrm(b,k-1));
s=-1*s;
}
}
return(det);
}
/*******************FUNCTION TO FIND COFACTOR*********************************/
void cofact(float num[25][25],float f)
{
float b[25][25],fac[25][25];
int p,q,m,n,i,j;
for(q=0;q<f;q++)
{
for(p=0;p<f;p++)
{
m=0;
n=0;
for(i=0;i<f;i++)
{
for(j=0;j<f;j++)
{
b[i][j]=0;
if(i!=q&&j!=p)
{
b[m][n]=num[i][j];
if(n<(f-2))
n++;
else
{
n=0;
m++;
}
}
}
}
fac[q][p]=pow(-1,q+p)*detrm(b,f-1);
}
}
trans(num,fac,f);
}
/*************FUNCTION TO FIND TRANSPOSE AND INVERSE OF A MATRIX**************************/
void trans(float num[25][25],float fac[25][25],float r)
{
int i,j;
float b[25][25],inv[25][25],d;
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
b[i][j]=fac[j][i];
}
}
d=detrm(num,r);
inv[i][j]=0;
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
inv[i][j]=b[i][j]/d;
}
}
printf("\nTHE INVERSE OF THE MATRIX:\n");
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
printf("\t%f",inv[i][j]);
}
printf("\n");
}
}
multiplication of array
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main ()
{int a[2][3],b[3][2],i,j,k,c[2][2];
for(i=1;i<=2;++i)
{for(k=1;k<=3;++k)
scanf("%d",&a[i][k]);}
for(k=1;k<=3;++k)
{for(j=1;j<=2;++j)
scanf("%d",&b[k][j]);}
for(i=1;i<=2;++i)
{for(j=1;j<=2;++j)
{c[i][j]=0;
for(k=1;k<=3;++k)
c[i][j]+=a[i][k]*b[k][j];}
}
for(i=1;i<=2;++i)
{for(j=1;j<=2;++j)
printf("\n %d",c[i][j]);}
getch();
return 0;
}
#include <conio.h>
#include <math.h>
int main ()
{int a[2][3],b[3][2],i,j,k,c[2][2];
for(i=1;i<=2;++i)
{for(k=1;k<=3;++k)
scanf("%d",&a[i][k]);}
for(k=1;k<=3;++k)
{for(j=1;j<=2;++j)
scanf("%d",&b[k][j]);}
for(i=1;i<=2;++i)
{for(j=1;j<=2;++j)
{c[i][j]=0;
for(k=1;k<=3;++k)
c[i][j]+=a[i][k]*b[k][j];}
}
for(i=1;i<=2;++i)
{for(j=1;j<=2;++j)
printf("\n %d",c[i][j]);}
getch();
return 0;
}
heighest and lowest in a 2D array
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main ()
{int a [31][10],i,j;
for(i=1;i<=3;++i)
for(j=1;j<=2;++j)
{scanf("%d",&a[i][j]);}
for(i=1;i<=3;++i)
for(j=1;j<=2;++j)
{if(a[1][1]>a[i][j])
{a[1][1]=a[i][j];}}
printf("lowest is %d",a[1][1])
;
for(i=1;i<=3;++i)
for(j=1;j<=2;++j)
{if(a[1][1]<a[i][j])
{a[1][1]=a[i][j];}}
printf("\n highst is %d",a[1][1]);
getch();
return 0;
}
#include <conio.h>
#include <math.h>
int main ()
{int a [31][10],i,j;
for(i=1;i<=3;++i)
for(j=1;j<=2;++j)
{scanf("%d",&a[i][j]);}
for(i=1;i<=3;++i)
for(j=1;j<=2;++j)
{if(a[1][1]>a[i][j])
{a[1][1]=a[i][j];}}
printf("lowest is %d",a[1][1])
;
for(i=1;i<=3;++i)
for(j=1;j<=2;++j)
{if(a[1][1]<a[i][j])
{a[1][1]=a[i][j];}}
printf("\n highst is %d",a[1][1]);
getch();
return 0;
}
highest number in a array
#include<stdio.h>
#include<conio.h>
int main()
{int a[5],h=a[0],i;
for(i=0;i<5;++i)
scanf("%d",&a[i]);
for(i=0;i<5;++i)
{if(a[i]>a[0])
{a[0]=a[i];
h=a[0];}
}
printf("highestis %d",h);
getch();
return 0;
}
#include<conio.h>
int main()
{int a[5],h=a[0],i;
for(i=0;i<5;++i)
scanf("%d",&a[i]);
for(i=0;i<5;++i)
{if(a[i]>a[0])
{a[0]=a[i];
h=a[0];}
}
printf("highestis %d",h);
getch();
return 0;
}
solution of balagurusamy of chapter 7.3
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main ()
{ int vot[10],can[6]={0},spilt=0,i;
for(i=1;i<=10;++i)
{scanf("%d",&vot[i]);
if(vot[i]<6)
++can[vot[i]];
else
++spilt;}
for(i=1;i<=5;++i)
{printf("\n%d",can[i]);}
printf("\n%d",spilt);
getch();
return 0;
}
#include <conio.h>
#include <math.h>
int main ()
{ int vot[10],can[6]={0},spilt=0,i;
for(i=1;i<=10;++i)
{scanf("%d",&vot[i]);
if(vot[i]<6)
++can[vot[i]];
else
++spilt;}
for(i=1;i<=5;++i)
{printf("\n%d",can[i]);}
printf("\n%d",spilt);
getch();
return 0;
}
a c program for half diamond
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,k,a,x;
printf("enter the number of diamond=");
scanf("%d",& a);
for(x=1;x<=a;x++)
{
for(i=1;i<=5;i++)
{
for(k=1;k<=5-i;k++)
printf(" ");
for(j=1;j<=2*i-1;j++)
{
printf("*");
}
printf("\n");
}
}
getch();
}
#include<conio.h>
int main()
{
int i,j,k,a,x;
printf("enter the number of diamond=");
scanf("%d",& a);
for(x=1;x<=a;x++)
{
for(i=1;i<=5;i++)
{
for(k=1;k<=5-i;k++)
printf(" ");
for(j=1;j<=2*i-1;j++)
{
printf("*");
}
printf("\n");
}
}
getch();
}
Subscribe to:
Posts (Atom)