Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

Wednesday, 18 December 2013

C Program to Find the First Occurence of the any Character of String2 in String1

#include < stdio.h>
 
void main()
{
    char s1[50], s2[10];
    int i, flag = 0;
    char *ptr1, *ptr2;
 
    printf("\nenter the string1:" );
    scanf(" %[^\n]s" , s1);    
    printf("\nenter the string2:" );
    scanf(" %[^\n]s" , s2);
 
    /*COMPARING THE STRING1 CHARACTER BY CHARACTER WITH ALL CHARACTERS OF STRING1*/
    for (i = 0, ptr1 = s1;*ptr1 !=  '\0';ptr1++)
    {
        i++;
        for (ptr2 = s2; *ptr2 != '\0';ptr2++)
        {
            if (*ptr1  ==  *ptr2)
            {
                flag = 1;
                break;
            }
        }
        if (flag  ==  1)
            break;
    }
 
    if (flag  ==  1)
        printf("\nfirst occurance of character of string2 in string1 is at position:%d and character is %c" , i, *ptr2);
    else
        printf("\nnone of the characters of string1 match with mone of characters of string2" );
}

Output

  
enter the string1:C Programming Class
 
enter the string2:rnp
 
first occurance of character of string2  in  string1 is at position: 3  and character is p

For More Details Please Visit Ictjobs.info

C Program to Insert Character/Word in any Desired Location in a String

#include < stdio.h>
#include < string.h>
 
void main()
{
    int i, j, count = 0, pos, flag = 0;
    char s1[100], s2[10], s3[100];
    char *ptr1, *ptr2, *ptr3;
 
    printf("\nenter the String:" );
    scanf(" %[^\n]s" , s1);
    printf("\nenter the string to be inserted:" );
    scanf(" %[^\n]s" , s2);
    printf("\nenter the position you like to insert:" );
    scanf("%d" , &pos);
 
    ptr1 = s1;
    ptr3 = s3;
    /*COPYING THE GIVEN STRING TO NEW ARRAY AND INSERTING THE STRING IN NEW ARRAY*/
    for (i = 0, j = 0;*ptr1 != '\0'; ptr1++, i++, j++, ptr3++)
    {
        s3[j] = s1[i];
        if (*ptr1 == ' ' && flag != 1)
            ++count;
        if (flag != 1 && count == pos - 1)
        {
            flag = 1;
            for(ptr2 = s2;*ptr2 != '\0'; ptr2++)
            {
                s3[++j] = *ptr2;
                ptr3++;
            }
            s3[++j] = ' ';
            ptr3++;
        }
    }
    s3[j] = '\0';
    printf("\nthe string after modification is\n\n %s\n" , s3);
}

Output

 enter the string:Welcome to Sanfoundry's C Programming Class,  Welcome Again to C Class!
enter the word to insert:Sanfoundry' s
enter the position you like to insert: 3 
the string after modification is
 
Welcome to Sanfounsry's Sanfoundry' s C Programming Class,  Welcome Again to C Class ! 

For More Details Please Visit Ictjobs.info

C Program to Count the Total Number of Words in the Sentence using Command Line Argument

#include < stdio.h>
 
int main(int argc, char *argv[])
{
    int i = 0;
 
    /* If no sentence is given in the command line */
    if (argc == 1)
    {
        printf("\n No sentence given on command line" );
        return;
    }
    else
    {
        printf("\nThe words in the sentence are:" );
        /*
         * From argv[1] to argv[argc -1] calculate the number of arguments
         */
        for (i = 1;i < argc;i++)
        {
            printf("\n%s" , argv[i]);
         }
        printf("\n\nTotal number of words:" );
        printf(" %d" , argc-1);
    }
}

Output

 Welcome to C Class
 
The words  in  the sentence are:
Welcome
to
C
Class
 
Total number of words:  4 
 
 
 
No sentence given on  command  line

For More Details Please Visit Ictjobs.info

C Program to Replace all the Characters by Lowercase

#include < stdio.h>
#include < stdlib.h>
 
void main(int argc, char* argv[])
{
    FILE *fp1;
    int ch;
 
    if ((fp1 = fopen(argv[1], "r+" )) == NULL)
    {
        printf("\nfile cant be opened" );
        exit(0);
    }
    ch = fgetc(fp1);
    while (ch != EOF)
    {
        if (ch >= 65 && ch <= 90)
        {
            fseek(fp1, -1L, 1);
            fputc(ch + 32, fp1);
        }
        ch = fgetc(fp1);
    }
}

