Friday, 27 July 2012

how to find prime number using c++


 This is the source code for finding prime number 
#include<iostream.h>
#include<conio.h>
main()
{
      int n,d,p=1;
      cout<<"enter the number?";
      cin>>n;
      d=2;
      while(d<=n/2)
      {
                   if(n%d==0)
                   p=0;
                   d++;
                   }
                   if(p==0)
                   cout<<"not prime number";
                   else
                   cout<<"prime number";
                   getch();
                   }
      End

Wednesday, 25 July 2012

how to fill and display array using functions

This is the source code of how to fill and display array using functions
                                               
#include<iostream.h>
#include<conio.h>

const int n=9;
int GA[n];


void fillarray(void){
    const int n=9;
    int A[n];
   
    int i=0;
    while(i<=9){
       
        cout<<"value of element"<<i<<"?"<<endl;
        cin>>A[i];
       
        GA[i]=A[i];
        i++;
       
   
    }
   

}


void displayarray(void){

  for(int i=0;i<=9;i++){
     cout<<GA[i]<<endl;
      }   
  }
 



int main()
{
    fillarray();
    displayarray();
    getch();
   
}
End

Sunday, 8 July 2012

how to fill the array of five elements



This is the source code to fill the array of five elements

Code: #include<iostream.h>
#include<conio.h>
main()
{
     int A[5];
     int r;
     for(r=0;r<5;r++)
     {
                     cout<<"enter the value";
                     cin>>A[r];
                     }
      getch();
      }

How to calculate the average of all the values in the array



This is the source code to calculate the average of all the values in the array
#include<iostream.h>
#include<conio.h>
main()
{
      int A[5]={3,6,9,12,15};
      int sum;
      float average;
      sum=A[0]+A[1]+A[2]+A[3]+A[4];
      average=sum/5;
      cout<<"Average="<<average<<endl;
      getch();
      }
End

How to calculate the sum of all values in an array



This is the source code to calculate the sum of all the values in an array
#include<iostream.h>
#include<conio.h>
main()
{
      int A[5]={3,6,9,12,15};
      int sum;
      sum=A[0]+A[1]+A[2]+A[3]+A[4];
      cout<<"sum="<<sum<<endl;
      getch();
      }
End