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

C Program to Compute the Product of Two Matrices

#include < stdio.h>
#define MAXROWS 10
#define MAXCOLS 10

void readMatrix(int arr[][MAXCOLS], int m, int n);
void printMatrix(int arr[][MAXCOLS], int m, int n);
void productMatrix(int array1[][MAXCOLS], int array2[][MAXCOLS],
int array3[][MAXCOLS], int m, int n);

void main()
{
int array1[MAXROWS][MAXCOLS], array2[MAXROWS][MAXCOLS],
array3[MAXROWS][MAXCOLS];
int m, n;

printf("Enter the value of m and n \n" );
scanf("%d %d" , &m, &n);
printf("Enter Matrix array1 \n" );
readMatrix(array1, m, n);
printf("Matrix array1 \n" );
printMatrix(array1, m, n);
printf("Enter Matrix array2 \n" );
readMatrix(array2, m, n);
printf("Matrix B \n" );
printMatrix(array2, m, n);
productMatrix(array1, array2, array3, m, n);
printf("The product matrix is \n" );
printMatrix(array3, m, n);
}
/* Input Matrix array1 */
void readMatrix(int arr[][MAXCOLS], int m, int n)
{
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d" , &arr[i][j]);
}
}
}
void printMatrix(int arr[][MAXCOLS], int m, int n)
{
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%3d" , arr[i][j]);
}
printf("\n" );
}
}
/* Multiplication of matrices */
void productMatrix(int array1[][MAXCOLS], int array2[][MAXCOLS],
int array3[][MAXCOLS], int m, int n)
{
int i, j, k;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
array3[i][j] = 0;
for (k = 0; k < n; k++)
{
array3[i][j] = array3[i][j] + array1[i][k] *
array2[k][j];
}
}
}
}

Output

 Enter the value of m and n
3 3
Enter matrix array1
4 5 6
1 2 3
3 7 8
Matrix array1
4 5 6
1 2 3
3 7 8
Enter matrix array2
5 6 9
8 5 3
2 9 1
Matrix array2
5 6 9
8 5 3
2 9 1
The product matrix is
72103 57
27 43 18
87125 56

For More Details Please Visit Ictjobs.info

C Program to Find the Number of Non Repeated Elements in an Array

#include < stdio.h>
int main()
{
int array[50];
int *ptr;
int i, j, k, size, n;

printf("\n Enter size of the array: " );
scanf("%d" , &n);
printf("\n Enter %d elements of an array: " , n);
for (i = 0; i < n; i++)
scanf("%d" , &array[i]);
size = n;
ptr = array;
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
if (i == j)
{
continue;
}
else if (*(ptr + i) == *(ptr + j))
{
k = j;
size--;
while (k < size)
{
*(ptr + k) = *(ptr + k + 1);
k++;
}
j = 0;
}
}
}
printf("\n The array after removing duplicates is: " );
for (i = 0; i < size; i++)
{
printf(" %d" , array[i]);
}
return 0;
}

Output

  
Enter size of the array: 6

Enter 6 elements of an array: 12
10
4
10
12
56

The array after removing duplicates is: 12 10 4 56

For More Details Please Visit Ictjobs.info

C Program to identify missing Numbers in a given Array

#include < stdio.h>

void main()
{
int n, i, j, c, t, b;

printf("Enter size of array : " );
scanf("%d" , &n);
int array[n - 1]; /* array size-1 */
printf("Enter elements into array : \n" );
for (i = 0; i < n - 1; i++)
scanf("%d" , &array[i]);
b = array[0];
for (i = 1; i < n - 1; i++)
b = b ^ array[i];
for (i = 2, c = 1; i <= n; i++)
c = c ^ i;
c = c ^ b;
printf("Missing element is : %d \n" , c);
}

Output

 Enter  size  of array :  6 
Enter elements into array :
1
2
3
5
6
Missing element is : 4

For More Details Please Visit Ictjobs.info

C Program to Find the Summation of Node values at Row or Level

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

struct btnode
{
int value;
struct btnode *r,*l;
}*root = NULL, *temp = NULL;

void create();
void insert();
void add(struct btnode *t);
void computesum(struct btnode *t);
void display();

int count = 0, sum[100] = {0}, max = 0;

