Showing posts with label basic programs. Show all posts
Showing posts with label basic programs. Show all posts

average of n numbers

Tuesday, September 1, 2009

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x[50],i,n,sum=0;
float avg;
cout<<"value of n=";
cin>>n;
cout<<"enter the elements:";
for(i=0;i<n;i++)
{
 cin>>x[i];
 }
for(i=0;i<n;i++)
{
 cout<<"x["<<i<<"]="<<x[i]<<endl;
 sum=sum+x[i];
 }
 cout<<"avg=";
 avg=sum/n;
 cout<<avg;
 getch();
}

calulate gain in amount by adding 11 % interst per year within 10 years

#include<stdio.h>
#include<conio.h>
#define interest 11
#define period 10
void main()
{
 clrscr();
 float amount=5000;
 int duration=0;
 while(duration<=period)
 {
  printf("%2d           %8.2f\n",duration,amount);
  amount=amount+amount*interest/100;
  duration++;
  }
  getch();
}

evalute z+z*z/2 +z*z*z/3 +z*z*z*z/4

   #include<iostream.h>
   #include<conio.h>
   #include<math.h>
   void main()
   {
    float x,y,z;
    clrscr();
    cout<<"enter the value of x ";
    cin>>x;
    z=x/(x+1);
    y=z+pow(z,2)/2+pow(z,3)/3+pow(z,4)/4;
    cout<<"value of y=";
    cout<<y;
    getch();
  }

range of integer value

#include<iostream.h>
#include<conio.h>
#include<limits.h>
void main()
{
 clrscr();
 cout<<"minimum value of int="<<INT_MIN<<"\n";
 cout<<"maximum value of int="<<INT_MAX<<"\n";
 cout<<"maximum value of unsigned int="<<UINT_MAX<<"\n";
 cout<<"maximum value of long int="<<LONG_MAX<<"\n";
 cout<<"minimum value of long int="<<LONG_MIN<<"\n";
 getch();
 }

how to use recursion

#include<iostream.h>
#include<conio.h>
void f(int);
void main()
{
  clrscr();
  f(5);
  getch();
}
void f(int x)
{ cout<<"hello"<<'\t';
  if(x>0)
  f(x-1);
  cout<<"world"<<'\t';
}

input, output two dimensional array

#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
 int x[10][10],m,n;
 cout<<"number of rows and colomns:"<<endl;
 cin>>m>>n;
 cout<<"enter the elements";
 for(int i=0;i<m;i++)

  for(int j=0;j<n;j++)//if we put braces here then int j is defined
            // in another line otherwise not
  cin>>x[i][j];

 for(i=0;i<m;i++)
 {
  for(j=0;j<n;j++)
  cout<<x[i][j]<<"\t";
  cout<<endl;
  }
  getch();
  }

importance of getche()

#include<stdio.h>
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char z;
z=getche();
cout<<z;
getch();
}

importance of getchar()

#include<stdio.h>
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char xy;
xy=getchar();
cout<<xy;
getch();
}

importance of getch() & clrscr()

#include<iostream.h>
#include<conio.h>
void main()
{
  cout<<"1"<<endl;
  getch();
  clrscr();
  cout<<"2"<<endl;
  getch();
  clrscr();
  cout<<"3"<<endl;
  getch();
  clrscr();
  cout<<"4";
  getch();
  clrscr();
  cout<<"5"<<endl;
  getch();
  clrscr();
  }

print a heart

#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
 cout<<"                   *  *         *  *         "<<endl;
 cout<<"               *         *   *        *     "<<endl;
 cout<<"              *           * *           *    "<<endl;
 cout<<"              *            *            *   "<<endl;
 cout<<"              *                         *   "<<endl;
 cout<<"               *                       *    "<<endl;
 cout<<"                *                     *     "<<endl;
 cout<<"                 *                   *      "<<endl;
 cout<<"                  *                 *       "<<endl;
 cout<<"                    *             *         "<<endl;
 cout<<"                      *         *           "<<endl;
 cout<<"                        *     *             "<<endl;
 cout<<"                          * *               "<<endl;
 cout<<"                           *                 "<<endl;
 getch();
 }

print a car

#include<iostream.h>
#include<conio.h>
void main()
{
   clrscr();
   cout<<"    ^    "<<endl;
   cout<<"   (*)   "<<endl;
   cout<<"   /^\\  "<<endl;
   cout<<"  /~~~\\ "<<endl;
   cout<<" /-----\\"<<endl;
   cout<<" |DHAMI| "<<endl;
   cout<<" | CAR | "<<endl;
   cout<<"  -----  "<<endl;
   cout<<"()     ()"<<endl;
   getch();
 }

fundamentals of signed and unsigned numbers

#include<iostream.h>
 #include<conio.h>
 void main()
 {
  clrscr();
  int i=32000;
  cout<<i<<endl;
  i=40000;
  cout<<i<<endl;
  signed k=40000;
  cout<<k<<endl;
  unsigned j=40000;
  cout<<j<<endl;
  float l=50000;
  cout<<l<<endl;
  l=555.57;
  cout<<l<<endl;
  getch();
  }

sum of two numbers

#include<iostream.h>
#include<conio.h>
void main()
{
 int a,b,sum;
 clrscr;
 cout<<"enter the value of a";
 cin>>a;
 cout<<"enter the value of b";
 cin>>b;
 sum=a+b;
 cout<<"sum=";
 cout<<sum;
 getch();
 }