CHEQUE THE IP ADRESS PROGRAME

CHEQUE THE IP ADRESS PROGRAME;
 

CHEQUE THE IP ADRESS PROGRAME C programming code;

#include<stdlib.h> main(){ system("C:\\Windows\\System32\\ipconfig"); system("pause");  return 0;} IP address program executable.Output of program: ( In Windows XP )


PROGRAME OF STRING

PROGRAME OF STRING
 

strlwr, strupr in c

Here we will change string case with and without strlwr, strupr functions.

strlwr in c

#include<stdio.h>
#include<string.h>
 
main()
{
    char string[] = "Strlwr in C";
 
    printf("%s\n",strlwr(string));
 
    return  0;
}

strupr in c

#include<stdio.h>
#include<string.h>
 
main()
{
    char string[] = "strupr in c";
 
    printf("%s\n",strupr(string));
 
    return  0;
}

Change string to upper case without strupr

#include<stdio.h>
 
void upper_string(char*);
 
main()
{
   char string[100];
 
   printf("Enter a string to convert it into upper case\n");
   gets(string);
 
   upper_string(string);
 
   printf("Entered string in upper case is \"%s\"\n", string);
 
   return 0;
}
 
void upper_string(char *string)
{
   while(*string)
   {
       if ( *string >= 'a' && *string <= 'z' )
       {
          *string = *string - 32;
       }
       string++;
   }
}

Change string to lower case without strlwr

#include<stdio.h>
 
void lower_string(char*);
 
main()
{
   char string[100];
 
   printf("Enter a string to convert it into lower case\n");
   gets(string);
 
   lower_string(string);
 
   printf("Entered string in lower case is \"%s\"\n", string);
 
   return 0;
}
 
void lower_string(char *string)
{
   while(*string)
   {
      if ( *string >= 'A' && *string <= 'Z' )
      {
         *string = *string + 32;
      }
      string++;
   }
}

C programming code for binary search

C programming code for binary search

#include<stdio.h>
 
main()
{
   int c, first, last, middle, n, search, array[100];
 
   printf("Enter number of elements\n");
   scanf("%d",&n);
 
   printf("Enter %d integers\n", n);
 
   for ( c = 0 ; c < n ; c++ )
      scanf("%d",&array[c]);
 
   printf("Enter value to find\n");
   scanf("%d",&search);
 
   first = 0;
   last = n - 1;
   middle = (first+last)/2;
 
   while( first <= last )
   {
      if ( array[middle] < search )
         first = middle + 1;    
      else if ( array[middle] == search ) 
      {
         printf("%d found at location %d.\n", search, middle+1);
         break;
      }
      else
         last = middle - 1;
 
      middle = (first + last)/2;
   }
   if ( first > last )
      printf("Not found! %d is not present in the list.\n", search);
 
   return 0;   
}
 

Matrix multiplication in c language

Matrix multiplication in c language

#include <stdio.h>
 
int main()
{
  int m, n, p, q, c, d, k, sum = 0;
  int first[10][10], second[10][10], multiply[10][10];
 
  printf("Enter the number of rows and columns of first matrix\n");
  scanf("%d%d", &m, &n);
  printf("Enter the elements of first matrix\n");
 
  for (  c = 0 ; c < m ; c++ )
    for ( d = 0 ; d < n ; d++ )
      scanf("%d", &first[c][d]);
 
  printf("Enter the number of rows and columns of second matrix\n");
  scanf("%d%d", &p, &q);
 
  if ( n != p )
    printf("Matrices with entered orders can't be multiplied with each other.\n");
  else
  {
    printf("Enter the elements of second matrix\n");
 
    for ( c = 0 ; c < p ; c++ )
      for ( d = 0 ; d < q ; d++ )
        scanf("%d", &second[c][d]);
 
    for ( c = 0 ; c < m ; c++ )
    {
      for ( d = 0 ; d < q ; d++ )
      {
        for ( k = 0 ; k < p ; k++ )
        {
          sum = sum + first[c][k]*second[k][d];
        }
 
        multiply[c][d] = sum;
        sum = 0;
      }
    }
 
    printf("Product of entered matrices:-\n");
 
    for ( c = 0 ; c < m ; c++ )
    {
      for ( d = 0 ; d < q ; d++ )
        printf("%d\t", multiply[c][d]);
 
      printf("\n");
    }
  }
 
  return 0;
}
A 3 X 3 matrix multiply in c is shown as example below.
Compiler used: 
GCC
Output of program: 

