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();
}

Program to convert readed temperature from celsius to Fahrenheit or vice versa.

//Program to convert readed temperature from celsius to Fahrenheit  or vice versa.
#include<stdio.h>
#include<conio.h>
void main()
{
float temp,a;
char ch;
clrscr();
printf("Reading temperature according to your choice");
printf("\n if you want to enter temprature in celsius and want to convert it into Fahrenheit please enter \" c \"");
printf("\n if you want to enter temprature in Fahrenheit and want to convert it into Celsius please enter \" f \"");
label:
printf("\n Please enter your choice: ");
ch=getche();
switch(ch)
{
 case 'c':
 case 'C':
     printf("\nPlease enter the temperature:");
     scanf("%f",&temp);
     printf("\nYou have entered %f Celius",temp);
     a=(temp*180)/100+32;
     printf("\nAs your requirement %f Celsius is converted %f Fahrenheit",temp,a);
     break;
 case 'f':
 case 'F':
     printf("\nPlease enter the temperature:");
     scanf("%f",&temp);
     printf("\nYou have entered %f Fahrenheit",temp);
     a=((temp-32)/180)*100;
     printf("\nAs your requirement %f Fahrenheit is converted %f Celsius",temp,a);
     break;
 default:
     printf("\nYou have entered a wrong choice please make sure that you are entering the correct choice");
     printf("\nPlease press \"c\" to enter temperatue in Celsius or \"f\" in fahrenheit");
     goto label;
 }
printf("\n Wheather you want to perform more conversions if yes then enter \"Y\" else \"N\"");
printf("\n Please enter your choice: ");
ch=getche();
if(ch=='y'||ch=='Y')
 {
  goto label;
 }
 else
 {
   printf("\nThank you! Have a gud time");
 }
getch();
}

Program to perform deletion operation in array.

//Program to perform deletion operation in array.
#include<stdio.h>
#include<conio.h>
void main()
{
 int i,*data,n,m,a,l,e;
 char c;
 clrscr();
 printf("Enter the number of elements in the array:");
 scanf("%d",&n);
 data=(int*)malloc(n*sizeof(int));
 for(i=0;i<n;i++)
 {
 printf("\nEnter the value of %d element:",i);
  scanf("%d",&data[i]);
 }
 printf("\nArray before deletion");
 for(i=0;i<n;i++)
  printf("%d, ",data[i]);
      a=0;
      l=n-1;
 printf("\nif u want to delete beginning element then press b");
 printf("\nif u want to delete middle element then press m");
 printf("\nif u want to delete end element then press e");
 printf("\nEnter ur choice: ") ;
 c=getche();
 switch(c)
 {
  case 'b':
  case 'B':
      e=data[0];
      for(i=0;i<n-1;i++)
      {
       data[i]=data[i+1];
      }
      n=n-1;
      break;
  case 'm':
  case 'M':
      m=(a+l)/2;
      e=data[m];
      for(i=m;i<n-1;i++)
      {
    data[i]=data[i+1];
      }
      n=n-1;
     break;
  case 'e':
  case 'E':
     e=data[l];
     n=n-1;
     break;
  default:
       printf("\nInvalid choice");
       getch();
       exit(0);
 }
  printf("\nArray after deletion:");
  for(i=0;i<n;i++)
  printf("%d, ",data[i]);
  printf("\n\n Deleted element is %d",e);
 getch();
}

Count number of days in the given month of the year.

//Program to count number of days in the given month of  the year.
#include<stdio.h>
#include<conio.h>
void main()
{
 int y,m;
 clrscr();
 printf("Program to count number of days in the given month of the year");
 printf("\n\nPlease enter the month and the year");
 scanf("%d%d",&m,&y);
 switch(m)
 {
 case 1:
 case 3:
 case 5:
 case 7:
 case 8:
 case 10:
 case 12:
  printf("\n\nThe number of days in the given month of the year is 31");
  break;
 case 4:
 case 6:
 case 9:
 case 11:
  printf("\n\nThe number of days in the given month of the year is 30");
  break;
 case 2:
  if(y/100==0)
  {
   if(y%400==0)
    {
      printf("\n\nThe number of days in the given month of the year is 29");
    }
    else
    {
      printf("\n\nThe number of days in the given month of the year is 28");
    }
  }
  else
  {
   if(y%4==0)
   {
      printf("\n\nThe number of days in the given month of the year is 29");
    }
    else
    {
      printf("\n\nThe number of days in the given month of the year is 28");
    }
  }
  break;
 default:
   printf("You have entered a wrong month plz be make sure you have entered coorect information");
   break;
 }
getch();
}