void main()
{
int ch;

printf("\n OPERATIONS ---" );
printf("\n 1] Insert an element into tree" );
printf("\n 2] Display the sum of the elements at the same level" );
printf("\n 3] Exit " );
while (1)
{
printf("\nEnter your choice : " );
scanf("%d" , &ch);
switch (ch)
{
case 1:
insert();
break;
case 2:
count = 0;
max = 0;
computesum(root);
display();
break;
case 3:
exit(0);
default :
printf("Wrong choice, Please enter correct choice " );
break;
}
}
}

/* To create a new node with the data from the user */
void create()
{
int data;

printf("Enter the data of node : " );
scanf("%d" , &data);
temp = (struct btnode* ) malloc(1*(sizeof(struct btnode)));
temp->value = data;
temp->l = temp->r = NULL;
}

/* To check for root node and then create it */
void insert()
{
create();

if (root == NULL)
root = temp;
else
add(root);
}

/* Search for the appropriate position to insert the new node */
void add(struct btnode *t)
{
if ((temp->value > t->value) && (t->r != NULL)) /* value more than root node value insert at right */
add(t->r);
else if ((temp->value > t->value) && (t->r == NULL))
t->r = temp;
else if ((temp->value < t->value) && (t->l != NULL)) /* value less than root node value insert at left */
add(t->l);
else if ((temp->value < t->value) && (t->l==NULL))
t->l = temp;
}

/* Function to find the sum of nodes at same distance */
void computesum(struct btnode *t)
{
if (root == NULL)
{
printf("Tree is empty " );
return;
}
if (t->l != NULL)
{
count++;
computesum(t->l);
}
sum[count] = sum[count] + t->value; /* addition of elelment by row wise */
if (max < count)
max = count;
if (t->r != NULL)
{
count++;
computesum(t->r);
}
count--;
}

/* To display the sum of the nodes at the same distance */
void display()
{
int i;

printf("Sum of nodes : \n Level \t Sum " );
for (i = 0; i <= max; i++)
printf("\n %d \t: %d " , i, sum[i]);
}

Output

  
OPERATIONS ---
1 ] Insert an element into tree
2 ] Display the sum of the elements at the same level
3 ] Exit
Enter your choice : 1
Enter the data of node : 40

Enter your choice : 1
Enter the data of node : 20

Enter your choice : 1
Enter the data of node : 60

Enter your choice : 1
Enter the data of node : 10

Enter your choice : 1
Enter the data of node : 30

Enter your choice : 1
Enter the data of node : 80

Enter your choice : 1
Enter the data of node : 90

Enter your choice : 2
Sum of nodes :
Level Sum
0 : 40
1 : 80
2 : 120
3 : 90
Enter your choice : 3

40
/ \
/ \
20 60
/ \ \
10 30 80
\
90

. /

OPERATIONS ---
1 ] Insert an element into tree
2 ] Display the sum of the elements at the same level
3 ] Exit
Enter your choice : 1
Enter the data of node : 50

Enter your choice : 1
Enter the data of node : 30

Enter your choice : 1
Enter the data of node : 20

Enter your choice : 1
Enter the data of node : 40

Enter your choice : 1
Enter the data of node : 35

Enter your choice : 1
Enter the data of node : 100

Enter your choice : 1
Enter the data of node : 70

Enter your choice : 1
Enter the data of node : 120

Enter your choice : 1
Enter the data of node : 140

Enter your choice : 2
Sum of nodes :
Level Sum
0 : 50
1 : 130
2 : 250
3 : 175
Enter your choice : 3



50
/ \
/ \
30 100
/ \ / \
20 40 70 120
/ \
35 140

For More Details Please Visit Ictjobs.info

C Program to Find Union &#038; Intersection of 2 Arrays

#include < stdio.h>
#define SIZE 5

void get_value(int arr[]);
void print_value(int arr[], int n);
void function_sort(int arr[]);
int find_intersection(int array1[], int array2[], int intersection_array[]);
int find_union(int array1[], int array2[], int union_array[]);

