Saturday 5 October 2013

Getting IP Address using C

#include<stdlib.h>
 main()
{
   system("C:\\Windows\\System32\\ipconfig");
}

Print Arguments passed using Command Line

#include<stdio.h>
main(int args,char *argv[])
{
    int i=0;
    for(i=0;i<args;i++)
         printf("\n%s",argv[i]);
}

Matrix Subtraction using C

#include<stdio.h>
#include<conio.h>
main()
{
int i,j,a[10][10],b[10][10],c[10][10],m1,n1,m2,n2;
printf("\nEnter the number of Rows of Mat1 : ");
scanf ("%d",&m1);

printf("\nEnter the number of Columns of Mat1 : ");
scanf ("%d",&n1);

for( i = 0 ; i < m1 ; i++ )
    for( j = 0 ; j < n1 ; j++ )
    {
    printf("\nEnter the Element a[%d][%d] : ",i,j);
    scanf("%d",&a[i][j]);
    }

// ------------------------------------------

printf("\nEnter the number of Rows of Mat2 : ");
scanf ("%d",&m2);

printf("\nEnter the number of Columns of Mat2 : ");
scanf ("%d",&n2);

if ( m1 != m2 || n1 != n2 )
 {
 printf("\nOrder of two matrices is not same ");
 exit(0);
 }

for( i = 0 ; i < m2 ; i++ )
    for( j = 0 ; j < n2 ; j++ )
    {
    printf("\nEnter the Element b[%d][%d] : ",i,j);
    scanf("%d",&b[i][j]);
    }

for( i = 0 ; i < m1 ; i++ )
    for( j = 0 ; j < n1 ; j++ )
    {
    c[i][j] = a[i][j] - b[i][j] ;
    }

printf("\n Matrix after Subtraction : \n");

for( i = 0 ; i < m1 ; i++ )
 {
    for( j = 0 ; j < n1 ; j++ )
    {
    printf("%d\t",c[i][j]);
    }
  printf("\n");
 }
}

Adding all Elements of a matrix using C

#include<stdio.h>
#include<conio.h>
main()
{
int i,j,a[10][10],sum,m,n;
printf("\nEnter the number of Rows : ");
scanf ("%d",&m);

printf("\nEnter the number of Columns : ");
scanf ("%d",&n);

for(i=0;i<m;i++)
       for(j=0;j<n;j++ )
       {
       printf("\nEnter the Element a[%d][%d] : ", i , j);
       scanf("%d",&a[i][j]);
       }

sum = 0;

for(i=0;i<m;i++ )
       for(j=0;j<n;j++ )
       {
       sum = sum + a[i][j];
       }
printf("\nThe Addition of All Elements in the Matrix : %d\n",sum);
}

Addition of Diagonal Elements of a matrix using C

#include<stdio.h>
main()
{
int i,j,a[10][10],sum,m,n;

printf("\nEnter the number of Rows : ");
scanf ("%d",&m);

printf("\nEnter the number of Columns : ");
scanf ("%d",&n);

for(i=0;i<m;i++ )
       for(j=0;j<n;j++)
       {
       printf("\nEnter the Element a[%d][%d] : ", i , j);
       scanf("%d",&a[i][j]);
       }

sum = 0;

for(i=0;i<m;i++ )
       for(j=0;j<n;j++)
       {
        if ( i == j )
        sum = sum + a[i][j];
       }
printf("\n Result after adding Diagonal Elements: %d\n",sum);
}

Convert given string into uppercase

#include <stdio.h>
#include <string.h>
 main()
{
    char string[] = "strupr in c";

    printf("%s\n",strupr(string));
}

Convert given string into Lowercase

#include <stdio.h>
#include <string.h>
 main()
{
    char string[] = "Strlwr in C";

    printf("%s\n",strlwr(string));
 }