Friday, 1 August 2014

Prime Number

Prime number

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. It means a prime number is only divisible by 1 and itself, and it start from 2. The smallest prime number is 2.
****************************************
        Prime Number
*****************************************/
#include< stdio.h>
#include< conio.h>
void main()
{
  int i,no;
  clrscr();
  printf("Enter any number: ");
  scanf("%d",&no);
  if(no==1)
  {
  printf("Smallest Prime number is 2");
  }
  for(i=2;i< no;i++)
  {
  if(no%i==0)
  {
  printf("Not Prime number");
  break;
  }
  }
 if(no==i)
 {
 printf("Prime number");
 }
  getch();
}
Output
Enter any number: 10
Not Prime Number

Program Explanation

First enter any number if this number is 1 then show message "Smallest Prime number is 2".
for(i=2;i< no;i++)
  {
  if(no%i==0)
  {
  printf("Not Prime number");
  break;
  }
  }
In above code we first perform for loop. First initilize i=2, because Every number is divisible by 1 so we start dividing given number from 2, then check condition "i" is less than number (i< no), and increment value of i (i++). Here "if(no%i==0)" is perform like : no%2==0, no%30=0, no%4==0, no%5==0, no%6==0, no%7==0,....... because every time value of i is increment until value of i is less than given number. If reminder is zero then print "Not Prime number" and control goes outside from for loop due to break keyword.
Note:break is only used within loops (for, while, etc) and switch case.
 if(no==i)
 {
 printf("Prime number");
 }
Here if value of i is equal to Given number then print "Prime number", In this code "i" have same value equal to given number because for loop conditon is "i < no" at that time it false when i==no. So this time "i" have same value to given number. And prime number is divisible by 1 and itself, So here we check no==i.

Print next Prime number

When we enter any number this code will print next Prime number.
Example: Suppose we enter 5 then next prime number is 7.
/***********************************************
               Next Prime number
************************************************/
#include< stdio.h>
#include< conio.h>
void main()
{
  int i,j=2,num;
  clrscr();
  printf("Enter any number: ");
  scanf("%d",&num);
  printf("Next Prime number: ");
  for(i=num+1;i< 3000;i++)
   {
    for(j=2;j< i;j++)
      {
     if(i %j==0)
     {
     break;
      } // if
     } // for
     if(i==j || i==1)
       {
       printf("%d\t",i);
    break;
    } // if
    }  // outer for
getch();
}
Output
Enter any number: 10
Next Prime Number 11

Swap two numbers

Swap two numbers using third variables

/********************************************************
               Swap of Two Number
********************************************************/
#include< stdio.h>
#include< conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter value of a:= ");
scanf("%d",&a);
printf("Enter value of b:= ");
scanf("%d",&b);
c=a;
a=b;
b=c;
printf("After swap a=: %d b=: %d",a,b);
getch();
}
Output
images

Swap two numbers without using third variable

/********************************************************
               Swap of Two Number
********************************************************/
#include< stdio.h>
#include< conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter value of a:= ");
scanf("%d",&a);
printf("Enter value of b:= ");
scanf("%d",&b);
a=a+b;
b=a-b;
a=a-b;
printf("After swap \na=: %d\nb=: %d",a,b);
getch();
}
Output
images

Swap two numbers using Pointers

/********************************************************
               Swap of Two Number
********************************************************/
#include< stdio.h>
#include< conio.h> 
int main()
{
  int x,y,*b,*a,temp;
  clrscr();
  printf("Enter any two number : ");
  scanf("%d%d",&x,&y);
  printf("Before swaping : x= %d and y=%d\n",x,y);
  a = &x;
  b = &y;
  temp = *a;
  *a = *b;
  *b = temp;
  printf("After swaping : x= %d and y=%d\n",x,y);
  getch();
}
Output
Enter any two number : 10 20
Before Swaping : x=10 and y=20
After swaping : x=20 and y=10

Swap two numbers using call by Reference

/********************************************************
               Swap of Two Number
********************************************************/
#include< stdio.h>
#include< conio.h> 
int main()
{
  int x,y,*b,*a,temp;
  clrscr();
  printf("Enter any two number : ");
  scanf("%d%d",&x,&y);
  swap(&x, &y);
  printf("Before swaping : x= %d and y=%d\n",x,y);
 }
 void swap(int *a, int *y)
 {
  int temp;
  temp = *a;
  *a = *b;
  *b = temp;
  printf("After swaping : x= %d and y=%d\n",x,y);
  getch();
}
Output
Enter any two number : 30 20
Before Swaping : x=30 and y=20
After swaping : x=20 and y=30

Swap two numbers using Bitwise XOR

/********************************************************
               Swap of Two Number
********************************************************/
#include< stdio.h>
#include< conio.h> 
int main()
{
  int x, y;
  clrscr();
  printf("Enter any two number : ");
  scanf("%d%d",&x,&y);
  printf("Before swaping : x= %d and y=%d\n",x,y);
  x = x ^ y;
  y = x ^ y;
  x = x ^ y;
  printf("After swaping : x= %d and y=%d\n",x,y);
  getch();
}
Output
Enter any two number : 4 5
Before Swaping : x=4 and y=5
After swaping : x=5 and y=4

Even or Odd

Even or Odd

Even numbers are those which are divisible by 2, and which numbers are not divisible 2 is called odd numbers.
But in term of programming for find even number we check remainder ofnumber is zero or not, If remainder is equal to zero that means number is divisible by 2. To find remainder of any number we use modulo (%) operator in C langauge which return remainder as result.

Check give number is Even or Odd

/***************************************************************
       Check give number is Even or Odd
***************************************************************/
#include< stdio.h>
#include< conio.h>
void main()
{
int no;
clrscr();
printf("Enter any number : ");
scanf("%d",&no);
if(no%2==0)
{
printf("Even number");
}
else
{
printf("Odd number");
}
getch();
}
Output
Enter any number : 5
Odd number

Check give number is Even or Odd Using ternary or conditional Operator

/**********************************************************
       Check number is Even or Odd
***********************************************************/
#include< stdio.h>
#include< conio.h>
void main()
{
int no;
clrscr();
printf("Enter any number : ");
scanf("%d",&no);
(no%2==0) ? printf("Even number") : printf("Odd number");
getch();
}
Output
Enter any number : 6
Even number

Check give number is Even or Odd Using Bitwise Operator

/***************************************************************
       Check give number is Even or Odd
***************************************************************/
#include< stdio.h>
#include< conio.h> 
int main()
{
  int num;
  clrscr();
  printf("Enter any number : ");
  scanf("%d",&num);
  if(num & 1)
  {
  printf("%d is odd",num);
  }
  else
  {
  printf("%d is even",num);
  }
  getch();
}
Output
Enter any number : 20
20 is even

Java Development Kit (JDK) and Java Runtime Environment (JRE)

                  Java Development Kit (JDK) and Java Runtime Environment (JRE)  To download and install the Java Development Kit (  JDK ) ...