Output

 file4test
   cat  file4test
chandana chanikya
ravella

For More Details Please Visit Ictjobs.info

C Program to find Longer Repeating Sequence

#include < stdio.h>
#include < string.h>
 
void main()
{
    char s1[100], ar[10][20], ar1[10][20], new[10];
    int i, j = 0, k = 0, l, count = 0, flag = 0, n, temp, len[20];
 
    printf("\nenter the string:" );
    scanf(" %[^\n]s" , s1);
 
    /*COPYING GIVEN STRING TO 2D ARRAY*/
    for (i = 0;s1[i] != '\0';i++,j++)
    {
        if (s1[i] >= 33 && s1[i] <= 64)
            i++;
        if (s1[i] == ' ')
        {
            ar[k][j] = '\0';
            k++;
            i++;
            j = 0;
        }
        ar[k][j] = s1[i];
    }
    ar[k][j] = '\0';
    /*PLACING THE REPEATED WORDS AND LENGTHS INTO NEW ARRAY*/
    l = 0;
    for (i = 0;i <= k;i++)
    {
        for (j = i + 1;j <= k;j++)
        {
            if (strcmp(ar[i], ar[j]) == 0)
            {
                for (n = 0;n < l && l != 0; n++)
                {
                    if (strcmp(ar[i], ar1[k]) == 0)
                    {
                        flag = 1;
                        break;
                    }
                }
                if (flag != 1)
                {
                    strcpy(ar1[l], ar[i]);
                    len[l] = strlen(ar1[l]);
                    l++;
                }
                flag = 0;
                break;
            }
        }
    }
    printf("\n" );
    /*SORTING IS DONE BASED ON THEIR LENGTHS*/
    for (i = 0;i < l;i++)
    {
        for (j = i + 1;j < l;j++)
        {
            if (len[i] < len[j])
            {
                temp = len[i];
                strcpy(new, ar1[i]);
                len[i] = len[j];
                strcpy(ar1[i], ar1[j]);
                len[j] = temp;
                strcpy(ar1[j], new);
            }
        }
    }
    maxlen = len[0];
    for (i = 0;i < l;i++)
    {
        if (len[i] == maxlen)
            printf("\nthe longer repeating sequence of the given string is: %s" , ar1[i]);
    }
}

Output

 enter the string:Welcome to C Programming Class, Welcome Again to C Programming Class ! 
 
the longer repeating sequence of the given string is: Programming

For More Details Please Visit Ictjobs.info

C Program to Find the Sum of ASCII values of All Characters in a given String

#include < stdio.h>
#include < string.h>
 
void main()
{
    int sum = 0, i, len;
    char string1[100];
 
    printf("Enter the string : " );
    scanf("%[^\n]s" , string1);
        len = strlen(string1);
    for (i = 0; i < len; i++)
    {
        sum = sum + string1[i];
    }
    printf("\nSum of all characters : %d " ,sum);
}

Output

 Enter the string : Welcome to Sanfoundry's C Programming Class, Welcome Again to C Class !
 
Sum of all characters : 6307 

For More Details Please Visit Ictjobs.info

C Program to Print the Words Ending with Letter S

#include < stdio.h>
#include < string.h>
 
char str[100];
 
void main()
{
    int i, t, j, len;
 
    printf("Enter a string : " );
    scanf("%[^\n]s" , str);
 
    len = strlen(str);
 
    str[len] = ' ';
 
    for (t = 0, i = 0; i < strlen(str); i++)
    {
        if ((str[i] == ' ') && (str[i - 1] == 's'))
        {
            for (j = t; j < i; j++)
                printf("%c" , str[j]);
            t = i + 1;
            printf("\n" );
        }
        else
        {
            if (str[i] == ' ')
            {
                t = i + 1;
            }
        }
    }
}

Output

 Enter a string : Welcome to Sanfoundry's C Programming Class, Welcome Again to C Class !
Sanfoundry' s
Class

For More Details Please Visit Ictjobs.info

C Program to Concatenate two Strings Lexically

#include < stdio.h>
 
void sort(char *p);
 
