/* Write a C program that uses functions to perform the following operations:
#include <stdio.h>
void delchar(char *x,int a, int b);
void main()
puts(“Enter the string”);
// Function to delete n characters
/* Write a C program that uses functions to perform the following operations:
#include <stdio.h>
void delchar(char *x,int a, int b);
void main()
puts(“Enter the string”);
// Function to delete n characters
//Multiplication of Matrix
#include <stdio.h>
#include <conio.h>
int m1,n1,m2,n2,i,j,k,z[10][10]={0};
void value_sub(int a,int b,int arr[][10] )
{
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
printf(“Mat[%d%d] = “,i+1,j+1);
scanf(“%d”,&arr[i][j]);
fflush(stdin);
}
printf(“”);
}
}
void mat_mul(int a,int b,int arr[][10],int brr[][10])
{
int k=0;
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
for(k=0;k<a;k++)
z[i][j]+=arr[i][k]*brr[k][j];
printf(“%d\t”,z[i][j]);
}
printf(“\n\n”);
}
}
int main()
{
int A[10][10]={0},B[10][10]={0};
printf(“Enter the column and row of first matrix(m x n)\n”);
scanf(“%d%d”,&m1,&n1);
printf(“Enter the column and row of second matrix(m x n)\n”);
scanf(“%d%d”,&m2,&n2);
printf(“\n\n”);
if (n1==m2)
{
value_sub(m1,n1,A);
printf(“\n\n”);
value_sub(m2,n2,B);
printf(“\n\n”);
mat_mul(m1,n2,A,B);
}
else
printf(“Matrix multiplication cannot be done”);
getch();
}
//Addition of Matrix
#include <stdio.h>
#include <conio.h>
int m1,n1,m2,n2,i,j,k,z[10][10]={0};
void value_sub(int a,int b,int arr[][10] )
{
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
printf(“Mat[%d%d] = “,i+1,j+1);
scanf(“%d”,&arr[i][j]);
fflush(stdin);
}
printf(“”);
}
}
void mat_mul(int a,int b,int arr[][10],int brr[][10])
{
int k=0;
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
z[i][j]+=arr[i][j]+brr[i][j];
printf(“%d\t”,z[i][j]);
}
printf(“\n\n”);
}
}
int main()
{
int A[10][10]={0},B[10][10]={0};
printf(“Enter the column and row of first matrix(m x n)\n”);
scanf(“%d%d”,&m1,&n1);
printf(“Enter the column and row of second matrix(m x n)\n”);
scanf(“%d%d”,&m2,&n2);
printf(“\n\n”);
if (n1==m1||n2==m2)
{
value_sub(m1,n1,A);
printf(“\n\n”);
value_sub(m2,n2,B);
printf(“\n\n”);
mat_mul(m1,n2,A,B);
}
else
printf(“Addition of Matrix cannot be done”);
getch();
}
/* Write a C program to find both the largest and smallest number in a list of integers*/
main( )
{
float largest(float a[ ], int n);
float value[4] = {2.5,-4.75,1.2,3.67};
printf(“%f\n”, largest(value,4));
}
float largest(float a[], int n)
{
int i;
float max;
max = a[0];
for(i = 1; i < n; i++)
if(max < a[i])
max = a[i];
return(max);
}
/* Write C programs that use both recursive and non-recursive functions
To find the GCD (greatest common divisor) of two given integers.*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
unsigned int GcdRecursive(unsigned m, unsigned n);
unsigned int GcdNonRecursive(unsigned p,unsigned q);
int main(void)
{
int a,b,iGcd;
clrscr();
printf(“Enter the two numbers whose GCD is to be found: “);
scanf(“%d%d”,&a,&b);
printf(“GCD of %d and %d Using Recursive Function is %d\n”,a,b,GcdRecursive(a,b));
printf(“GCD of %d and %d Using Non-Recursive Function is %d\n”,a,b,GcdNonRecursive(a,b));
getch();
}
/* Recursive Function*/
unsigned int GcdRecursive(unsigned m, unsigned n)
{
if(n>m)
return GcdRecursive(n,m);
if(n==0)
return m;
else
return GcdRecursive(n,m%n);
}
/* Non-Recursive Function*/
unsigned int GcdNonRecursive(unsigned p,unsigned q)
{
unsigned remainder;
remainder = p-(p/q*q);
if(remainder==0)
return q;
else
GcdRecursive(q,remainder);
}
/* Write a C program to generate all the prime numbers between 1 and n, where n is a value supplied by the user. */
#include <stdio.h>
void main()
{
int no,counter,counter1,check;
clrscr();
printf(“<———————–PRIME NO. SERIES————————>”);
printf(“\n\n\n\t\t\tINPUT THE VALUE OF N: “);
scanf(“%d”,&no);
printf(“\n\nTHE PRIME NO. SERIES B/W 1 TO %d : \n\n”,no);
for(counter = 1; counter <= no; counter++)
{
check = 0;
//THIS LOOP WILL CHECK A NO TO BE PRIME NO. OR NOT.
for(counter1 = counter-1; counter1 > 1 ; counter1–)
if(counter%counter1 == 0)
{
check++; // INCREMENT CHECK IF NO. IS NOT A PRIME NO.
break;
}
if(check == 0)
printf(“%d\t”,counter);
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int num, k=1, sum=0;
clrscr();
printf(“Enter the number whose digits are to be added:”);
scanf(“%d”,&num);
while(num!=0)
{
k=num%10;
sum=sum+k;
k=num/10;
num=k;
}
printf(“Sum of the digits:%d”,sum);
getch();
}
#include<stdio.h>
#include<conio.h>
#include<math.h>
char postfix[80];
float stack[80];
char stack1[80];
int top=-1,top1=-1;
float eval(char postfix[], float x1);
void infix_postfix(char infix[]);
main()
{
float x0, xn, h, s,e1,e2, e3;
char exp[80], arr[80];
int i,n,l=0;
clrscr();
printf(“\nEnter an expression: “);
gets(exp);
puts(“Enter x0, xn and number of sub-intervals: “);
scanf(“%f%f%d”, &x0, &xn, &n);
h=(xn-x0)/n;
if(exp[0]==’l'&& exp[1]==’o'&& exp[2]==’g')
{
l=strlen(exp);
for(i=0;i<l-3; i++)
arr[0]=exp[i+3];
arr[i]=”;
infix_postfix(arr);
e1=eval(postfix,x0);
e2=eval(postfix,xn);
e3=4*eval(postfix, x0+h);
s=log(e1)+log(e2)+log(e3);
for (i=3;i<=n-1;i+=2)
s+=4*eval(postfix,x0+i*h)+2*eval(postfix, x0+(i-1)*h);
}
else
{
infix_postfix(exp);
s=eval(postfix,x0)+eval(postfix,xn)+4*eval(postfix, x0+h);
for (i=3;i<=n-1;i+=2)
s+=4*eval(postfix,x0+i*h)+2*eval(postfix, x0+(i-1)*h);
}
printf(“The value of integral is %6.3f\n”,(h/3)*s);
return(0);
}
/*Inserting the operands in a stack. */
void push(float item)
{
if(top==99)
{
printf(“\n\tThe stack is full”);
getch();
exit(0);
}
else
{
top++;
stack[top]=item;
}
return;
}
/*Removing the operands from a stack. */
float pop()
{
float item;
if(top==-1)
{
printf(“\n\tThe stack is empty\n\t”);
getch();
}
item=stack[top];
top–;
return (item);
}
void push1(char item)
{
if(top1==79)
{
printf(“\n\tThe stack is full”);
getch();
exit(0);
}
else
{
top1++;
stack1[top1]=item;
}
return;
}
/*Removing the operands from a stack. */
char pop1()
{
char item;
if(top1==-1)
{
printf(“\n\tThe stack1 is empty\n\t”);
getch();
}
item=stack1[top1];
top1–;
return (item);
}
/*Converting an infix expression to a postfix expression. */
void infix_postfix(char infix[])
{
int i=0,j=0,k;
char ch;
char token;
for(i=0;i<79;i++)
postfix[i]=’ ‘;
push1(‘?’);
i=0;
token=infix[i];
while(token!=”)
{
if(isalnum(token))
{
postfix[j]=token;
j++;
}
else if(token==’(‘)
{
push1(‘(‘);
}
else if(token==’)')
{
while(stack1[top1]!=’(‘)
{
ch=pop1();
postfix[j]=ch;
j++;
}
ch=pop1();
}
else
{
while(ISPriority(stack1[top1])>=ICP(token))
{
ch=pop1();
postfix[j]=ch;
j++;
}
push1(token);
}
i++;
token=infix[i];
}
while(top1!=0)
{
ch=pop1();
postfix[j]=ch;
j++;
}
postfix[j]=”;
}
/*Determining the priority of elements that are placed inside the stack. */
int ISPriority(char token)
{
switch(token)
{
case ‘(‘:return (0);
case ‘)’:return (9);
case ‘+’:return (7);
case ‘-’:return (7);
case ‘*’:return (8);
case ‘/’:return (8);
case ‘?’:return (0);
default: printf(“Invalid expression”);
}
return 0;
}
/*Determining the priority of elements that are approaching towards the stack. */
int ICP(char token)
{
switch(token)
{
case ‘(‘:return (10);
case ‘)’:return (9);
case ‘+’:return (7);
case ‘-’:return (7);
case ‘*’:return (8);
case ‘/’:return (8);
case ”:return (0);
default: printf(“Invalid expression”);
}
return 0;
}
/*Calculating the result of expression, which is converted in postfix notation. */
float eval(char p[], float x1)
{
float t1,t2,k,r;
int i=0,l;
l=strlen(p);
while(i<l)
{
if(p[i]==’x')
push(x1);
else
if(isdigit(p[i]))
{
k=p[i]-’0′;
push(k);
}
else
{
t1=pop();
t2=pop();
switch(p[i])
{
case ‘+’:k=t2+t1;
break;
case ‘-’:k=t2-t1;
break;
case ‘*’:k=t2*t1;
break;
case ‘/’:k=t2/t1;
break;
default: printf(“\n\tInvalid expression”);
}
push(k);
}
i++;
}
if(top>0)
{
printf(“You have entered the operands more than the operators”);
exit(0);
}
else
{
r=pop();
return (r);
}
return 0;
}
# include
# include
/* Function to swap values at two pointers */
void
swap (
char
*x,
char
*y)
{
char
temp;
temp = *x;
*x = *y;
*y = temp;
}
/* Function to print permutations of string
This function takes three parameters:
1. String
2. Starting index of the string
3. Ending index of the string. */
void
permute(
char
*a,
int
i,
int
n)
{
int
j;
if
(i == n)
printf
(
"%s\n"
, a);
else
{
for
(j = i; j <= n; j++)
{
swap((a+i), (a+j));
permute(a, i+1, n);
swap((a+i), (a+j));
//backtrack
}
}
}
/* Driver program to test above functions */
int
main()
{
char
a[] =
"ABC"
;
permute(a, 0, 2);
getchar
();
return
0;
}