Friday, 4 April 2014

How to Check a Number is Even or Odd?

It is very simple to check that a given number is even or odd by dividing the number by 2. But it is not possible to check by division every number. So it is very easy that we can do it by writing a C Program with different methods.

/*Check Even or ODD with Modulus Operator*/

#include<stdio.h>
#include<conio.h>
main()
{
    clrscr();
    int numb;

    printf("\nPlease Enter Number:= ");
    scanf("%d",&numb);

    if(numb%2==0)
    {
         printf("\nEven Number");
    else
         printf("\nOdd Number");
    }
getch();
}

/*Check Even or ODD with Bitwise Operator*/

#include<stdio.h>
#include<conio.h>
main()
{
    clrscr();
    int numb;

    printf("\nPlease Enter Number:= ");
    scanf("%d",&numb);

    if(numb & 1==1)
    {
         printf("\nOdd Number");
    else
         printf("\nEven Number");
    }
getch();
}

/*Check Even or ODD with Conditional Operator*/

#include<stdio.h>
#include<conio.h>
main()
{
    clrscr();
    int numb;

    printf("\nPlease Enter Number:= ");
    scanf("%d",&numb);

    numb%2==0 ? printf("\nEven") : printf("\nODD");
    return 0;
}

If you have programming skills then you can develop other methods of finding whether given number is even or odd. And if you have any queries do get back to me, I will respond to your queries within 24 hours with the positive solution.

How To Swap Two Numbers in c Language Program?

Swapping two numbers in C Language is very easy. There are 5 (Five) ways to swap two numbers, you can choose anyone you like. I am mentioning  all one by one below. 
  1. Swap Numbers With Temp (Third) Variable

  2. Swap Numbers Without Temp (Third) Variable

  3. Swap Numbers With Pointers

  4. Swap Numbers With Call By Reference

  5. Swap Numbers With bitwise XOR

Method One  //swap two numbers with third variable

#include<stdio.h>
main()
{
    int valX, valY, valTemp;
    printf("\nEnter the value of X:= ");
    scanf("%d",&valX);
    printf("\nEnter the value of Y:= " );
    scanf("%d",&valY);

    printf("\nBefore Swap\nX=%d\nY=%d",valX,valY);

    valTemp=valX;
    valX=valY;
    valY=valTemp;

    printf("\nAfter Swap\nX=%d\nY=%d",valX,valY);
}

Output of above program is as pasted below: 

Output of Swapping Values





Method Two   //swap two numbers without third variable

#include<stdio.h>
#include<conio.h>
main()
{
     clrscr();
     int x,y;
    printf("\nEnter the value of X:= ");
    scanf("%d",&x);

    printf("\nEnter the value of Y:= " );
    scanf("%d",&y);

    x=x+y;
    y=x-y;
    x=x-y;
    
    printf("\nValues of X, Y After Swap Are:-\n");
    printf("\nValue of  X=%d\nValue of Y=%d",x,y);
    getch();
}

Method Three     //swap two numbers with pointers

#include <stdio.h>
#include <conio.h>
swap(int *valX , int *valY)
{
    int temp;

    temp=*valX;
    *valX=*valY;
    *valY=temp;
}
main()
{
    int a,b;
    clrscr();
    printf("Enter value of A= ");
    scanf("%d", &a);

    printf("\nEnter number B= ");
    scanf("%d", &b);

    printf("\nBefore Swapping\n");
    printf("\nValue of A:= %d ",a);
    printf("\nValue of B:= %d ",b);

    swap(&a,&b);
    printf("\nAfter Swapping\n");
   
printf("\nValue of A:= %d ",a);
    printf("\nValue of B:= %d ",b);

    getch();
}

Method Four  //swap two numbers with call byreference

#include <stdio.h>
#include<conio.h>
void swap(int*, int*);
int main()
{
   clrscr();
   int x, y;

   printf("\nEnter the value of X= %d",x);
   scanf("%d",&x);
   printf("Enter the value of Y= %d",y);
   scanf("%d",&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;
}

How to Divide Two Numbers in C Language

Division in C Programs is very easy as it is in Addition, subtraction and multiplication because pattern is same and also syntax is same. Just one operation is different and that is division. I have put all my efforts while creating this blog. However there are lots of other blogs websites are available for online learning of C Language programs but almost all are based on same complex method of teaching. But my teaching method id quite different as you can see

C Program to Divide Two Numbers

#include<stdio.h>
main()
{
     int var1,var2,answer;
     printf("\nEnter First Value:= ");
     scanf( "%d",&var1);

     printf("\nEnter Second Value:= ");
     scanf( "%d",&var2);

     answer=var1/var2;
     printf("\Answer Is:= %d ",answer);
}

Output of the program

Output of Division

How to Multiply Two Numbers in C Program

Multiplication in C Programs is very easy as it is in Addition, subtraction because pattern is same and also syntax is same. Just one operation is different and that is multiply. I have put all my efforts while creating this blog. However there are lots of other blogs websites are available for online learning of C Language programs but almost all are based on same complex method of teaching. But my teaching method id quite different as you can see

C Program to Multiply Two Numbers

#include<stdio.h>
main()
{
     int var1,var2,answer;
     printf("\nEnter First Value:= ");
     scanf( "%d",&var1);

     printf("\nEnter Second Value:= ");
     scanf( "%d",&var2);

     answer=var1*var2;
     printf("\Answer Is:= %d ",answer);
}

Output of the program

Output of Multiply



How to Subtract Two Numbers in C Language Program

Subtraction in C Programs is very easy as it is in Addition because both are on same pattern and syntax for both is same. Just one operation is different and that is minus. I have put all my efforts while creating this blog. However there are lots of other blogs websites are available for online learning of C Language programs but almost all are based on same complex method of teaching. But my teaching method id quite different as you can see

C Program to Subtract Two Numbers

#include<stdio.h>
main()
{
     int var1,var2,answer;
     printf("\nEnter First Value:= ");
     scanf( "%d",&var1);

     printf("\nEnter Second Value:= ");
     scanf( "%d",&var2);

     answer=var1-var2;
     printf("\Answer Is:= %d ",answer);
}

Output of the program

Output for Subtraction