20_10

Write a C code to add and subtract two one dimensional array of size 5 using function addarray() and subtractarray().

 Source code:

/*Write a C code to add and subtract two one dimensional array of size 5 using function addarray() and subtractarray().*/

#include<stdio.h>

#define size 5

void readarraya(int a[],int );

void readarrayb(int b[],int );

void addarray(int a[],int b[],int add[], int );

void subarray(int a[], int b[], int sub[], int );

void printarrayadd(int add[], int );

void printarraysub(int sub[], int );

void readarraya(int a[],int n)

{

    int i;

    printf("Enter the value of the array 'a':");

    for(i=0; i < n; i++)

        scanf("%d",&a[i]);

}

void readarrayb(int b[],int n)

{

    int i;

    printf("Enter the value of the array 'b':");

    for(i=0; i < n; i++)

      scanf("%d",&b[i]);

}

 void addarray(int a[],int b[],int add[],int n)

{

    int i;

    for(i=0; i< n; i++)

        add[i]=a[i]+b[i];

}

void subarray(int a[],int b[],int sub[],int n)

{

    int i;

    for(i=0; i< n; i++)

        sub[i]=a[i]-b[i];

void printarrayadd (int add[],int n)

    int i;

    for(i=0; i < n; i++)

        printf("%d\n",add[i]);

}           

void printarraysub (int sub[],int n)

{

  int i;

    for(i=0; i < n; i++)

        printf("%d\n",sub[i]);

}

int main()

{

    int i,n;

    int a[size],b[size],add[size],sub[size];

       

    /* Read Array a */

    readarraya(a,size);


    /* Read Array b */   

    readarrayb(b,size);


    /* add two arrays*/

    addarray(a,b,add,size);


    /* subtract two arrays*/

    subarray(a,b,sub,size);


    printf("After adding\n");

    printarrayadd(add,size);


    printf("After subtracting\n");

    printarraysub(sub,size);


    return 0;

}

Output:

C code to add and subtract two one dimensional array using function


Write a C code to compare two strings.

 Source code:

/*Write a C code to compare two strings e.g. str1 and str2 and check whether strings are equal and str1 is greater than str2 or less, without using standard string library. */

#include <stdio.h>

#include <string.h>

int main(){

    char str1[20],str2[20];

    int c=0,d=0;

printf("Enter 1st string:");

    //Read String 1

    scanf("%s", &str1);

printf("Enter 2nd string:");

    //Read String 2

    scanf("%s", &str2);

int i=0;

while (str1[i] != '\0')

{

c++;

i++;

}

i=0;

while (str2[i] != '\0')

{

d++;

i++;

}

if (c==d)

{

        printf ("Strings are equal");

}

else if (c>d)

{

        printf ("String 1 is greater than String 2");

}

else

{

        printf ("String 1 is less than String 2");

}

return 0;

}

Output:
C code to compare two strings e.g. str1 and str2 and check whether strings are equal and str1 is greater than str2 or less, without using standard string library


Write a C code to concatenate two strings.

Source code:

 //Write a C code to concatenate two strings

#include <stdio.h>

#include <string.h>

int main(){

    char str1[20],str2[2];

    printf("Enter 1st string:");

    //Read String 1

    scanf("%s", str1);

printf("Enter 2nd string:");

    //Read String 2

    scanf("%s", str2);

int i,j,temp;

i=j=0;

while (str1[i] != '\0')

        i++;

    for (j = 0; str2[j] != '\0'; i++, j++)

      str1[i] = str2[j];

    str1[i] = '\0';

printf("The concatenated output:%s",str1);

return 0;

}

Output:

C code to concatenate two strings


Write a C code to reverse a string without using any string library functions.

 Source code:

/*Write a C code to reverse a string without using any string library functions , e.g. '12345' should be '54321'. */

#include <stdio.h>

#include <string.h>

int main(){

    char str[20];

printf("Enter a string:");

    //Read String

    scanf("%s", str);

int temp;

int i=0,j=0,length;


while (str[j] != '\0')

        j++;

        j--;

    while (i<j) {

        temp=str[i];

        str[i]=str[j];

        str[j]=temp;

        i++;

        j--;

}

printf("The reverse string:%s",str);

return 0;

}

Output:

C code to reverse a string without using any string library functions


Write a C code to check whether a given string is palindrome or not.

Source code:

 /*A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward, e.g. madam 123321 etc. Write a C code to check whether a given string is palindrome or not. */

#include <stdio.h>

#include <string.h>

int main(){

    char string[20];

    int flag = 0;

    printf("Enter a string:");

    //Read a String

    scanf("%s", string);

int i, length; 

length = strlen(string);

  for(i=0; i < length ; i++){

        if(string[i] != string[length-i-1]){

            flag = 1;

            break;

    }

}

  if (flag) {

        printf("The given string %s is not palindrome", string);

    }

    else {

        printf("The given string %s is palindrome", string);

    }

    return 0;

}

Output:

C code to check whether a given string is palindrome or not


Write a C code to find the difference between two matrices of any order and print the result in matrix format.

 Source code:

/*Write a C code to find the difference between two matrices of any order and  print the result in matrix format.*/

#include <stdio.h>

int main()

{

        int row, col, i, j, first[10][10], second[10][10], difference[10][10]; 

printf("Enter the number of row & column:");

        scanf("%d %d", &row, &col);  

printf("Enter the 1st matrix:\n");

    for (i = 0; i < row; i++)

    for (j = 0 ; j < col; j++)

scanf("%d", &first[i][j]);

  printf("Enter the 2nd matrix:\n");

for (i = 0; i < row; i++)

for (j = 0; j < col; j++)

        scanf("%d", &second[i][j]);

/* Find the difference of entered matrices and store it in difference[10][10] */

 

    for (i = 0; i < row; i++) {

    for (j = 0; j < col; j++) {

        difference[i][j] = first[i][j] - second[i][j];

     

    }

/*printing the difference array */

printf("Subtraction of two matrix is:\n");

    for (i = 0; i < row; i++)

{

    for (j = 0 ; j < col; j++){

    printf("%d\t",difference[i][j]);    

}

printf("\n");

    }

return 0;

}

Output:

C code to find the difference between two matrices of any order and  print the result in matrix format


Write a program in C to convert a decimal number to binary using recursion.

 Source code: //Write a program in C to convert a decimal number to binary using recursion. #include<stdio.h> long convertB_to_D(int d...