Sunday, May 8, 2011

To delete n Characters from a given position in a given string

/* Write a C program that uses functions to perform the following operations:

To delete n Characters from a given position in a given string.
*/

#include <stdio.h>

#include <conio.h>
#include <string.h>

void delchar(char *x,int a, int b);

void main()

{
char string[10];
int n,pos,p;
clrscr();

puts(“Enter the string”);

gets(string);
printf(“Enter the position from where to delete”);
scanf(“%d”,&pos);
printf(“Enter the number of characters to be deleted”);
scanf(“%d”,&n);
delchar(string, n,pos);
getch();
}

// Function to delete n characters

void delchar(char *x,int a, int b)
{
if ((a+b-1) <= strlen(x))
{
strcpy(&x[b-1],&x[a+b-1]);
puts(x);
}
}


Multiplication of Two Matrices

//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 Two Matrices

//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

/* 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);

}

WAP To find the GCD (greatest common divisor) of two given integers

/* 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

/* 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();

}

Write a C program to find the sum of individual digits of a positive integer

#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();

}

Write C program to implement Simpson method

#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;

}

write c program to find the roots of a quadratic equation

#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<process.h>

void main()

{

float a,b,c,x1,x2,disc;

clrscr();

printf("Enter the co-efficients\n");

scanf("%f%f%f",&a,&b,&c);

disc=b*b-4*a*c;/*to find discriminant*/

if(disc>0)/*distinct roots*/

{

x1=(-b+sqrt(disc))/(2*a);

x2=(-b-sqrt(disc))/(2*a);

printf("The roots are distinct\n");

exit(0);

}

if(disc==0)/*Equal roots*/

{

x1=x2=-b/(2*a);

printf("The roots are equal\n");

printf("x1=%f\nx2=%f\n",x1,x2);

exit(0);

}

x1=-b/(2*a);/*complex roots*/

x2=sqrt(fabs(disc))/(2*a);

printf("The roots are complex\n");

printf("The first root=%f+i%f\n",x1,x2);

printf("The second root=%f-i%f\n",x1,x2);

getch();

}

write a program to find the factorial of a number

#include<stdio.h>

int main(){

int i=1,f=1,num;

printf("\nEnter a number:");

scanf("%d",&num);

while(i<=num){

f=f*i;

i++;

}

printf("\nFactorial of %d is:%d",num,f);

return 0;

}

write a c program to find prime numbers

main()
{
int i,j=2,ch=0;
clrscr();
printf("\nENTER ANY NUMBER");
scanf("%d",&i);
while(j<=i/2)
{
if(i%j==0)
{
printf("%d IS NOT PRIME",i);
ch=1;
break;
}
else
{
j++;
}
}
if(ch==0)
{
printf("%d IS PRIME",i);
}
}

Write a C program to reverse the words in a sentence in place.

#include

void rev(char *l, char *r);

int main(int argc, char *argv[])
{
char buf[] = "the world will go on forever";
char *end, *x, *y;

// Reverse the whole sentence first..
for(end=buf; *end; end++);
rev(buf,end-1);

// Now swap each word within sentence...
x = buf-1;
y = buf;

while(x++ < end)
{
if(*x == '' || *x == ' ')
{
rev(y,x-1);
y = x+1;
}
}

// Now print the final string....
printf("%s\n",buf);

return(0);
}

// Function to reverse a string in place...
void rev(char *l,char *r)
{
char t;
while(l{
t = *l;
*l++ = *r;
*r-- = t;
}
}

Reverse words in a String C Program

#include
void reverse(char *begin, char *end);
void reverseWords(char *s)
{
char *word_begin = s;
char *temp = s;
while( *temp )
{
temp++;
if (*temp == '\0')
{
reverse(word_begin, temp-1);
}
else if(*temp == ' ')
{
reverse(word_begin, temp-1);
word_begin = temp+1;
}
}

reverse(s, temp-1);
}

void reverse(char *begin, char *end)
{
char temp;
while (begin < end)
{
temp = *begin;
*begin++ = *end;
*end-- = temp;
}
}

int main( )
{
char s[] = "i like this program very much";
char *temp = s;
reverseWords(s);
printf("%s", s);
getchar();
return 0;
}

Write a C program to print all permutations of a given string

# 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;
}