void main()
{
    char string1[100], string2[100];
    int i, len, j;
 
    printf("\nEnter a string : " );
    scanf("%[^\n]s" , string1);
    printf("\nEnter another string to concat : " );
    scanf(" %[^\n]s" , string2);
    len = strlen(string1);
    string1[len] = ' ';
    for(i = 0, j = len + 1; i < strlen(string2); i++, j++)
        string1[j] = string2[i];
    string1[j]='\0';
    sort(string1);
}
 
/* Sorting to make concatenation lexical */
void sort(char *p)
{
    char temp[100];
    char a[100][100];
    int t1, i, j = 0, k = 0, l = strlen(p), x = 0, y = 0, z = 0, count, l1, l2;
 
    for (i = 0; i < l; i++)
    {
        if (p[i] != ' ') 
        {
            a[k][j++] = p[i];
        }
        else
        {
            a[k][j] = '\0';
            k++;
            j = 0;
        }
    }
 
    t1 = k;
    k = 0;
 
    for (i = 0; i < t1; i++)
    {
        for (j = i + 1; j <= t1; j++)
        {
            l1 = strlen(a[i]);
            l2 = strlen(a[j]);
            if (l1 > l2)
                count = l1;
            else
                count = l2;
            x = 0, y = 0;
            while ((x < count) || (y < count))
            {
                if (a[i][x] == a[j][y])
                {
                    x++;
                    y++;
                    continue;
                }
                else 
                    if (a[i][x] < a[j][y]) break;
                else 
                    if (a[i][x] > a[j][y])
                    {
                        for (z = 0; z < l2; z++)
                        {
                            temp[z] = a[j][z];
                            a[j][z] = '\0';
                        }
                        temp[z] = '\0';
 
                        for (z = 0; z < l1; z++)
                        {
                            a[j][z] = a[i][z];
                            a[i][z] = '\0';
                        }
                        a[j][z] = '\0';
 
                        for (z = 0; z < strlen(temp); z++)
                        {
                            a[i][z] = temp[z];
                        }
                        break;
                    }    
                }
            }    
        }
 
    for (i = 0; i < l; i++)
    p[i] = '\0';
    k = 0;
    j = 0;
    for (i = 0; i < l; i++)
    {
        if (a[k][j] != '\0')
        {
            p[i] = a[k][j++];
        }
        else
        {
            k++;
            j = 0;
            p[i] = ' ';
        }
    }
    puts(p);        
}

Output

  
Enter a string : hello this
 
Enter another string to concat : is Ictjobs.info
hello is Ictjobs.info this

For More Details Please Visit Ictjobs.info

C Program to List All Lines containing a given String

#include < stdio.h>
#include < stdlib.h>
#include < string.h>
 
int search(FILE *, char *);
 
void main(int argc, char * argv[])
{
    FILE *fp1;
    int p;
 
    fp1 = fopen(argv[1], "r+" );
    if (fp1 == NULL)
    {
        printf("cannot open the file " );
        exit(0);
    }
    search(fp1, argv[2]);
    fclose(fp1);
}
 
/* Searches the lines */
int search(FILE *fp, char * str)
{
    FILE *fp1;
    fp1 = fopen("fp1" ,"w" );
    char s[10],c;
    int len = strlen(str);
    int i = 0;
    int d;
    int seek = fseek(fp, 0, 0);
    c = fgetc(fp);
    while (c != EOF)
    {
        if (c == ' ' || c == '\n')
        {
            s[i] = '\0';
            i = 0;
            if (strcmp(s, str) == 0)
            {
                while (c = fgetc(fp) != '\n')
                {
                    fseek(fp, -2L, 1);
                    d = ftell(fp);
                }
                while ((c = fgetc(fp)) != '\n')
                {
                    fputc(c, fp1);
                }
            }
        }
        else
        {
            s[i] = c;
            i++;
        }
        c = fgetc(fp);
    }
    return 1;
}

Output

 example hi
hi hello everyone
again hi to the late comers

For More Details Please Visit Ictjobs.info

C Program to Reverse every Word of given String

#include < stdio.h>
#include < string.h>
 
