Anda di halaman 1dari 14

#include<stdio.

h>
#include<conio.h>

/* The functions that are being used are declared first. Following are
the prototypes of those fuctions.*/

void main_menu();
void mix_programs();
void leapyear();
void descorder();
void p_factorial();
void str_operation();
void reverse();
void merge_string();
void palindrome();
void string_length();
int length(char a[50]);
void mtrx_menu();
void mtrxadd();
void mtrxsub();
void mtrxmul();
void inverse();
void pattern_menu();
void pattern1();
void p1(int);
void pattern2();
void p2(int);
void sq();
void square(int);
void prompt();

/* main function - execution starts from main*/


void main()
{
clrscr();
main_menu();
}

/*Mixed programs menu - this is the main menu for mixed programs*/
void mix_programs()
{
int ch;
clrscr();
printf("1. Leapyear whether given year is a leap year or not");
printf("2. Sort given numbers in decreasing order");
printf("3. Print integers and their factorials");

printf("4. Go back");

printf(">>> Enter your choice: ");


scanf("%d", &ch);
switch(ch)
{
case 1:
clrscr();
leapyear();
getch();
mix_programs();
break;
case 2:
clrscr();
descorder();
getch();
mix_programs();
break;
case 3:
clrscr();
p_factorial();
getch();
mix_programs();
break;
case 4:
main_menu();
default:
clrscr();
prompt();

getch();
mix_programs();
}
}

/*Leap year definition*/