Program to print the date in the 19 th century

//Program to print the date in the 19 th century
#include<stdio.h>
#include<conio.h>
void main()
{
 int d,m,y;
 void check(int,int,int);
 char c;
 clrscr();
 printf("Program to print the date in the 19 th century");
 while(1)
 {
 printf("\n Enter the three numbers please make sure that\"First value is less than equal to 31\" and also \"the second is less than equal to 12\" and \"third value is less than equal to 99\": ");
 scanf("%d%d%d",&d,&m,&y);
 if(d<=31&&m<=12&&y<=99)
 {
  y=19*100+y;
  switch(d)
  {
  case 29:
   case 30:
   case 31:
   if(m==2)
   {
     if(y%4==0&&y!=1900)
     {
      if(d<=29)
      {
       printf("\nDate entered by you is :");
       printf("%dnd ");
       break;
      }
      else
      {
        printf("\nyou have entered wrong date please make sure that you are entering correct data");
        continue;
      }
     }
    else
     {
      if(d>28)
      {
        printf("\n you have entered wrong date please make sure that you are entering correct data");
        continue;
      }
     }
   }
   else
   {
    if(d==31)
    {
     printf("\nDate entered by you is :");
     printf("%dst ");
     break;
    }
    else
    {
     printf("\nDate entered by you is :");
    printf("%dth ");
    break;
    }
   }
   case 1:
   case 21:
 printf("\nDate entered by you is :");
    printf("%dst ");
    break;
   case 2:
   case 22:
 printf("\nDate entered by you is :");
    printf("%dnd ");
    break;
   case 3:
   case 23:
 printf("\nDate entered by you is :");
    printf("%drd ");
    break;

   default:
 printf("\nDate entered by you is :");
    printf("%dth ");
  }
  switch(m)
  {
   case 1:
    printf("Jan ");
    break;
   case 2:
    printf("Feb ");
    break;
   case 3:
    printf("March ");
    break;
   case 4:
    printf("April ");
    break;
   case 5:
    printf("May ");
    break;
   case 6:
    printf("June ");
    break;
   case 7:
    printf("July ");
    break;
   case 8:
    printf("Aug ");
    break;
   case 9:
    printf("Sep ");
    break;
   case 10:
    printf("Oct ");
    break;
   case 11:
    printf("Nov ");
    break;
   case 12:
    printf("Dec ");
    break;
  }
  printf("%d",y);
 }
 else
 {
  printf("\nPlease read the instructions carefully and enter the correct data");
  continue;
 }
 printf("\n wheather you want display more dates please press y for \"Yes\" or n for \"No\": ");

 c=getche();
 if(c=='y'||c=='Y')
  continue;
  else
  break;
}
 getch();
}

Create simple Calculator using C concepts.

//Program to create simple Calculator using C concepts.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,r;
float f;
char ch,fun;
clrscr();
label:
printf("\n Please choose operation you want to perform ");
printf("\nPress + for the addition of the two numbers");
printf("\nPress - for the subtraction of the two numbers");
printf("\nPress * for the multiplication of the two numbers");
printf("\nPress / for the division of the two numbers");
printf("\n please enter your choice:");
fun=getche();
 switch (fun)
 {
 case '+':
  printf("\nPlease enter the first number which you want to add:");
  scanf("%d",&a);
  printf("\nPlease enter the second number:");
  scanf("%d",&b);
  r=a+b;
  printf("%d + %d= %d",a,b,r);
  break;
 case '-':
   printf("\nPlease enter the first number");
  scanf("%d",&a);
  printf("\nPlease enter the second number:");
  scanf("%d",&b);
  r=a-b;
  printf("%d - %d= %d",a,b,r);
  break;
 case '*':
   printf("\nPlease enter the first number:");
  scanf("%d",&a);
  printf("\nPlease enter the second number:");
  scanf("%d",&b);
  r=a*b;
  printf("%d * %d= %d",a,b,r);
  break;
 case '/':
  printf("\nPlease enter the first number :");
  scanf("%d",&a);
  printf("\nPlease enter the second number:");
  scanf("%d",&b);
  f=(float)a/b;
  printf("%d / %d=%f",a,b,f);
  break;
 default:
  printf(" you hve entered a wrong choice! please make sure that you are choosing accurate choice");
  break;
 }
 printf("\n Wheather you want to perform some another function please enter your choice Y or N");
