Search This Blog

Thursday, 9 April 2015

Arithmetic Operators


C program to perform addition, subtraction, multiplication and division


 #include <stdio.h>

int main()
{
   int first, second, add, subtract, multiply;
   float divide;

   printf("Enter two integers\n");
   scanf("%d%d", &first, &second);

   add = first + second;
   subtract = first - second;
   multiply = first * second;
   divide = first / (float)second;   //typecasting

   printf("Sum = %d\n",add);
   printf("Difference = %d\n",subtract);
   printf("Multiplication = %d\n",multiply);
   printf("Division = %.2f\n",divide);

   return 0;
}

Tuesday, 7 April 2015

SQL Triggers

SQL Triggers

A database trigger is procedural code that is automatically executed in response to certain events on a particular table or view in a database. The trigger is mostly used for maintaining the integrity of the information on the database. For example, when a new record (representing a new worker) is added to the employees table, new records should also be created in the tables of the taxes, vacations and salaries.
CREATE TRIGGER name { BEFORE | AFTER } { event [ OR ... ] }
    ON TABLE [ FOR [ EACH ] { ROW | STATEMENT } ]
    EXECUTE PROCEDURE funcname ( arguments )
GO

IF OBJECT_ID ('Sales.reminder2','TR') IS NOT NULL
    DROP TRIGGER Sales.reminder2;
GO

CREATE TRIGGER reminder2
ON Sales.Customer
AFTER INSERT, UPDATE, DELETE
AS
   EXEC msdb.dbo.sp_send_dbmail
        @profile_name = 'AdventureWorks2012 Administrator',
        @recipients = 'danw@Adventure-Works.com',
        @body = 'Don''t forget to print a report for the sales force.',
        @subject = 'Reminder';
GO

IF OBJECT_ID ('Purchasing.LowCredit','TR') IS NOT NULL
   DROP TRIGGER Purchasing.LowCredit;
GO
⦁    This trigger prevents a row from being inserted in the Purchasing.PurchaseOrderHeader table
⦁    When the credit rating of the specified vendor is set to 5 (below average).

CREATE TRIGGER Purchasing.LowCredit ON Purchasing.PurchaseOrderHeader
AFTER INSERT
AS
IF EXISTS (SELECT *
           FROM Purchasing.PurchaseOrderHeader p
           JOIN inserted AS i
           ON p.PurchaseOrderID = i.PurchaseOrderID
           JOIN Purchasing.Vendor AS v
           ON v.BusinessEntityID = p.VendorID
           WHERE v.CreditRating = 5
          )