void main()
{
    int i, j = 0, k = 0, x, len;
    char str[100], str1[10][20], temp;
 
    printf("enter the string :" );
    scanf("%[^\n]s" , str);
 
/* reads into 2d character array */
    for (i = 0;str[i] != '\0'; i++)
    {
        if (str[i] == ' ')
        {
            str1[k][j]='\0';
            k++;
            j=0;
        }
        else
        {
            str1[k][j]=str[i];
            j++;
        }
    }
    str1[k][j] = '\0';
 
/* reverses each word of a given string */
    for (i = 0;i <= k;i++)
    {
        len = strlen(str1[i]);
        for (j = 0, x = len - 1;j < x;j++,x--)
        {
            temp = str1[i][j];
            str1[i][j] = str1[i][x];
            str1[i][x] = temp;
        }
    }
    for (i = 0;i <= k;i++)
    {
        printf("%s " , str1[i]);
    }
}

Output

 enter the string :C Programming Class
C gnimmargorP ssalC

For More Details Please Visit Ictjobs.info

C Program to Display every possible Combination of two Words or Strings from the input Strings without Repeated Combinations

#include < stdio.h>
#include < string.h>
 
void main()
{
    int i, j = 0, k, k1 = 0, k2 = 0, row = 0;
    char temp[50];
    char str[100], str2[100], str1[5][20], str3[6][20], str4[60][40];
 
    printf("enter the string :" );
    scanf(" %[^\n]s" , &str);
    printf("enter string:" );
    scanf(" %[^\n]s" , &str2);
 
/* read strings into 2d character arrays */
    for (i = 0;str[i] != '\0'; i++)
    {
        if (str[i] == ' ')
        {
            str1[k1][j] = '\0';
            k1++;
            j = 0;
        }
        else
        {
            str1[k1][j] = str[i];
            j++;
        }
    }
    str1[k1][j] = '\0';
    j = 0;
    for (i = 0;str2[i] != '\0';i++)
    {
        if (str2[i] == ' ')
        {
            str3[k2][j] = '\0';
            k2++;
            j = 0;
        }
        else
        {
            str3[k2][j] = str2[i];
            j++;
        }
    }
    str3[k2][j] = '\0';
 
/* concatenates string1 words with string2 and stores in 2d array */    
    row = 0;
    for (i = 0;i <= k1;i++)
    {
        for (j = 0;j <= k2;j++)
        {
            strcpy(temp, str1[i]);
            strcat(temp, str3[j]);
            strcpy(str4[row], temp);
            row++;
        }
    }
    for (i = 0;i <= k2;i++)
    {
        for (j = 0;j <= k1;j++)
        {
            strcpy(temp, str3[i]);
            strcat(temp, str1[j]);
            strcpy(str4[row], temp);
            row++;
        }
    }
 
/* eliminates repeated combinations */
    for (i = 0;i < row;i++)
    {
        for (j = i + 1;j < row;j++)
        {
            if (strcmp(str4[i], str4[j]) == 0)
            {
                for (k = j;k <= row;k++)
                {
                    strcpy(str4[k], str4[k + 1]);
                }
                row--;
            }
        }
    }
 
/* displays the output */
    for (i = 0;i < row;i++)
    {
        printf("\n%s" , str4[i]);
    }
}

Output

 enter the string :welcome to Ictjobs.info's class
enter string:welcome to c programming class
 
welcomewelcome
welcometo
welcomec
welcomeprogramming
welcomeclass
towelcome
toto
toc
toprogramming
toclass
Ictjobs.info' swelcome
Ictjobs.info'sto
Ictjobs.info' sc
Ictjobs.info'sprogramming
Ictjobs.info' sclass
classwelcome
classto
classc
classprogramming
classclass
welcomeIctjobs.info's
toIctjobs.info' s
cwelcome
cto
cIctjobs.info's
cclass
programmingwelcome
programmingto
programmingIctjobs.info' s
programmingclass
classIctjobs.info's 

For More Details Please Visit Ictjobs.info

C Program to Count the Number of Vowels &#038; Consonants in a Sentence

#include < stdio.h>
 
void main()
{
    char sentence[80];
    int i, vowels = 0, consonants = 0, special = 0;
 
    printf("Enter a sentence \n" );
    gets(sentence);
    for (i = 0; sentence[i] != '\0'; i++)
    {
        if ((sentence[i] == 'a' || sentence[i] == 'e' || sentence[i] ==
        'i' || sentence[i] == 'o' || sentence[i] == 'u') ||
        (sentence[i] == 'A' || sentence[i] == 'E' || sentence[i] ==
        'I' || sentence[i] == 'O' || sentence[i] == 'U'))
        {
            vowels = vowels + 1;
        }
        else
        {
            consonants = consonants + 1;
        }
        if (sentence[i] =='t' ||sentence[i] =='\0' || sentence[i] ==' ')
        {
            special = special + 1;
        }
    }
    consonants = consonants - special;
    printf("No. of vowels in %s = %d\n" , sentence, vowels);
    printf("No. of consonants in %s = %d\n" , sentence, consonants);
}

