C Program to Compute sum of the array elements using pointers !

C Program to Compute sum of the array elements using pointers !

Write a ‘C’ Program to compute the sum of all elements stored in an array using pointers

C Program to compute sum of the array elements using pointers

Program :

#include<stdio.h>
#include<conio.h>
void main()
{
 int a[10];
 int i,sum=0;
 int *ptr;

 printf("Enter 10 elements:n");

 for(i=0;i<10;i++)
    scanf("%d",&a[i]);

 ptr = a;           /* a=&a[0] */

 for(i=0;i<10;i++)
    {
    sum = sum + *ptr;    //*p=content pointed by 'ptr'
    ptr++;
    }

 printf("The sum of array elements is %d",sum);
}

Output :

Enter 10 elements : 11 12 13 14 15 16 17 18 19 20

The sum of array elements is 155

Explanation of Program :

Accept the 10 elements from the user in the array.
for(i=0;i<10;i++)
      scanf("%d",&a[i]);
We are storing the address of the array into the pointer.
ptr = a;
Now in the for loop we are fetching the value from the location pointer by pointer variable. Using De-referencing pointer we are able to get the value at address.
for(i=0;i<10;i++)
    {
    sum = sum + *ptr;
    ptr++;
    }
Suppose we have 2000 as starting address of the array. Then in the first loop we are fetching the value at 2000. i.e
sum = sum + (value at 2000)
    = 0   + 11
    = 11
In the Second iteration we will have following calculation -
sum = sum + (value at 2002)
    = 11  + 12
    = 23
C Program to Calculate Area of Square

C Program to Calculate Area of Square

C Program for Beginners : Area of Square

Shape : Square
   Formula : side * side
Definition :
  1. A plane rectangle with four equal sides andfour right angles 
  2. A four-sided regular polygon
  3. You can compute the area of a square if you know the length of its sides
Program :
#include<stdio.h>
#include<conio.h>
void main()
{
int side,area;
clrscr();  // Clear Screen

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

area = side * side ;

printf("nArea of Square : %d",area);
getch();
}
Output :
Enter the Length of Side : 5
Area of Square : 25
C Program to Calculate Area of Rectangle

C Program to Calculate Area of Rectangle

C Program for Beginners : Area of Rectangle

Shape : Rectangle
formula : area = l * b
Definition
  1. A plane figure with 4 sides and 4 right angles and having Equal Opposite Sides
  2. Adjucent sides makes an angle of 90 degree
  3. You can compute the area of a Rectangle if you know its length and breadth
Program :
#include<stdio.h>
#include<conio.h>

void main()
{
int length,breadth,side;
clrscr();  // Clear Screen

printf("nEnter the Length of Rectangle : ");
scanf("%d",&length);

printf("nEnter the Breadth of Rectangle : ");
scanf("%d",&breadth);

area = length * breadth;

printf("nArea of Rectangle : %d",area);
getch();
}
Output :
Enter the Length of Rectangle  : 5
Enter the Breadth of Rectangle : 4
Area of Rectangle : 20
C Program to Calculate Area of Circle

C Program to Calculate Area of Circle

C Program for Beginners : Area of Circle

Shape : Circle
   Formula : Π * r * r
 Definition :
  1. Ellipse in which the two axes are of equal length
  2. Plane curve generated by one point moving at aconstant distance from a fixed point
  3. You can compute the area of a Circle if you know itsradius.
Program :
#include<stdio.h>
#include<conio.h>
void main()
{
float radius,area;
clrscr();  // Clear Screen

printf("nEnter the radius of Circle : ");
scanf("%d",&radius);

area = 3.14 * radius * radius;

printf("nArea of Circle : %f",area);
getch();
}
Output :
Enter the radius of Circle : 2.0
Area of Circle : 6.14
C Program to Calculate Area of Right angle Triangle

C Program to Calculate Area of Right angle Triangle

C Program for Beginners : Area of Right Angled Triangle

Right angle Triangle
 Definition :
  1. Triangle having one angle of measure 90 degree is called Right angle Triangle.
  2. Sides of Triangle are : base , height , hypotenuse.
  3. You can compute the area of a Tr. if you know its any two sides.

Program :
#include<stdio.h>
#include<conio.h>
void main()
{
int base,height;
float area;
clrscr();  // Clear Screen

printf("nEnter the base of Right Angle Triangle : ");
scanf("%d",&base);

printf("nEnter the height of Right Angle Triangle : ");
scanf("%d",&height);

area = 0.5 * radius * radius;

printf("nArea of Right Angle Triangle : %f",area);
getch();
}
Output :
Enter the base of Right Angle Triangle   : 4
Enter the height of Right Angle Triangle : 8
Area of Right Angle Triangle : 16.0

C Program to Calculate Area of Equilatral Triangle

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 ;