BEGIN
RAISERROR ('A vendor''s credit rating is too low to accept new
purchase orders.', 16, 1);
ROLLBACK TRANSACTION;
RETURN
END;
GO
⦁    This statement attempts to insert a row into the PurchaseOrderHeader table
⦁    For a vendor that has a below average credit rating.
⦁    The AFTER INSERT trigger is fired and the INSERT transaction is rolled back.

INSERT INTO Purchasing.PurchaseOrderHeader (RevisionNumber, Status, EmployeeID,
VendorID, ShipMethodID, OrderDate, ShipDate, SubTotal, TaxAmt, Freight)
VALUES (2,3,261,1652,4,GETDATE(),GETDATE(),44594.55,3567.564,1114.8638 );
GO

Even and Odd Programs

C program to check odd or even using modulus operator

#include <stdio.h>
 
int main()
{
   int n;
 
   printf("Enter an integer\n");
   scanf("%d", &n);
 
   if (n%2 == 0)
      printf("Even\n");
   else
      printf("Odd\n");
 
   return 0;
}
 

C program to check odd or even using bitwise operator

#include <stdio.h>
 
int main()
{
   int n;
 
   printf("Enter an integer\n");
   scanf("%d", &n);
 
   if (n & 1 == 1)
      printf("Odd\n");
   else
      printf("Even\n");
 
   return 0;
}
 

Find odd or even using conditional operator

#include <stdio.h>
 
int main()
{
   int n;
 
   printf("Input an integer\n");
   scanf("%d", &n);
 
   n%2 == 0 ? printf("Even\n") : printf("Odd\n");
 
   return 0;
}
 

C program to check odd or even without using bitwise or modulus operator

#include <stdio.h>
 
int main()
{
   int n;
 
   printf("Enter an integer\n");
   scanf("%d", &n);
 
   if ((n/2)*2 == n)
      printf("Even\n");
   else
      printf("Odd\n");
 
   return 0;
}
 
 
 

Thursday, 15 May 2014


C Program to implement prims algorithm using greedy method
#include<stdio.h>
#include<conio.h>

int n, cost[10][10];

void prim()
{
 int i,j,k,l,x,nr[10],temp,min_cost=0,tree[10][3];
 /* For first smallest edge */
 temp=cost[0][0];
 for(i=0;i< n;i++)
 {
     for(j=0;j< n;j++)
     {
        if(temp>cost[i][j])
        {
        temp=cost[i][j];
        k=i;
        l=j;
        }
     }
 }
 /* Now we have fist smallest edge in graph */
 tree[0][0]=k;
 tree[0][1]=l;
 tree[0][2]=temp;
 min_cost=temp;

 /* Now we have to find min dis of each 
vertex from either k or l
by initialising nr[] array 
*/

 for(i=0;i< n;i++)
    {
    if(cost[i][k]< cost[i][l])
        nr[i]=k;
    else
        nr[i]=l;
    }
 /* To indicate visited vertex initialise nr[] for them to 100 */
 nr[k]=100;
 nr[l]=100;
 /* Now find out remaining n-2 edges */
 temp=99;
 for(i=1;i< n-1;i++)
    {
    for(j=0;j< n;j++)
       {
       if(nr[j]!=100 && cost[j][nr[j]] < temp)
           {
           temp=cost[j][nr[j]];
           x=j;
           }
       }
 /* Now i have got next vertex */
 tree[i][0]=x;
 tree[i][1]=nr[x];
 tree[i][2]=cost[x][nr[x]];
 min_cost=min_cost+cost[x][nr[x]];
 nr[x]=100;

 /* Now find if x is nearest to any vertex 
than its previous near value */

    for(j=0;j< n;j++)
    {
    if(nr[j]!=100 && cost[j][nr[j]] > cost[j][x])
         nr[j]=x;
    }
    temp=99;
 }
 /* Now i have the answer, just going to print it */
 printf("n The min spanning tree is:- ");
 for(i=0;i< n-1;i++)
    {
    for(j=0;j< 3;j++)
           printf("%d", tree[i][j]);
    printf("n");
    }

 printf("n Min cost:- %d", min_cost);
}

//////////////////////////////////////////////
void main()
{
 int i,j;
 clrscr();

 printf("n Enter the no. of vertices:- ");
 scanf("%d", &n);

 printf ("n Enter the costs of edges in matrix form:- ");
 for(i=0;i< n;i++)
     for(j=0;j< n;j++)
          scanf("%d",&cost[i][j]);

 printf("n The matrix is:- ");
    for(i=0;i< n;i++)
    {
      for(j=0;j< n;j++)
           printf("%dt",cost[i][j]);
      printf("n");
    }
 prim();
 getch();
}

Output :
Enter the no. of vertices:- 3

 Enter the costs of edges in matrix form:-
99 2 3
2 99 5
3 5 99

The matrix is:-
99      2       3
2       99      5
3       5       99

The min spanning tree is:-
0  1  2
2  0  3

Min cost:- 5

Area of Square in C Program

 

C Program for Beginners : Area of Square

#include<stdio.h>
#include<math.h>

int main()
{
int side;
float area,r_4;

r_4 = sqrt(3) / 4 ;

printf("nEnter the Length of Side : ");
scanf("%d",&side);

area = r_4 * side * side ;

printf("nArea of Equilateral Triangle : %f",area);
return(0);
}
Output :
Enter the Length of Side : 5
Area of Equilateral Triangle : 10.82

Properties of Equilateral Triangle :

  1. Equilateral triangle is a triangle in which all three sides are equal .
  2. All angles are of measure 60 degree
  3. A three-sided regular polygon

Formula to Calculate Area of Equilateral Triangle :


Explanation of Program :

First of all we have to find the value of Root 3 / 2. So we have used following statement to compute the value.
r_4 = sqrt(3) / 4 ;
Now after computing the value of the Root 3 by 2 we have multiplied it by Square of the side.
area = r_4 * side * side ;

Tuesday, 13 May 2014

Area of Scalene Triangle

 

C Program to Find Area of Scalene Triangle :

#include<stdio.h>

int main()
{
int s1,s2,angle;
float area;

printf("nEnter Side1 : ");
scanf("%d",&s1);

printf("nEnter Side2 : ");
scanf("%d",&s2);

printf("nEnter included angle : ");
scanf("%d",&angle);

area = (s1 * s2 * sin((M_PI/180)*angle))/2;

printf("nArea of Scalene Triangle : %f",area);
return(0);
}

Output :

Enter Side1 : 3
Enter Side2 : 4
Enter included angle : 30
Area of Scalene Triangle : 3.000000

C Program for Beginners : Area of Scalene Triangle

C Program to find Area of Scalane Triangle

Properties of Scalene Triangle :

  1. Scalene Triangle does not have sides having equal length.
  2. No angles of Scalene Triangle are equal.
  3. To calculate area we need at least two sides and the angle included by them.

Formula to Find Area of Scalene Triangle :


Explanation and Program Logic :

Part 1 : M_PI

sin((M_PI/180)*angle)
  • It is Constant defined in math.h Header File
  • It contain value = 3.14 in short instead of writing 3.14 write directly M_PI.

Part 2 : ( M_PI / 180 ) * angle

  • We are accepting angle in degree.
  • C function sin takes argument in radian , so to convert angle into radian we are purposefully writing above statement.

Part 3 : sin function

  • It computes sine of an angle

C Program For Area And Circle

 

C Program to find area and circumference of circle

#include<stdio.h>

int main()
{
int rad;
float PI=3.14,area,ci;

printf("nEnter radius of circle: ");
scanf("%d",&rad);

area = PI * rad * rad;
printf("nArea of circle : %f ",area);

ci = 2 * PI * rad;
printf("nCircumference : %f ",ci);

return(0);
}

Output :

Enter radius of a circle : 1
Area of circle : 3.14
Circumference  : 6.28

Explanation of Program :

In this program we have to calculate the area and circumference of the circle. We have following 2 formulas for finding circumference and area of circle.

Area of Circle = PI * R * R

and

Circumference of Circle = 2 * PI * R

In the above program we have declared the floating point variable PI whose value is defaulted to 3.14.We are accepting the radius from user.

printf("nEnter radius of circle: ");
scanf("%d",&rad);