Wednesday, 17 August 2011

Program to identify the prime number


Code:
#include<stdio.h>
#include<conio.h>
void main()
{
      int num,flag=0,a;
            printf("Enter an integer: ");
            scanf("%d",&num);
      //Logic for prime number
            if(num==2)
                  printf("%d is Prime Number.",num); 
            else
            {
                  for(a=2;a<num;a++)
                  {
                        if(num%a==0)
                              flag=1;
                  }
            }
            if(flag==1)
                  printf("%d is not a Prime Number.",num);
            else
                  printf("%d is Prime Number.",num);
      getch();

}

Result:

Enter an integer: 8
8 is not a Prime Number.

If you have any confusion about that program, contact me freely :)


Interchanging two variables without using the third one


Using less variables is a sign of good programming. Here's the example program to interchanging the values of two variables without using third variable.

Code:
#include<stdio.h>
#include<conio.h>
void main()
{
//Initialization
  int var1,var2; 
    printf("Enter two integers\n");
//Taking input from user
      printf("a = ");
      scanf("%d",&var1);
      printf("b = ");
      scanf("%d",&var2);
   var1=var1+var2;
   var2=var1-var2;
   var1=var1-var2;
    printf("a = %d\n",var1);
    printf("b = %d\n",var2);
  getch();

}

Result:

Enter two integers
a = 5
b = 11
a = 11
b = 5