void main()
{
int array1[SIZE], array2[SIZE], intersection_array[SIZE], union_array[SIZE*2];
int num_elements;

//input elements of Array1
printf("\n Enter the elements of Array 1: n" );
get_value(array1);
printf("\n\n Elements of Array 1: " );
print_value(array1, SIZE);

//Sort array 1
function_sort(array1);
printf("nnSorted elements of Array 1: " );
print_value(array1, SIZE);

//input elements of Array2
printf("nnEnter the elements of Array 2: n" );
get_value(array2);
printf("\n\n Elements of Array 2: " );
print_value(array2, SIZE);

//Sort array 2
function_sort(array2);
printf("\n\nSorted elements of Array 2: " );
print_value(array2, SIZE);

//Find Intersection
num_elements = find_intersection(array1, array2, intersection_array);
printf("\n\n Intersection is: " );
print_value(intersection_array, num_elements);

//Find Union
num_elements = find_union(array1, array2, union_array);
printf("\n\n Union is: " );
print_value(union_array, num_elements);
}

void get_value(int arr[])
{
int i, j;
for (i = 0; i < SIZE; i++)
{
j = i + 1;
printf("\n Enter element %d: " , j);
scanf("%d" , &arr[i]);
}
}

void print_value(int arr[], int n)
{
int i;
printf("{ " );
for (i = 0; i < n; i++)
{
printf("%d " , arr[i]);
}
printf("}" );
}

void function_sort(int arr[])
{
int i, j, temp, swapping;

for (i = 1; i < size; i++)
{
swapping = 0;
for (j = 0; j < size-i; j++)
{
if (arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapping = 1;
}
}
if (swapping == 0)
{
break;
}
}
}

int find_intersection(int array1[], int array2[], int intersection_array[])
{
int i = 0, j = 0, k = 0;
while ((i < size) && (j < size))
{
if (array1[i] < array2[j])
{
i++;
}
else if (array1[i] > array2[j])
{
j++;
}
else
{
intersection_array[k] = array1[i];
i++;
j++;
k++;
}
}
return(k);
}

int find_union(int array1[], int array2[], int union_array[])
{
int i = 0, j = 0, k = 0;
while ((i < SIZE) && (j < SIZE))
{
if (array1[i] < array2[j])
{
union_array[k] = array1[i];
i++;
k++;
}
else if (array1[i] > array2[j])
{
union_array[k] = array2[j];
j++;
k++;
}
else
{
union_array[k] = array1[i];
i++;
j++;
k++;
}
}
if (i == SIZE)
{
while (j < SIZE)
{
union_array[k] = array2[j];
j++;
k++;
}
}
else
{
while (i < SIZE)
{
union_array[k] = array1[i];
i++;
k++;
}
}
return(k);
}

Output

  
Enter the elements of Array 1 :

Enter element 1 : 12

Enter element 2 : 34

Enter element 3 : 23

Enter element 4 : 56

Enter element 5 : 45


Elements of Array 1 : { 12 34 23 56 45 }

Sorted elements of Array 1 : { 12 23 34 45 56 }

Enter the elements of Array 2 :

Enter element 1 : 34

Enter element 2 : 56

Enter element 3 : 12

Enter element 4 : 78

Enter element 5 : 66


Elements of Array 2 : { 34 56 12 78 66 }

Sorted elements of Array 2 : { 12 34 56 66 78 }

Intersection is: { 12 34 56 }

Union is: { 12 23 34 45 56 66 78 }

For More Details Please Visit Ictjobs.info

C Program to Find Ceiling &#038; Floor of X given a Sorted Array &#038; a value X

#include < stdio.h>

/* Function to get index of ceiling of x in arr[low..high] */
int ceilSearch(int arr[], int low, int high, int x)
{
int i;

/* If x is smaller than or equal to first element,then return the first element */
if (x <= arr[low])
return low;

/* Otherwise, linearly search for ceil value */
for (i = low; i < high; i++)
{
if (arr[i] == x)
return i;

/* if x lies between arr[i] and arr[i+1] including arr[i+1], then return arr[i+1] */
if (arr[i] < x && arr[i + 1] >= x)
return i + 1;
}

/* If we reach here then x is greater than the last element of the array, return -1 in this case */
return -1;
}

int main()
{
int arr[] = {1, 2, 8, 10, 10, 12, 19};
int n = sizeof(arr)/sizeof(arr[0]);
int x = 3;
int index = ceilSearch(arr, 0, n-1, x);
if (index == -1)
printf("Ceiling of %d doesn't exist in array " , x);
else
printf("ceiling of %d is %d" , x, arr[index]);
getchar();
return 0;
}