Output

 Enter a sentence
welcome to Ictjobs.info
No. of vowels  in  welcome to Ictjobs.info =  7 
No. of consonants  in  welcome to Ictjobs.info =  12 

For More Details Please Visit Ictjobs.info

C Program to Replace Lowercase Characters by Uppercase &#038; Vice-Versa

#include < stdio.h>
#include < ctype.h>
 
void main()
{
    char sentence[100];
    int count, ch, i;
 
    printf("Enter a sentence \n" );
    for (i = 0; (sentence[i] = getchar()) != '\n'; i++)
    {
        ;
    }
    sentence[i] = '\0';
    /*  shows the number of chars accepted in a sentence */
    count = i;
    printf("The given sentence is   : %s" , sentence);
    printf("\n Case changed sentence is: " );
    for (i = 0; i < count; i++)
    {
        ch = islower(sentence[i])? toupper(sentence[i]) :
tolower(sentence[i]);
        putchar(ch);
    }
}

Output

 Enter a sentence
wELCOME tO sANFOUNDRY
The given sentence is   : wELCOME tO sANFOUNDRY
Case changed sentence is: Welcome To Sanfoundry

For More Details Please Visit Ictjobs.info

C Program to read two Strings &#038; Concatenate the Strings

#include < stdio.h>
#include < string.h>
 
void main()
{
    char string1[20], string2[20];
    int i, j, pos;
 
    /*  Initialize the string to NULL values */
    memset(string1, 0, 20);
    memset(string2, 0, 20);
 
    printf("Enter the first string : " );
    scanf("%s" , string1);
    printf("Enter the second string: " );
    scanf("%s" , string2);
    printf("First string  = %s\n" , string1);
    printf("Second string = %s\n" , string2);
 
    /*  Concate the second string to the end of the first string */
    for (i = 0; string1[i] != '\0'; i++)
    {
        /*  null statement: simply traversing the string1 */
        ;
    }
    pos = i;
    for (j = 0; string2[j] != '\0'; i++)
    {
        string1[i] = string2[j++];
    }
    /*  set the last character of string1 to NULL */
    string1[i] = '\0';
    printf("Concatenated string = %s\n" , string1);
}

Output

 Enter the first string : San
Enter the second string: foundry
First string  = San
Second string = foundry
Concatenated string = Sanfoundry

For More Details Please Visit Ictjobs.info

C Program to Check if a given String is Palindrome

#include < stdio.h>
#include < string.h>
 
void main()
{
    char string[25], reverse_string[25] = {'\0'};
    int  i, length = 0, flag = 0;
 
    fflush(stdin);
    printf("Enter a string \n" );
    gets(string);
    /*  keep going through each character of the string till its end */
    for (i = 0; string[i] != '\0'; i++)
    {
        length++;
    }
    for (i = length - 1; i >= 0; i--)
    {
       reverse_string[length - i - 1] = string[i];
    }
    /*
     * Compare the input string and its reverse. If both are equal
     * then the input string is palindrome.
     */
    for (i = 0; i < length; i++)
    {
        if (reverse_string[i] == string[i])
            flag = 1;
        else
            flag = 0;
    }
    if (flag == 1)
        printf("%s is a palindrome \n" , string);
    else
        printf("%s is not a palindrome \n" , string);
}

Output

 Enter a string
Ictjobs.info
Ictjobs.info is not a palindrome
 
   
Enter a string
malayalam
malayalam is a palindrome

For More Details Please Visit Ictjobs.info

Sunday, 15 December 2013

C Program to Interchange any two Rows &#038; Columns in the given Matrix

#include < stdio.h>

