Wednesday, August 21, 2013

Program to illustrate the working of new and delete operators

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i;
cout<<"Enter the number of students in the class : ";
cin>>n;
int *p=new int[n];
int *q=new int[n];
cout<<"Input the class numbers : ";
for(i=0;i<n;i++)
cin>>*(p+i);
cout<<"Input the marks : ";
for(i=0;i<n;i++)
cin>>*(q+1);
cout<<"The list of students with marks : ";
for(i=0;i<n;i++)
cout<<"\n"<<*(p+i)<<"\t"<<*(q+i);
delete [] p;
delete [] q;
getch();
}

For more details about pointers click here

Wednesday, August 7, 2013

Program for Newton's Law of Gravitation

In this program user inputs the mass of two bodies and the distance between them and calculate and output the Force of attraction between two masses according to Newton's law of gravitation.

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
float f,m1,m2,r;
cout<<"Enter the value of two masses in Kg : ";
cin>>m1>>m2;
cout<<"Enter the distance between two masses in meters : ";
cin>>r;
f=(6.67*pow(10.0,-11.0)*m1*m2)/(r*r);
cout<<"The force of attraction between two masses = "<<f<<" Newton ";
getch();
}

Program to delete an element from an array

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int s,n,i,p,a[50];
clrscr();
cout<<"Enter number of students : ";
cin>>n;
cout<<"Enter the marks : ";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"Enter the marks to be deleted : ";
cin>>s;
for(i=0;i<n;i++)
if(a[i]==s)
    {
     p=i;
     break;
    }
for(i=p;i<n-1;i++)
a[i]=a[i+1];
cout<<"The new mark list is : ";
for(i=0;i<n-1;i++)
cout<<a[i];
getch();
}

Sunday, August 4, 2013

Program to create a square matrix and find the sum of diagonal elements

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[20][20],sum=0,m,n,i,j;
cout<<"Enter the size : ";
cin>>m>>n;
if(m==n)
{
cout<<"Enter the elements : ";
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
if(i==j)
sum+=a[i][j];
}
}
cout<<sum;
}
else
cout<<"Not a square matrix";
getch();
}

Friday, August 2, 2013

Program to search element in an array - Linear search

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int n,p=-1;
char a[100],s;
cout<<"Enter the line of text : ";
gets(a);
cout<<"Enter the character to be searched : ";
cin>>s;
for(int i=0;a[i]!='\0';++i)
if(a[i]==s)
p=i;
if(p>=0)
cout<<"Character found in "<<p<<"th position";
else
cout<<"Character not found ";
getch();
}

Program to find out the volume of cone


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float r,h,v;
cout<<"Enter the base radius in cm: ";
cin>>r;
cout<<"Enter the height in cm: ";
cin>>h;
v=(1/3)*3.14*r*r*h;
cout<<"Volume of cone is : "<<v<<" "<<"cc";
getch();
}

Output of this program will be :

Enter the base radius in cm :
25
Enter the height in cm :
14.43
Volume of cone is : 9444.4 cc