Output

 ceiling of  3  is  8 

For More Details Please Visit Ictjobs.info

C Program to Increment every Element of the Array by one &#038; Print Incremented Array

#include < stdio.h>

void incrementArray(int[]);

void main()
{
int i;
int array[4] = {10, 20, 30, 40};

incrementArray(array);
for (i = 0; i < 4; i++)
printf("%d\t" , array[i]); // Prints 2, 3, 4, 5
}

void incrementArray(int arr[])
{
int i;

for (i = 0; i < 4; i++)
arr[i]++; // this alters values in array in main()
}

Output

  11      21      31 

For More Details Please Visit Ictjobs.info

C Program to Find 2 Elements in the Array such that Difference between them is Largest

#include < stdio.h>

int maximum_difference(int array[], int arr_size)
{
int max_diff = array[1] - array[0];
int i, j;
for (i = 0; i < arr_size; i++)
{
for (j = i + 1; j < arr_size; j++)
{
if (array[j] - array[i] > max_diff)
max_diff = array[j] - array[i];
}
}
return max_diff;
}

int main()
{
int array[] = {10, 15, 90, 200, 110};
printf("Maximum difference is %d" , maximum_difference(array, 5));
getchar();
return 0;
}

Output

 Maximum difference is  190 

For More Details Please Visit Ictjobs.info

C Program to Find the Sum of Contiguous Subarray within a 1 &#8211; D Array of Numbers which has the Largest Sum

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

int maxSubArraySum(int a[], int size, int *begin, int *end)
{
int max_so_far = 0, max_end = 0;
int i, current_index = 0;

for (i = 0; i < size; i++)
{
max_end = max_end + a[i];
if (max_end <= 0)
{
max_end = 0;
current_index = i + 1;
}
else if (max_so_far < max_end)
{
max_so_far = max_end;
*begin = current_index;
*end = i;
}
}
return max_so_far;
}

int main()
{
int arr[] = {10, -2, 15, 9, -8, 12, 20, -5};
int start = 0, end = 0;
int size = sizeof(arr) / sizeof(arr[0]);

printf(" The max sum is %d" , maxSubArraySum(arr, size, &start, &end));
printf(" The begin and End are %d & %d" , start, end);
getchar();
return 0;
}

Output

  The max  sum  is  56  The begin and End are  0   &   6 

For More Details Please Visit Ictjobs.info

C Program to Find the Median of the Elements after Merging these 2 Sorted Arrays with Same Size

#include < stdio.h>

int getMedian(int array1[], int array2[], int n)
{
int i = 0; /* Current index of i/p array array1[] */
int j = 0; /* Current index of i/p array array2[] */
int count;
int m1 = -1, m2 = -1;

for (count = 0; count <= n; count++)
{
if (i == n)
{
m1 = m2;
m2 = array2[0];
break;
}
else if (j == n)
{
m1 = m2;
m2 = array1[0];
break;
}
if (array1[i] < array2[j])
{
m1 = m2; /* Store the prev median */
m2 = array1[i];
i++;
}
else
{
m1 = m2; /* Store the prev median */
m2 = array2[j];
j++;
}
}
return (m1 + m2)/2;
}

int main()
{
int array1[] = {20, 25, 35, 30, 38};
int array2[] = {22, 53, 65, 72, 45};

int n1 = sizeof(array1) / sizeof(array1[0]);
int n2 = sizeof(array2) / sizeof(array2[0]);
if (n1 == n2)
printf("Median is %d" , getMedian(array1, array2, n1));
else
printf("not possible to findout" );
getchar();
return 0;
}

Output

 Median is  34 

For More Details Please Visit Ictjobs.info

C Program to Input an Array, Store the Squares of these Elements in an Array &#038; Print it

#include < stdio.h>
#define MAX_ROWS 3
#define MAX_COLS 4

void print_square(int [ ] );

void main (void)
{
int i;
int num [MAX_ROWS][MAX_COLS] = { {10, 20, 30, 40}, {50, 60, 70, 80}, {90, 100, 110, 120} };

for (i = 0; i < MAX_ROWS; i++)
print_square(num[i]);
}
void print_square(int x[ ])
{
int j;
for (j = 0; j < MAX_COLS; j++)
printf ("%d\t" , x[j] * x[j]);
printf("\n" );
}

Output

  100      400      900      1600 