ch=getche();
 if(ch=='y'||ch=='Y')
 {
   goto label;
 }
 else
 {
  printf(" \n you completed your calculations!!!!!!! thank u to use my program Bimal");
  getch();
  exit();
 }
 getch();
}

Friday, August 19, 2011

Program to check wheather entered 3 digit number has all even or all odd or mixed odd and even digits.

//Program to check wheather entered 3 digit number has all even or all odd or mixed odd and even digits.
#include<stdio.h>
#include<conio.h>
void main()
{
int num,r,n,a,b;
char ch;
clrscr();
printf("To check wheather entered 3 digit number has all even or all odd or mixed odd and even digits");
label:
a=0;
b=0;
n=0;
printf("\nPlease enter 3 digit number:");
scanf("%d",&num);
while(num>0)
{
 r=num%10;
 num=num/10;
 if(r%2==0)
 {
  a=a+1;
 }
 else
 {
 b=b+1;
 }
 n=n+1;
}
if(n!=3)
{
 printf("\nSory you have entered a wrong number,please enter a correct number");
 goto label;
}
if(a==3)
{
 printf("\nNumber entered by you have all even digits");
}
else if(b==3)
{
 printf("\nNumber entered by you have all odd digits");
}
else
{
 printf("\nNumber entered by you have %d even digits and %d odd digits",a,b);
}
printf("\n\nWheather you want to check more numbers or not if Yes then press \"Y\" else press\"N\" ");
ch=getche();
if(ch=='Y'||ch=='y')
{
goto label;
}
else
{
 printf("\n\nThank you! Have a gud day");
}
getch();
}

Program to demonstarte the use of Switch case.

//Program to demonstarte the use of Switch case.
#include<stdio.h>
#include<conio.h>
void main()
{
 int code;
 clrscr();
 printf("main menu");
 printf("\n 1 for color red");
 printf("\n 2 for color green");
 printf("\n 3 for color white");
 printf("\n 4 for color yellow");
 printf("\n enter the color code");
 scanf("%d",&code);
 switch(code)
 {
  case 1:
  printf("\n color is red");
  break;
   case 2:
  printf("\n color is green");
  break;
   case 3:
  printf("\n color is white");
  break;
   case 4:
  printf("\n color is yellow");
  break;
  default:
  printf("\n color does not found");
 }
 getch();
}

Program to check whether given alphabet is vowel or not using If Else statement.

//Program to check whether given alphabet is vowel or not using If Else statement.
#include<stdio.h>
#include<conio.h>
void main()
{
 char c;
 printf("\n Enter the alphabet:");
 scanf("%s",&c);
 if(c=='a'|| c=='e'|| c=='i'|| c=='o'|| c=='u')
 {
  printf("\n alphabet is vowel");
 }
 else
 {
  printf("\n alphabet is not vowel");
 }
 getch();
}

Program to check whether the given number is Even or Odd.

//Program to check whether the given number is Even or Odd.
#include<stdio.h>
#include<conio.h>
void main()
{
 int n;
 clrscr();
 printf("\n enter the value of n");
 scanf("%d",&n);
 if((n%2)==0)
 {
   printf("\n number is even");
 }
 else
 {
   printf("\n number is odd");
 }
 getch();
}

Program to find the greatest number among two numbers.

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

Next Home
Twitter Delicious Facebook Favorites More