void leapyear()
{
int y;
printf("Enter a year : ");
scanf("%d",&y);
if(y%400==0)
printf("\n%d is a leap year!",y);
else if(y%100==0)
printf("\n%d is not a leap year!",y);
else if(y%4==0)
printf("\n%d is a leap year!",y);
else
printf("\n%d is not a leap year!",y);

/* Sorting in decreasing order definition*/


void descorder()
{
int i, j, n, temp, a[50];
printf("Enter no of inputs: ");
scanf("%d", &n);
printf("\nEnter numbers:\n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
for(i=0; i<n-1; i++)
for(j=1; j<n; j++)
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
printf("\n\nThe numbers in decreasing order are:\n");
for(i=0; i<n; i++)
printf("\n%d",a[i]);
}

/*We are printing factorial of a number*/


void p_factorial()
{
int i, n;
long f=1;
printf("Enter a number between 1 to 10: ");
scanf("%d", &n);
for(i=1; i<=n; i++)
f=f*i;
printf("\nThe factorial of %d is %ld!",n,f);
}

/*THis is the string menu*/


void str_operation()
{
int ch;
clrscr();
printf("1. Reverse a string");

printf("2. Concatenate two strings");

printf("3. Find whether given string is palindrome or not");

printf("4. Find length of a string");


printf("5. Go back");

printf(">>> Enter your choice: ");


scanf("%d", &ch);
switch(ch)
{
case 1:
clrscr();
reverse();
getch();
str_operation();
break;
case 2:
clrscr();
merge_string();
getch();
str_operation();
break;
case 3:
clrscr();
palindrome();
getch();
str_operation();
break;
case 4:
clrscr();
string_length();
getch();
str_operation();
break;
case 5:
main_menu();
break;
default:
clrscr();
prompt();
getch();
str_operation();
}
}

/* Whether the string is palindrome or not. We hae to first find its reverse*/
void palindrome()
{
int i, j, n, flag=1;
char a[50], b[50], temp;
printf("Enter a string: ");
fflush(stdin);
gets(b);
n=length(b);
for(i=0; b[i]!='\0'; i++)
a[i]=b[i];
i=0;
j=n-1;
while(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
i++;
j--;
}
for(i=0; i<n; i++)
if(a[i]!=b[i])
flag=0;
if(flag==1)
printf("\n\nString is palindrome!");
else
printf("\n\nString is not palindrome!");
}

/*Length of string*/
void string_length()
{
int n;
char s[50];
printf("Enter a string: ");
fflush(stdin);
gets(s);
n=length(s);
printf("\nThe entered string is %d characters long!",n);
}

int length(char a[50])


{
int i=0;
while(a[i]!='\0')
i++;
return i;
}

/* We are reversing the entered string*/


void reverse()
{
char a[50], temp;
int i, j, n;
printf("Enter a string: ");
fflush(stdin);
gets(a);
n=length(a);
i=0;
j=n-1;
while(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
i++;
j--;
}
printf("\nReverse of the string is : %s", a);
}

/* We are making a user defined function that performs


the same operation as strcat in string.h header file */
void merge_string()
{
char a[50], b[50];
int n1, n2, i=0, j=0;
printf("Enter first string: ");
fflush(stdin);
gets(a);
printf("Enter second string: ");
fflush(stdin);
gets(b);
//n1=length(a);
//n2=length(b);
while(a[i]!='\0')
i++;
while(b[j]!='\0')
a[i++]=b[j++];
a[i]='\0';
printf("The resultant conconcatenated string is:\n%s",a);

/* This is the matrix menu */


void mtrx_menu()
{
int ch;
clrscr();
printf("1. Addition of two matrices");

printf("2. Subtraction of two matrices");

printf("3. Multipliconcatenateion of two matrices");


printf("4. Find inverse of a MATRIX");
printf("5. Go Back");
printf(">>> Enter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
clrscr();
mtrxadd();
getch();
mtrx_menu();
break;
case 2:
clrscr();
mtrxsub();
getch();
mtrx_menu();
break;
case 3:
clrscr();
mtrxmul();
getch();
mtrx_menu();
break;
case 4:
clrscr();
inverse();
getch();
mtrx_menu();
break;
case 5:
main_menu();
break;
default:
clrscr();
prompt();
getch();
mtrx_menu();
}
}

/* Subtracting two matrices */


void mtrxsub()
{
int a[10][10], b[10][10], d[10][10], r, c, i, j;
printf("Enter the no of rows: ");
scanf("%d", &r);
printf("Enter the no of columns: ");
scanf("%d", &c);
printf("\nEnter first MATRIX:\n");
for(i=0; i<r; i++)
for(j=0; j<c; j++)
scanf("%d", &a[i][j]);
printf("\nEnter second MATRIX:\n");
for(i=0; i<r; i++)
for(j=0; j<c; j++)
scanf("%d", &b[i][j]);
for(i=0; i<r; i++)
for(j=0; j<c; j++)
d[i][j]=a[i][j]-b[i][j];
printf("\n\nFirst MATRIX:\n");
for(i=0; i<r; i++)
{
printf("\n");
for(j=0; j<c; j++)
printf("%d ", a[i][j]);
}
printf("\n\nSecond MATRIX:\n");
for(i=0; i<r; i++)
{
printf("\n");
for(j=0; j<c; j++)
printf("%d ", b[i][j]);
}
printf("\n\nResultant MATRIX:\n");
for(i=0; i<r; i++)
{
printf("\n");
for(j=0; j<c; j++)
printf("%d ", d[i][j]);
}
}

/* MATRIX addition */
void mtrxadd()
{
int a[10][10], b[10][10], d[10][10], r, c, i, j;
printf("Enter the no of rows: ");
scanf("%d", &r);
printf("Enter the no of columns: ");
scanf("%d", &c);
printf("\nEnter first MATRIX:\n");
for(i=0; i<r; i++)
for(j=0; j<c; j++)
scanf("%d", &a[i][j]);
printf("\nEnter second MATRIX:\n");
for(i=0; i<r; i++)
for(j=0; j<c; j++)
scanf("%d", &b[i][j]);
for(i=0; i<r; i++)
for(j=0; j<c; j++)
d[i][j]=a[i][j]+b[i][j];
printf("\n\nFirst MATRIX:\n");
for(i=0; i<r; i++)
{
printf("\n");
for(j=0; j<c; j++)
printf("%d ", a[i][j]);
}
printf("\n\nSecond MATRIX:\n");
for(i=0; i<r; i++)
{
printf("\n");
for(j=0; j<c; j++)
printf("%d ", b[i][j]);
}
printf("\n\nResultant MATRIX:\n");
for(i=0; i<r; i++)
{
printf("\n");
for(j=0; j<c; j++)
printf("%d ", d[i][j]);
}
}

/* MATRIX multiplication */
void mtrxmul()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], product[10][10];
printf("Enter the number of rows and columns of first MATRIX\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of first MATRIX\n");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
scanf("%d", &first[c][d]);
printf("Enter the number of rows and columns of second MATRIX\n");
scanf("%d%d", &p, &q);
if ( n != p )
printf("Matrices with entered orders can't be multiplied with each
other.\n");
else
{
printf("Enter the elements of second MATRIX\n");
for ( c = 0 ; c < p ; c++ )
for ( d = 0 ; d < q ; d++ )
scanf("%d", &second[c][d]);
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
{
for( k = 0 ; k < p ; k++ )
{
sum = sum + first[c][k]*second[k][d];
}

product[c][d] = sum;
sum = 0;
}
}
printf("Product of entered matrices:-\n");
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
printf("%d\t", product[c][d]);
printf("\n");
}
}
}