2500 3600 4900 6400
8100 10000 12100 14400

For More Details Please Visit Ictjobs.info

C Program to Print the Alternate Elements in an Array

#include < stdio.h>

void main()
{
int array[10];
int i, j, temp;
printf("enter the element of an array \n" );
for (i = 0; i < 10; i++)
scanf("%d" , &array[i]);
printf("Alternate elements of a given array \n" );
for (i = 0; i < 10; i += 2)
printf( "%d\n" , array[i]) ;
}

Output

 enter the element of an array
12
23
45
57
68
73
84
97
120
125
Alternate elements of a given array
12
45
68
84
120

For More Details Please Visit Ictjobs.info

C Program to Find the Odd Element given an Array with only two Different Element

#include < stdio.h>

void printodd(int array[], int size)
{
int xor2 = array[0]; /* Will hold XOR of two odd occurring elements */
int set;
int i;
int n = size - 2;
int x = 0, y = 0;

/* The xor will basically be xor of two odd occurring elements */
for (i = 1; i < size; i++)
xor2 = xor2 ^ array[i];
/* Get one set rightmost bit in the xor2. */
set = xor2 & ~(xor2 - 1);
/* Now divide elements in two sets: */
for (i = 0; i < size; i++)
{
/* XOR of first set is finally going to hold one odd occurring number x */
if (array[i] & set)
x = x ^ array[i];
/* XOR of second set is finally going to hold the other odd occurring number y */
else
y = y ^ array[i];
}
printf("\n The ODD elements are %d & %d " , x, y);
}

int main()
{
int array[] = {10, 3, 2, 10, 2, 8, 8, 7};
int arr_size = sizeof(array) / sizeof(array[0]);
printodd(array, arr_size);
getchar();
return 0;
}

Output

  
The ODD elements are 7 & 3

For More Details Please Visit Ictjobs.info

C Program to Check Array bounds while Inputing Elements into the Array

#include < stdio.h>

int main(void)
{
int array[5], b, c;
for (b = 0; b < 10 && (scanf("%d" , &c)); b++)
array[b] = c;
for (b = 0; b < 15; b++)
printf("%d " , array[b]);
return 0;
}

Output

  12 
23
56
12
14
19
23
12 23 56 12 14 23 6 134513824 0 -1081194040 11672807 1 -1081193996 -1081193988 -1216161720

For More Details Please Visit Ictjobs.info

C Program to Merge the Elements of 2 Sorted Array

#include < stdio.h>

void main()
{
int array1[50], array2[50], array3[100], m, n, i, j, k = 0;

printf("\n Enter size of array Array 1: " );
scanf("%d" , &m);
printf("\n Enter sorted elements of array 1: \n" );
for (i = 0; i < m; i++)
{
scanf("%d" , &array1[i]);
}
printf("\n Enter size of array 2: " );
scanf("%d" , &n);
printf("\n Enter sorted elements of array 2: \n" );
for (i = 0; i < n; i++)
{
scanf("%d" , &array2[i]);
}
i = 0;
j = 0;
while (i < m && j < n)
{
if (array1[i] < array2[j])
{
array3[k] = array1[i];
i++;
}
else
{
array3[k] = array2[j];
j++;
}
k++;
}
if (i >= m)
{
while (j < n)
{
array3[k] = array2[j];
j++;
k++;
}
}
if (j >= n)
{
while (i < m)
{
array3[k] = array1[i];
i++;
k++;
}
}
printf("\n After merging: \n" );
for (i = 0; i < m + n; i++)
{
printf("\n%d" , array3[i]);
}
}

Output

  
Enter size of array Array 1 : 4

Enter sorted elements of array 1 :
12
18
40
60

Enter size of array 2 : 4

Enter sorted elements of array 2 :
47
56
89
90

After merging:

12
18
40
47
56
60
89
90

For More Details Please Visit Ictjobs.info

C Program to Input a String &#038; Store their Ascii Values in an Integer Array &#038; Print the Array

#include < stdio.h>

void main()
{
char string[20];
int n, count = 0;
printf("Enter the no of characters present in an array \n " );
scanf("%d" , &n);
printf(" Enter the string of %d characters \n" , n);
scanf("%s" , string);
while (count < n)
{
printf(" %c = %d\n" , string[count], string[count] );
++ count ;
}
}