SUBSTRACT MATRIX IN PROGRAMMING C;

SUBSTRACT MATRIX IN PROGRAMMING C;
Subtract matrices

C code to subtract matrices of any order. This program finds difference between corresponding elements of two matrices and then print the resultant matrix.

C programming code

#include<stdio.h>
 
int main()
{
  int m, n, c, d, first[10][10], second[10][10], difference[10][10];
 
  printf("Enter the number of rows and columns of matrix\n");
  scanf("%d%d", &m, &n);
  printf("Enter the elements of first matrix\n");
 
  for (c = 0; c < m; c++)
    for (d = 0 ; d < n; d++)
      scanf("%d", &first[c][d]);
 
  printf("Enter the elements of second matrix\n");
 
  for (c = 0; c < m; c++)
    for (d = 0; d < n; d++)
        scanf("%d", &second[c][d]);
 
  for (c = 0; c < m; c++)
    for (d = 0; d < n; d++)
      difference[c][d] = first[c][d] - second[c][d];
 
  printf("difference of entered matrices:-\n");
 
  for (c = 0; c < m; c++)
  {
    for (d = 0; d < n; d++)
      printf("%d\t",difference[c][d]);
 
    printf("\n");
  }
 
  return 0;
}



SHUT DOWN OF YOUR COMPUTER PROGRAME

SHUT DOWN OF YOUR COMPUTER PROGRAME

C programming code for Windows XP

#include <stdio.h>
#include <stdlib.h>
 
main()
{
   char ch;
 
   printf("Do you want to shutdown your computer now (y/n)\n");
   scanf("%c",&ch);
 
   if (ch == 'y' || ch == 'Y')
      system("C:\\WINDOWS\\System32\\shutdown -s");
 
   return 0;
}

C programming code for Windows 7

#include <stdio.h>
#include <stdlib.h>
 
main()
{
   char ch;
 
   printf("Do you want to shutdown your computer now (y/n)\n");
   scanf("%c",&ch);
 
   if (ch == 'y' || ch == 'Y')
      system("C:\\WINDOWS\\System32\\shutdown /s");
 
   return 0;
}
To shutdown immediately use "C:\\WINDOWS\\System32\\ shutdown /s /t 0". To restart use /r instead of /s.

C programming code for Ubuntu Linux

#include <stdio.h>
 
int main() {
  system("shutdown -P now");
  return 0;
} 
You need to be logged in as root user for above program to execute otherwise you will get the message shutdown: Need to be rootnow specifies that you want to shutdown immediately. '-P' option specifes you want to power off your machine. You can specify minutes as:
shutdown -P "number of minutes"
For more help or options type at terminal: man shutdown

C programming code to swap using bitwise XOR

C programming code to swap using bitwise XOR

#include <stdio.h>
 
int main()
{
  int x, y;
 
  scanf("%d%d", &x, &y);
 
  printf("x = %d\ny = %d\n", x, y);
 
  x = x ^ y;
  y = x ^ y;
  x = x ^ y;
 
  printf("x = %d\ny = %d\n", x, y);
 
  return 0;
}

Swapping numbers using call by reference

Swapping numbers using call by reference

In this method we will make a function to swap numbers.
#include <stdio.h>
 
void swap(int*, int*);
 
int main()
{
   int x, y;
 
   printf("Enter the value of x and y\n");
   scanf("%d%d",&x,&y);
 
   printf("Before Swapping\nx = %d\ny = %d\n", x, y);
 
   swap(&x, &y); 
 
   printf("After Swapping\nx = %d\ny = %d\n", x, y);
 
   return 0;
}
 
void swap(int *a, int *b)
{
   int temp;
 
   temp = *b;
   *b = *a;
   *a = temp;   
}

Swap two numbers using pointers

Swap two numbers using pointers

#include <stdio.h>
 
int main()
{
   int x, y, *a, *b, temp;
 
   printf("Enter the value of x and y\n");
   scanf("%d%d", &x, &y);
 
   printf("Before Swapping\nx = %d\ny = %d\n", x, y);
 
   a = &x;
   b = &y;
 
   temp = *b;
   *b = *a;
   *a = temp;
 
   printf("After Swapping\nx = %d\ny = %d\n", x, y);
 
   return 0;
}

Swapping of two numbers without third variable

Swapping of two numbers without third variable