void main()
{
static int array1[10][10], array2[10][10];
int i, j, m, n, a, b, c, p, q, r;

printf("Enter the order of the matrix \n" );
scanf("%d %d" , &m, &n);
printf("Enter the co-efficents of the matrix \n" );
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d," , &array1[i][j]);
array2[i][j] = array1[i][j];
}
}
printf("Enter the numbers of two rows to be exchanged \n" );
scanf("%d %d" , &a, &b);
for (i = 0; i < m; ++i)
{
/* first row has index is 0 */
c = array1[a - 1][i];
array1[a - 1][i] = array1[b - 1][i];
array1[b - 1][i] = c;
}
printf("Enter the numbers of two columns to be exchanged \n" );
scanf("%d %d" , &p, &q);
printf("The given matrix is \n" );
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
printf(" %d" , array2[i][j]);
printf("\n" );
}
for (i = 0; i < n; ++i)
{
/* first column index is 0 */
r = array2[i][p - 1];
array2[i][p - 1] = array2[i][q - 1];
array2[i][q - 1] = r;
}
printf("The matix after interchanging the two rows(in the original matrix) \n" );
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
printf(" %d" , array1[i][j]);
}
printf("\n" );
}
printf("The matix after interchanging the two columns(in the original matrix) \n" );
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
printf(" %d" , array2[i][j]);
printf("\n" );
}
}

Output

 Enter the order of the matrix
2 2
Enter the co-efficents of the matrix
34 70
45 90
Enter the numbers of two rows to be exchanged
1 2
Enter the numbers of two columns to be exchanged
1 2
The given matrix is
34 70
45 90
The matix after interchanging the two rows ( in the original matrix )
45 90
34 70
The matix after interchanging the two columns ( in the original matrix )
70 34
90 45

For More Details Please Visit Ictjobs.info

C Program to Check if 2 Matrices are Equal

#include < stdio.h>
#include < stdlib.h>

void main()
{
int a[10][10], b[10][10];
int i, j, row1, column1, row2, column2, flag = 1;

printf("Enter the order of the matrix A \n" );
scanf("%d %d" , &row1, &column1);
printf("Enter the order of the matrix B \n" );
scanf("%d %d" , &row2, &column2);
printf("Enter the elements of matrix A \n" );
for (i = 0; i < row1; i++)
{
for (j = 0; j < column1; j++)
{
scanf("%d" , &a[i][j]);
}
}
printf("Enter the elements of matrix B \n" );
for (i = 0; i < row2; i++)
{
for (j = 0; j < column2; j++)
{
scanf("%d" , &b[i][j]);
}
}
printf("MATRIX A is \n" );
for (i = 0; i < row1; i++)
{
for (j = 0; j < column1; j++)
{
printf("%3d" , a[i][j]);
}
printf("\n" );
}
printf("MATRIX B is \n" );
for (i = 0; i < row2; i++)
{
for (j = 0; j < column2; j++)
{
printf("%3d" , b[i][j]);
}
printf("\n" );
}
/* Comparing two matrices for equality */
if (row1 == row2 && column1 == column2)
{
printf("Matrices can be compared \n" );
for (i = 0; i < row1; i++)
{
for (j = 0; j < column2; j++)
{
if (a[i][j] != b[i][j])
{
flag = 0;
break;
}
}
}
}
else
{
printf(" Cannot be compared\n" );
exit(1);
}
if (flag == 1)
printf("Two matrices are equal \n" );
else
printf("But, two matrices are not equal \n" );
}

Output

 Enter the order of the matrix A
2 2
Enter the order of the matrix B
2 2
Enter the elements of matrix A
23 56
45 80
Enter the elements of matrix B
50 26
39 78
MATRIX A is
23 56
45 80
MATRIX B is
50 26
39 78
Matrices can be compared
But,two matrices are not equal


Enter the order of the matrix A
2 2
Enter the order of the matrix B
2 2
Enter the elements of matrix A
10 50
15 30
Enter the elements of matrix B
10 50
15 30
MATRIX A is
10 50
15 30
MATRIX B is
10 50
15 30
Matrices can be compared
Two matrices are equal

For More Details Please Visit Ictjobs.info

C Program to Check if a given Matrix is an Identity Matrix

#include < stdio.h>

void main()
{
int a[10][10];
int i, j, row, column, flag = 1;

printf("Enter the order of the matrix A \n" );
scanf("%d %d" , &row, &column);
printf("Enter the elements of matrix A \n" );
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
{
scanf("%d" , &a[i][j]);
}
}
printf("MATRIX A is \n" );
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
{
printf("%3d" , a[i][j]);
}
printf("\n" );
}
/* Check for unit (or identity) matrix */
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
{
if (a[i][j] != 1 && a[j][i] != 0)
{
flag = 0;
break;
}
}
}
if (flag == 1 )
printf("It is identity matrix \n" );
else
printf("It is not a identity matrix \n" );
}