/* We are finding inverse */


void inverse()
{
int a[3][3],i,j;
float determinant=0;
printf("Enter 9 elements of MATRIX: ");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nThe MATRIX is\n");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
}
for(i=0;i<3;i++)
determinant = determinant + (a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3] - a[1]
[(i+2)%3]*a[2][(i+1)%3]));
printf("\nInverse of MATRIX is: \n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) -
(a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
printf("\n");
}
}
/* This is the supreme main menu of the developer's program */
void main_menu()
{
int ch;
clrscr();
printf("1. Mixed Programs");
printf("2. String functions");
printf("3. MATRIX operations");
printf("4. Pattern creation");
printf("5. Exit");
printf(">>> Enter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
mix_programs();
break;
case 2:
str_operation();
break;
case 3:
mtrx_menu();
break;
case 4:
pattern_menu();
break;
case 5:
exit(0);
default:
clrscr();
prompt();
getch();
}
}
/* Invalid input error message*/
void prompt()
{ int df;
printf("Invalid input!");

printf("Enter 1 to go back to main menu");


scanf("%d", &df);
switch(df)
{
case 1:
clrscr();
main_menu();
default: "invalid input";
}
}

/* pattern menu - here all the pattern fucntions are called */


void pattern_menu()
{
int ch;
clrscr();

printf("1. Pyramid of @ symbols");

printf("2. Pyramid of numbers");

printf("3. Square of hash");

printf("4. Go back");

printf(">>> Enter your choice: ");


scanf("%d", &ch);
switch(ch)
{
case 1:
clrscr();
pattern1();
getch();
pattern_menu();
break;
case 2:
clrscr();
pattern2();
getch();
pattern_menu();
break;
case 3:
clrscr();
sq();
getch();
pattern_menu();
break;
case 4:
main_menu();
default:
clrscr();
prompt();
getch();
pattern_menu();
}
}
/* Sub function 1 of pattern_menu menu */
void pattern1()
{
int r;

printf("Enter no. of rows: ");


scanf("%d", &r);
clrscr();
p1(r);
}

// Printing pyramid 1 with 'n' no of rows


void p1(int n)
{
int i, j;
for(i=1; i<=n; i++)
{
printf("\n");
for(j=1; j<=i; j++)
printf("@ ");
}
}

/* Sub function 2 of pattern_menu menu */


void pattern2()
{
int r;
printf("Enter no. of rows: ");
scanf("%d", &r);
clrscr();
p2(r);
}

// Printing pyramid 2 with 'n' no of rows


void p2(int n)
{
int i, j;
for(i=1; i<=n; i++)
{
printf("\n");
for(j=n; j>=i; j--)
printf("%d ", j);
}
}

/* Sub function 3 of pattern_menu menu */


void sq()
{
int r;
printf("Enter no. of rows: ");
scanf("%d", &r);
clrscr();
square(r);
}

// Printing square with 'n' no of rows


void square(int n)
{
int i, j;
for(i=1; i<=n; i++)
{
printf("\n");
for(j=1; j<=n; j++)
printf("# ");
}
}