You can also swap two numbers without using temp or temporary or third variable. In that case c program will be as shown :-
#include <stdio.h>
 
int main()
{
   int a, b;
 
   printf("Enter two integers to swap\n");
   scanf("%d%d", &a, &b);
 
   a = a + b;
   b = a - b;
   a = a - b;
 
   printf("a = %d\nb = %d\n",a,b);
   return 0;
}

To understand above logic simply choose a as 7 and b as 9 and then do what is written in program. You can choose any other combination of numbers as well. Sometimes it's a good way to understand a program.

Swapping of two numbers in c

Swapping of two numbers in c

#include <stdio.h>
 
int main()
{
   int x, y, temp;
 
   printf("Enter the value of x and y\n");
   scanf("%d%d", &x, &y);
 
   printf("Before Swapping\nx = %d\ny = %d\n",x,y);
 
   temp = x;
   x = y;
   y = temp;
 
   printf("After Swapping\nx = %d\ny = %d\n",x,y);
 
   return 0;
}

SWAPING IN C PROGRAME

SWAPING IN C PROGRAME;

c program to swap two numbers

C program to swap two numbers with and without using third variable, swapping in c using pointersfunctions (Call by reference) and using bitwise XOR operator, swapping means interchanging. For example if in your c program you have taken two variable a and b where a = 4 and b = 5, then before swapping a = 4, b = 5 after swapping a = 5, b = 4
In our c program to swap numbers we will use a temp variable to swap two numbers. Swapping is used in sorting that is when we wish to arrange numbers in a particular order either in ascending order or in descending order.

DECIMAL TO BINARY CONVERSON

DECIMAL TO BINARY CONVERSON;

C programming code


#include <stdio.h>
 
int main()
{
  int n, c, k;
 
  printf("Enter an integer in decimal number system\n");
  scanf("%d", &n);
 
  printf("%d in binary number system is:\n", n);
 
  for (c = 31; c >= 0; c--)
  {
    k = n >> c;
 
    if (k & 1)
      printf("1");
    else
      printf("0");
  }
 
  printf("\n");
 
  return 0;
}

C Programming (Turbo C++ Compiler) - Greatest Common Divisor (GCD) and Least Common Multiple (LCM)

C Programming (Turbo C++ Compiler) - Greatest Common Divisor (GCD) and Least Common Multiple (LCM)









I have given here the source code for calculating Greatest Common Divisor(GCD) and Least Common Multiple (LCM). GCD can be found with a simple while loop by comaring the two numbers and assigning the difference to the largest number until the two numbers are equal. Once you know GCD, finding LCM is easy with the formula.

LCM(a,b) = (a * b)/ GCD(a,b)



Source Code




#include <stdio.h>
int GetGCD(int num1, int num2)
{
while(num1!=num2)
{
if(num1 > num2)
num1 = num1 - num2;
if(num2 > num1)
num2 = num2 - num1;
}
return num1;
}
int GetLCM(int num1, long num2)
{
return (num1 * num2) / GetGCD(num1, num2);
}
long main()
{
long num1, num2;
long gcd, lcm;
printf("Enter First Number: ");
scanf("%d", &num1);
printf("Enter Second Number: ");
scanf("%d", &num2);
gcd = GetGCD(num1, num2);
lcm = GetLCM(num1, num2);
printf("\nGCD(%d,%d) = %d\n", num1, num2, gcd);
printf("\nLCM(%d,%d) = %d\n\n\n", num1, num2, lcm);
return 0;
}

Output




Enter First Number: 10
Enter Second Number: 135
GCD(10,135) = 5
LCM(10,135) = 270
Press any key to continue

C Programming (Turbo C++ Compiler) Enumerations

.C Programming (Turbo C++ Compiler) Enumerations





There is a very clear difference between macro and enumerations, event it may confuse some people. enumeration is not a macro and it stays even after compilation. enums are used when there is logical grouping of elements are necessary.

The first element in the enum has default value set to 0, if there is no value assigned. The next elements has follow the simple formula of the value of the previous element plus 1.

Look at the sample code and output. It is easy to understand.

Source Code


void main()
{
enum Location {
LocUSA, LocUK, LocGermany, LocIndia, LocChina, LocJapan,
LocPhilippines, LocMalaysia, LocSingapore, LocThailand,
LocIndonesia, LocOthers
};
enum Random {
randData1 = -23, randData2, randData3, randData4 = 10, randData5
};
printf("USA: %d\n", LocUSA);
printf("India: %d\n", LocIndia);
printf("randData2: %d\n", randData2); printf("randData5: %d\n", randData5);
}

