Monday, June 18, 2012

To check whether the entered character is vowel or not, using Switch statement


/* To check whether the entered character is vowel or not, using Switch statement */

#include<stdio.h>

#include<conio.h>

void main()

{

  char ch;

  clrscr();

  printf("\nEnter an alphabet: ");

  scanf("%c",&ch);

  switch(ch)

  {

    case 'a':

    case 'A':

    case 'e':

    case 'E':

    case 'i':

    case 'I':

    case 'o':

    case 'O':

    case 'u':

    case 'U':

      printf("%c is a Vowel!",ch);

      break;

    default:

      printf("%c is NOT a Vowel!",ch);

  }

  getch();

}

 

OUTPUT-1

Enter an alphabet: A

A is a Vowel!

 

OUTPUT-2

Enter an alphabet: d

d is NOT a Vowel!

 

To print the area of geometrical figure using Switch statement


/* To print the area of geometrical figure using Switch statement */

#include<stdio.h>

#include<conio.h>

void main()

{

  int choice;

  float a,b,area;

  clrscr();

  printf("\nGiven choices are:\n");

  printf("\n1-Circle");

  printf("\n2-Square");

  printf("\n3-Triangle");

  printf("\n4-Rectangle");

  printf("\n\nEnter your choice: ");

  scanf("%d",&choice);

  switch(choice)

  {

    case 1:

      printf("\nEnter the radius of circle: ");

      scanf("%f",&a);

      area=3.142*a*a;

      printf("\nArea of Circle=%f",area);

      break;

   case 2:

      printf("\nEnter the side of square: ");

      scanf("%f",&a);

      area=a*a;

      printf("\nArea of Square=%f",area);

      break;

   case 3:

      printf("\nEnter the base and height of triangle: ");

      scanf("%f%f",&a,&b);

      area=a*b/2;

      printf("\nArea of Triangle=%f",area);

      break;

   case 4:

      printf("\nEnter the length and breadth of rectangle: ");

      scanf("%f%f",&a,&b);

      area=3.142*a*a;

      printf("\nArea of Circle=%f",area);

      break;

   default:

     printf("Given choice does not exist!!!\n");

     break;

  }

  getch();

}

 

OUTPUT

 

Given choices are:

 

1-Circle

2-Square

3-Triangle

4-Rectangle

 

Enter your choice: 3

 

Enter the base and height of triangle: 40 32

 

Area of Triangle=640.000000

 

To implement simple calculator using Switch statement


/* To implement simple calculator using Switch statement */

#include<stdio.h>

#include<conio.h>

void main()

{

  float a,b;

  char op,c;

  clrscr();

  printf("\nType in your expression\n");

  scanf("%f %c %f",&a,&op,&b);

  switch(op)

  {

    case '+':

      printf("%f\n",a+b);

      break;

   case '-':

      printf("%f\n",a-b);

      break;

   case '*':

      printf("%f\n",a*b);

      break;

   case '/':

      printf("%f\n",a/b);

      break;

   default:

     printf("Unknown operator!!!\n");

     break;

  }

  getch();

}

 

 

OUTPUT

Type in your expression

7+9

16.000000

To find the greatest among three numbers


/* To find the greatest among three numbers */

#include<stdio.h>

#include<conio.h>

void main()

{

 int a,b,c;

 clrscr();

 printf("Enter any three numbers: \n");

 scanf("%d%d%d",&a,&b,&c);

 if(a>b)

 {

   if(a>c)

     printf("\nFirst number is the largest");

   else

     printf("\nThird number is the largest");

 }

 else

 {

   if(b>c)

     printf("\nSecond number is the largest");

   else

     printf("\nThird number is the largest");

 }

 getch();

}

 

OUTPUT

Enter any three numbers:

23

59

47

 

Second number is the largest

 

To find whether the roots of quadratic equation are real or imaginary


/* To find whether the roots of quadratic equation are real or imaginary */

#include<stdio.h>

#include<conio.h>

#include<math.h>

void main()

{

 int a,b,c,d;

 float x1,x2;

 clrscr();

 printf("For a given quadratic equation\n...enter the value of a,b,c:\n");

 scanf("%d%d%d",&a,&b,&c);

 /* To calculate determinant of quadratic equation */

 d=b*b-4*a*c;

 /* To check whether the roots are real or imaginary */

 if(d>=0)

 {

   printf("\nRoots are real!");

   x1=(-(b) + sqrt(d))/(2*a);

   x2=(-(b) - sqrt(d))/(2*a);

   printf("\nRoots are %f and %f",x1,x2);

 }

 else

   printf("\nRoots are imaginary!");

 getch();

}

 

OUTPUT

For a given quadratic equation

...enter the value of a,b,c:

1

3

2

 

Roots are real!

