Showing posts with label Decision Making Statements. Show all posts
Showing posts with label Decision Making Statements. Show all posts

Saturday, August 20, 2011

Find twin prime numbers.

//Program to find twin prime numbers.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
 int pr,i=0,k,flag=1,j,a[25],n;
 clrscr
 ();
 n=0;
 printf("prime numbers");
 while(n<=100)
 {
   if(n==0||n==1)
   {
    n=n+1;
    continue;
   }
   k=sqrt(n);
   for(j=2;j<=k;j++)
   {
    if((n%j)==0)
    {
     flag=0;
     break;
    }
    else
    {
    flag=1;
    }
   }
   if(flag==1)
    {
    a[i]=n;
    printf("%d,",n);
    i+=1;
    }
    n=n+1;
  }
  printf("\n Number of primes %d",i);
  for(i=0;i<24;i++)
  {
   if(a[i+1]-a[i]==2)
   {
    printf("\n %d and %d are the twin primes",a[i],a[i+1]);
   }
  }
  getch();
}

Check whether the entered number is Palidrone or not.

//Program to check whether the entered number is Palidrone or not.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
 int num,r,n=0,temp;
 float s;
 char c;
 clrscr();
 printf("\n Enter the number: ");
 scanf("%d",&num);
 temp=num;
 while(temp>0)
 {
  r=temp%10;
  temp=temp/10;
  n=n*10+r;
 }
 if(num==n)
 printf("entered number is palidrone");
 else
 printf("entered number is not palidrone");
 getch();
}

Program to find the greatest number among three numbers without using nested if else.

//Program to find the greatest number among three numbers without using nested if else.
#include<stdio.h>
#include<conio.h>
void main()
{
 int a,b,c,d;
 clrscr();
 printf("The values of a,b and c");
 scanf("%d%d%d",&a,&b,&c);
 if(a>b)
 {
  d=a;
 }
  else
  {
   d=b;
  }
  if(c>d)
  {
   d=c;
  }
  else
  {
   d=d;
  }
 printf("\n Greatest no. is:%d",d);
 getch();
}

Program to check wheather the given year is leap year or not.


//Program to check wheather the given year is leap year or not.
#include<stdio.h>
#include<conio.h>
void main()
{
 int year;
 clrscr();
 printf("To check wheather the given year is leap year or not");
 printf("\nPlease enter the year:");
 scanf("%d",&year);
 if(year%100==0)
 {
  if(year%400==0)
  {
  printf("\nThe entered year (%d) is leap year",year);
  }
  else
  {
   printf("\nThe entered year (%d) is not leap year",year);
   getch();
   exit();
  }
 }
 else
 {
 if(year%4==0)
  {
   printf("\nThe entered year (%d) is leap year",year);
  }
  else
  {
   printf("\nThe entered year (%d) is not leap year",year);
  }
 }
getch();
}

Program to find hcf and lcm of two numbers

//Program to find hcf and lcm of two numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,a,b,h,l,r=1;
clrscr();
printf("Program to find hcf nd lcm of two numbers");
printf("Enter the two numbers greater then equal to 1: ");
scanf("%d%d",&m,&n);
if(m>=1&&n>=1)
{
if(m>=n)
{
a=m;
b=n;
}
else
{
a=n;
b=m;
}
while(r!=0)
{
 r=a%b;
 if(r!=0)
 {
  a=b;
  b=r;
 }
}
h=b;
l=(m*n)/h;
}
printf("hcf of two numbers is %d and lcm of two numbers is %d ",h,l);
getch();
}

Next Home
Twitter Delicious Facebook Favorites More