Output


USA: 0
India: 3
randData2: -22
randData5: 11

Press any key to continue . . .

Turbo C - Working With Arrays

Turbo C - Working With Arrays




Here is a simple program that is to initialize the array and count the number of elements in the perfect way.
Also refer the following sample code that also explains the concept of array:

Find the biggest of N Numbers

Find the smallest of N Numbers


Source Code


#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <bios.h>
#include <ctype.h>
void main()
{
int numbers[] = { 10, 11, 12, 13 };
int numElements = sizeof(numbers) / sizeof(numbers[0]);
for(int i = 0; i <numElements; i++)
printf("%d\n", numbers[i]);
}

Output


10
11
12
13


C Programming (Turbo C++ Compiler) - Switch Case Statement

C Programming (Turbo C++ Compiler) - Switch Case Statement





We can write the entire code with if else statements and with out using switch case statement at all. Then why do we need switch case statements? The answer is for program clarity and ease of maintenance.

If we have 2 - 4 cases, then we can use if else statements. If we have more number of cases, then still you can proceed with if else, however it is better to use switch case statements.

switch(key) - switch will take key as an argument. It can be of numeric integer value only (both positive and negative numbers). It can not accept any floating point numbers or strings. It can accept  as they are treated as integers.

Note: If you do not use break statement after case block, then it will continue executing the next case.

Source Code


int main()
{
enum Location {
LocUSA, LocUK, LocGermany, LocIndia, LocChina, LocJapan,
LocPhilippines, LocMalaysia, LocSingapore, LocThailand,
LocIndonesia, LocOthers
};
Location key = LocIndia;
switch(key)
{
case LocUSA:
{
printf("USA");
break;
}
case LocUK:
{
printf("UK");
break;
}
case LocGermany:
{
printf("Germany");
break;
}
case LocIndia:
{
printf("India");
break;
}
case LocChina:
{
printf("China");
break;
}
case LocPhilippines:
{
printf("Philippines");
break;
}
default:
{
printf("Others");
break;
}
};
}

Output


India
.



Turbo C - Simple IF Statement

Turbo C - Simple IF Statement






Here is an example for simple if statement.

if statement evaluates the condition inside the paranthesis and if it is true, then it executes one line followed by if statement or the sequence of steps bound of { }.

if statement can be combined with else part which is explained in if else statement.


Source Code



#include <stdio.h>
void main()
{
int x;
printf("Enter a Number: ");
scanf("%d", &x);
if(x < 0)
printf("%d is a negative number\n"x);
}

Output



Enter a Number: -100
-100 is a negative number

Turbo C - Simple IF ELSE Statement


.Turbo C - Simple IF ELSE Statement





Here is an example for simple if else statement.

if statement evaluates the condition inside the paranthesis and if it is true, then it executes one line followed by if statement or the sequence of steps bound of { }.

if statement can be combined with else part. Else part will be executed when the if condition is false.


Source Code


#include <stdio.h>
void main()
{
int x;
printf("Enter a Number: ");
scanf("%d", &x);
if(x < 0)
printf("%d is a negative number\n"x);
else
printf("%d is a positive number\n"x);
}

Output


Enter a Number: -100
-100 is a negative number

Enter a Number: 55
55 is a positive number

Turbo C - Nested IF Statement

Turbo C - Nested IF Statement





Here is an example for nested if statement in C Programming Language. Nested if means, an simple if statement occurs with in another if statement.

if statement evaluates the condition inside the paranthesis and if it is true, then it executes one line followed by if statement or the sequence of steps bound of { }.

if statement can be combined with else part which is explained in if else statement.

Nested if statement is explained with finding the biggest of 3 numbers example given below.


Source Code


#include <stdio.h>
void main()
{
int a,b,c;
int biggest;
printf("Enter 1st Number: ");
scanf("%d", &a);
printf("Enter 2nd Number: ");
scanf("%d", &b);
printf("Enter 3rd Number: ");
scanf("%d", &c);
if(a > b)
{
if(a > c)
biggest = a;
else
biggest = c;
}
else
{
if(b > c)
biggest = b;
else
biggest = c;
}
printf("Biggest of 3 numbers is: %d\n"biggest);
}

Output


Enter a Number: -100
-100 is a negative number

Enter a Number: 55
55 is a positive number