Output

 Enter the no of characters present  in  an array
10
Enter the string of 10 characters
Ictjobs.info
s = 115
a = 97
n = 110
f = 102
o = 111
u = 117
n = 110
d = 100
r = 114
y = 121

For More Details Please Visit Ictjobs.info

C Program to Print all the Repeated Numbers with Frequency in an Array

#include < stdio.h>
#include

void duplicate(int array[], int num)
{
int *count = (int *)calloc(sizeof(int), (num - 2));
int i;

printf("duplicate elements present in the given array are " );
for (i = 0; i < num; i++)
{
if (count[array[i]] == 1)
printf(" %d " , array[i]);
else
count[array[i]]++;
}
}

int main()
{
int array[] = {5, 10, 10, 2, 1, 4, 2};
int array_freq = sizeof(array) / sizeof(array[0]);
duplicate(array, array_freq);
getchar();
return 0;
}

Output

  duplicate elements present  in  the given array are   10    2 

For More Details Please Visit Ictjobs.info

C Program to Find the Largest Number in an Array

#include < stdio.h>

int main()
{
int array[50], size, i, largest;
printf("\n Enter the size of the array: " );
scanf("%d" , &size);
printf("\n Enter %d elements of the array: " , size);
for (i = 0; i < size; i++)
scanf("%d" , &array[i]);
largest = array[0];
for (i = 1; i < size; i++)
{
if (largest < array[i])
largest = array[i];
}
printf("\n largest element present in the given array is : %d" , largest);
return 0;
}

Output

  
Enter the size of the array: 5

Enter 5 elements of the array: 12
56
34
78
100

largest element present in the given array is : 100

For More Details Please Visit Ictjobs.info

C Program to Find the Number of Elements in an Array

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

int main()
{
int array[] = {15, 50, 34, 20, 10, 79, 100};
int n;

n = sizeof(array);
printf("Size of the given array is %d\n" , n/sizeof(int));
return 0;
}

Output

 Size of the given array is  7 

For More Details Please Visit Ictjobs.info

C Program to Implement two Stacks using a Single Array &#038; Check for Overflow &#038; Underflow

#include < stdio.h>
#include < stdlib.h>
#define max 10