Output

 Enter the order of the matrix A
3 3
Enter the elements of matrix A
1 2 3
5 1 8
6 4 1
MATRIX A is
1 2 3
5 1 8
6 4 1
It is not a identity matrix


Enter the order of the matrix A
3 3
Enter the elements of matrix A
1 0 0
0 1 0
0 0 1
MATRIX A is
1 0 0
0 1 0
0 0 1
It is identity matrix

For More Details Please Visit Ictjobs.info

C Program to Calculate the Addition or Subtraction &#038; Trace of 2 Matrices

#include < stdio.h>
void trace(int arr[][10], int m, int n);

void main()
{
int array1[10][10], array2[10][10], arraysum[10][10],
arraydiff[10][10];
int i, j, m, n, option;

printf("Enter the order of the matrix array1 and array2 \n" );
scanf("%d %d" , &m, &n);
printf("Enter the elements of matrix array1 \n" );
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d" , &array1[i][j]);
}
}
printf("MATRIX array1 is \n" );
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%3d" , array1[i][j]);
}
printf("\n" );
}
printf("Enter the elements of matrix array2 \n" );
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d" , &array2[i][j]);
}
}
printf("MATRIX array2 is \n" );
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%3d" , array2[i][j]);
}
printf("\n" );
}
printf("Enter your option: 1 for Addition and 2 for Subtraction \n" );
scanf("%d" , &option);
switch (option)
{
case 1:
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
arraysum[i][j] = array1[i][j] + array2[i][j];
}
}
printf("Sum matrix is \n" );
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%3d" , arraysum[i][j]) ;
}
printf("\n" );
}
trace (arraysum, m, n);
break;
case 2:
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
arraydiff[i][j] = array1[i][j] - array2[i][j];
}
}
printf("Difference matrix is \n" );
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%3d" , arraydiff[i][j]) ;
}
printf("\n" );
}
trace (arraydiff, m, n);
break;
}

}
/* Function to find the trace of a given matrix and print it */
void trace (int arr[][10], int m, int n)
{
int i, j, trace = 0;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
if (i == j)
{
trace = trace + arr[i][j];
}
}
}
printf("Trace of the resultant matrix is = %d\n" , trace);
}

Output

 Enter the order of the matrix array1 and array2
3 3
Enter the elements of matrix array1
2 3 4
7 8 9
5 6 8
MATRIX array1 is
2 3 4
7 8 9
5 6 8
Enter the elements of matrix array2
3 3 3
3 4 6
8 4 7
MATRIX array2 is
3 3 3
3 4 6
8 4 7
Enter your option: 1 for Addition and 2 for Subtraction
1
Sum matrix is
5 6 7
10 12 15
13 10 15
Trace of the resultant matrix is = 32


Enter the order of the matrix array1 and array2
3 3
Enter the elements of matrix array1
10 20 30
15 18 20
12 14 16
MATRIX array1 is
10 20 30
15 18 20
12 14 16
Enter the elements of matrix array2
1 5 9
10 15 14
9 12 13
MATRIX array2 is
1 5 9
10 15 14
9 12 13
Enter your option: 1 for Addition and 2 for Subtraction
2
Difference matrix is
9 15 21
5 3 6
3 2 3
Trace of the resultant matrix is = 15

For More Details Please Visit Ictjobs.info

C Program to Find the Transpose of a given Matrix

#include < stdio.h>

void main()
{
static int array[10][10];
int i, j, m, n;

printf("Enter the order of the matrix \n" );
scanf("%d %d" , &m, &n);
printf("Enter the coefiicients of the matrix\n" );
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d" , &array[i][j]);
}
}
printf("The given matrix is \n" );
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
printf(" %d" , array[i][j]);
}
printf("\n" );
}
printf("Transpose of matrix is \n" );
for (j = 0; j < n; ++j)
{
for (i = 0; i < m; ++i)
{
printf(" %d" , array[i][j]);
}
printf("\n" );
}
}

Output

 Enter the order of the matrix
3 3
Enter the coefiicients of the matrix
3 7 9
2 7 5
6 3 4
The given matrix is
3 7 9
2 7 5
6 3 4
Transpose of matrix is
3 2 6
7 7 3
9 5 4

For More Details Please Visit Ictjobs.info