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

}