int top1, top2, array[max];
void push(void)
{
int x, ch;
if (top1 == top2 - 1)
{
printf("stack overflow \n" );
return;
}
printf("enter a no \n" );
scanf("%d" , &x);
printf("\n press 1 to push the element in 1st stack or press 2 for stack 2:" );
scanf("%d" , &ch);
if (ch == 1)
array[++top1] = x;
else
array[--top2] = x;
printf("%d element is successfully pushed \n" , x);
return;
}
void pop(void)
{
int y, ch;
printf("\n press 1 to pop the element from 1st stack or press 2 for from stack 2" );
scanf("%d" , &ch);
if (ch == 1)
{
if (top1 == -1) // Condition for checking If Stack 1 is Empty
{
printf("stack underflow \n" );
return;
}
y = array[top1];
array[top1--]=0;
}
else
{
if (top2 == max) // Condition for checking If Stack 2 is Empty
{
printf("stack underflow \n" );
return;
}
y = array[top2];
array[top2++] = 0;
}
printf("%d element is successfully poped from stack \n" , y);
return;
}
void display(void)
{
int i;
if (top1 == -1)
{
printf("stack 1 is empty \n" );
}
else
{
printf("elements of Stack 1 are : \n" );
for (i = 0; i = top2; i--)
{
printf("%d\n" , array[i]);
}
return ;
}

void main(void)
{
int ch;
top1 = -1, top2 = max;

do
{
printf("1:push\n 2:pop\n 3:display\n 4:exit\n choice:" );
scanf("%d" , &ch);
switch (ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:printf("quits from program \n" );
break;
default:printf("wrong choice \n" );
break;
}
} while (ch != 4);
}

Output

  1 :push
2 :pop
3 :display
4 :exit
choice: 1
enter a no
13

press 1 to push the element in 1st stack or press 2 for stack 2 : 1
13 element is successfully pushed
1 :push
2 :pop
3 :display
4 :exit
choice: 1
enter a no
30

press 1 to push the element in 1st stack or press 2 for stack 2 : 1
30 element is successfully pushed
1 :push
2 :pop
3 :display
4 :exit
choice: 1
enter a no
37

press 1 to push the element in 1st stack or press 2 for stack 2 : 2
37 element is successfully pushed
1 :push
2 :pop
3 :display
4 :exit
choice: 1
enter a no
65

press 1 to push the element in 1st stack or press 2 for stack 2 : 2
65 element is successfully pushed
1 :push
2 :pop
3 :display
4 :exit
choice: 2

press 1 to pop the element from 1st stack or press 2 for from stack 21
30element is successfully poped from stack
1 :push
2 :pop
3 :display
4 :exit
choice: 2

press 1 to pop the element from 1st stack or press 2 for from stack 22
65element is successfully poped from stack
1 :push
2 :pop
3 :display
4 :exit
choice: 3
elements of Stack 1 are :
13
Elements of stack 2 are
37
1 :push
2 :pop
3 :display
4 :exit
choice: 1
enter a no
10

press 1 to push the element in 1st stack or press 2 for stack 2 : 1
10 element is successfully pushed
1 :push
2 :pop
3 :display
4 :exit
choice: 1
enter a no
98

press 1 to push the element in 1st stack or press 2 for stack 2 : 2
98 element is successfully pushed
1 :push
2 :pop
3 :display
4 :exit
choice: 3
elements of Stack 1 are :
13
10
Elements of stack 2 are
37
98
1 :push
2 :pop
3 :display
4 :exit
choice: 4
quits from program

For More Details Please Visit Ictjobs.info

C Program to Generate Pascal Triangle 1 D Array

#include < stdio.h>

void main()
{
int array[30], temp[30], i, j, k, l, num; //using 2 arrays

printf("Enter the number of lines to be printed: " );
scanf("%d" , &num);
temp[0] = 1;
array[0] = 1;
for (j = 0; j < num; j++)
printf(" " );
printf(" 1\n" );
for (i = 1; i < num; i++)
{
for (j = 0; j < i; j++)
printf(" " );
for (k = 1; k < num; k++)
{
array[k] = temp[k - 1] + temp[k];
}
array[i] = 1;
for (l = 0; l <= i; l++)
{
printf("%3d" , array[l]);
temp[l] = array[l];
}
printf("\n" );
}
}

Output

 Enter the number of lines to be printed:  4 
1
1 1
1 2 1
1 3 3 1

For More Details Please Visit Ictjobs.info

C Program to Print the Number of Odd &#038; Even Numbers in an Array

#include < stdio.h>

void main()
{
int array[100], i, num;

printf("Enter the size of an array \n" );
scanf("%d" , &num);
printf("Enter the elements of the array \n" );
for (i = 0; i < num; i++)
{
scanf("%d" , &array[i]);
}
printf("Even numbers in the array are - " );
for (i = 0; i < num; i++)
{
if (array[i] % 2 == 0)
{
printf("%d \t" , array[i]);
}
}
printf("\n Odd numbers in the array are -" );
for (i = 0; i < num; i++)
{
if (array[i] % 2 != 0)
{
printf("%d \t" , array[i]);
}
}
}

Output

 Enter the  size  of an array
6
Enter the elements of the array
12
19
45
69
98
23
Even numbers in the array are - 12 98
Odd numbers in the array are - 19 45 69 23

For More Details Please Visit Ictjobs.info

C Program to Find the Biggest Number in an Array of Numbers using Recursion

#include < stdio.h>

int large(int[], int, int);

int main()
{
int size;
int largest;
int list[20];
int i;

printf("Enter size of the list:" );
scanf("%d" , &size);
printf("Printing the list:\n" );
for (i = 0; i < size ; i++)
{
list[i] = rand() % size;
printf("%d\t" , list[i]);
}
if (size == 0)
{
printf("Empty list\n" );
}
else
{
largest = list[0];
largest = large(list, size - 1, largest);
printf("\nThe largest number in the list is: %d\n" , largest);
}
}
int large(int list[], int size, int largest)
{
if (size == 1)
return largest;

if (size > -1)
{
if (list[size] > largest)
{
largest = list[size];
}
return(largest = large(list, size - 1, largest));
}
else
{
return largest;
}
}

Output

 Enter  size  of the list: 8 
Printing the list:
7 6 1 3 1 7 2 4
The largest number in the list is: 7

For More Details Please Visit Ictjobs.info