Roots are -1.000000 and -2.000000

 

To find whether the entered year is leap year or not


/* To find whether the entered year is leap year or not */

#include<stdio.h>

#include<conio.h>

void main()

{

 int year;

 clrscr();

 printf("Enter year: ");

 scanf("%d",&year);

 /* Checking for leap year */

 if(year%4 == 0 && year%100 != 0)

   printf("\n Leap Year");

 else if(year%400 == 0)

   printf("\n Leap Year");

 else

   printf("\n Not Leap Year");

 getch();

}

 

OUTPUT

Enter year: 1960

 

 Leap Year

 

To find whether the number entered is even or odd


/* To find whether the number entered is even or odd */

#include<stdio.h>

#include<conio.h>

void main()

{

 int num;

 clrscr();

 printf("Enter any number: ");

 scanf("%d",&num);

 /* Checking for even or odd */

 if(num%2 == 0)

 {

   printf("\n Even Number");

 }

 else

 {

   printf("\n Odd Number");

 }

 getch();

}

 

OUTPUT: 1

Enter any number: 67

 

 Odd Number

 

OUTPUT: 2

Enter any number: 102

 

 Even Number

 

To calculate the gross salary of an emloyee


/* To calculate the gross salary of an employee */

#include<stdio.h>

#include<conio.h>

void main()

{

 float bs,da,hra,gs;

 clrscr();

 printf("Enter the basic salary: ");

 scanf("%f",&bs);

 /* To calculate gross salary */

 if(bs<=1500)

 {

   hra=10*bs/100.00;

   da=90*bs/100.00;

 }

 else

 {

   hra=500;

   da=98*bs/100.00;

 }

 gs=bs+hra+da;

 printf("\nGross Salary=%f",gs);

 getch();

}

 

OUTPUT

Enter the basic salary: 1050.50

 

Gross Salary=2101.000000

 

To calculate the discount; if available


/* To calculate the discount; if available */

#include<stdio.h>

#include<conio.h>

void main()

{

 int qty;

 float dis=0,rate,amt;

 clrscr();

 printf("Enter the quantity of the required item: ");

 scanf("%d",&qty);

 printf("\nEnter the price of the item: ");

 scanf("%f",&rate);

 /* To check if discount is available */

 if(qty>=1000)

 {

   dis=10;

 }

 amt=qty*rate-qty*rate*dis/100;

 printf("\nTotal expenses=%f",amt);

 getch();

}

 

OUTPUT

Enter the quantity of the required item: 1340

 

Enter the price of the item: 6.45

Total expenses=7778.699707

 

To show the implementation of Bitwise Operators


/* To show the implementation of Bitwise Operators */

#include<stdio.h>

#include<conio.h>

void main()

{

  unsigned int a=60;

  unsigned int b=13;

  unsigned int c=0;

  clrscr();

  printf("\nA=60");

  printf("\nB=13");

  /* To implement Logical AND */

  c=a&b;

  printf("\n\nOn implementing Logical AND:\t");

  printf("%u",c);

  /* To implement Logical OR */

  c=a|b;

  printf("\nOn implementing Logical OR:\t");

  printf("%u",c);

  /* To implement Logical XOR */

  c=a^b;

  printf("\nOn implementing Logical XOR:\t");

  printf("%u",c);

  /* To implement Left Shift */

  c=a<<2;

  printf("\nOn implementing Left Shift:\t");

  printf("%u",c);

  /* To implement Right Shift */

  c=a>>2;

  printf("\nOn implementing Right Shift:\t");

  printf("%u",c);

  getch();

}

 

OUTPUT

 

A=60

B=13

 

On implementing Logical AND:        12

On implementing Logical OR:            61

On implementing Logical XOR:         49

On implementing Left Shift:              240

On implementing Right Shift:            15

To calculate simple interest and compound interest

/* To calculate simple interest and compound interest */
#include<stdio.h>

#include<conio.h>

#include<math.h>

void main()

{

 int p,t,n;

 float r,si,ci;

 clrscr();

 printf("Enter the principal amount: ");

 scanf("%d",&p);

 printf("\nEnter the rate of interest: ");

 scanf("%f",&r);

 printf("\nEnter the time period: ");

 scanf("%d",&t);

 printf("\nEnter the compounding per period: ");

 scanf("%d",&n);

 /* To calculate simple interest */

 si = p*r*t/100.00;

 /* To calculate compound interest */

 ci=p*pow((1+r/100),(n*t))-p;

 printf("\n\nSimple Interest=%f",si);

 printf("\nCompound Interest=%f",ci);

 getch();

}

 

OUTPUT

Enter the principal amount: 5000

Enter the rate of interest: 3.4

Enter the time period: 2
 
Enter the compounding per period: 1


Simple Interest=340.000000

Compound Interest=345.779999