Thursday, 12 May 2016

C++ Program of Array to Calculate Sum and Average of given Numbers

// This program calculates the average of any number of numbers.
// Using the for structure
#include <iostream>
using namespace std;
#include <iostream>
 
int main()
{
   int n, count;
   float x, sum, avg;
 
   sum = 0;
   cout << "How many numbers?  ";
   cin >> n;
   int size= n;
   int array[size];
   for (count=1; count<=n; count++){
       cout << "Enter Number:  ";
         cin >> array[n];
        sum = sum + array[n];
   }  //end for
   cout << "The sum is " << sum << endl;
   avg = sum / n;
   cout << "The average is  " << avg << endl;
   system("pause");
   return 0;   //successful termination

 }
Share:

C++ Program of Array to Calculate Average of Numbers Using Arrays

#include <iostream>
using namespace std;
int main(){
    int n, i;
    float num[100], sum=0.0, average;
    cout << "Enter the numbers of data: ";
    cin >> n;
    while (n>100 || n<=0)
    {
        cout << "Error! number should in range of (1 to 100)." << endl;
        cout << "Enter the number again: ";
        cin >> n;
    }
   for(i=0; i<n; ++i)
   {
      cout << i+1 << ". Enter number: ";
      cin >> num[i];
      sum+=num[i];
   }
   average=sum/n;
   cout << "Average = " << average;
   return 0;
}
Share:

Monday, 9 May 2016

C++ Program to Find Even and Odd Elements in Array

#include<iostream.h>
#include<conio.h>

 void main()
  {
   int arr[20],even[20],odd[20],i,j=0,k=0,no;
   clrscr();
   cout<<"How Size of Array: ";
   cin>>no;
   cout<<"Enter any "<<no<<" elements in Array: ";
   for(i=0; i<no;i++)
   {
   cin>>arr[i];
   }
   for(i=0; i<no;i++)
   {
   if(arr[i]%2==0)
   {
    even[j]=arr[i];
    j++;
   }
   else
   {
   odd[k]=arr[i];
   k++;
   }
   }
  cout<<"\nEven Elements: ";
  for(i=0; i<j ;i++)
   {
    cout<<even[i]<<"  ";
   }
  cout<<"\nOdd Elements: ";
  for(i=0; i<k; i++)
   {
    cout<<odd[i]<<"  ";
   }
  getch();
  }
Share:

C++ Program Of Calculator

#include <iostream>
using namespace std;

int addition      (int num1, int num2);
int subtraction   (int num1, int num2);
int division      (int num1, int num2);
int multiplication(int num1, int num2);



int main()
{
    char option;
    int num1, num2, result;

    cout<<"\n\t\t\tSimple calculator"<<endl;
    do
    {

    cout<<"Enter + for Addtion\nEnter - for Subtraction\n
    Enter / for Division\nEnter * for Multiplication\n
    Enter E to EXIT\n";
    cout<<"\n\t\t\tEnter Option: ";
    cin>>option;

     if(option!='e'&&option!='E')
     {
     cout<<"\n\t\t\tEnter First Number: ";
     cin>>num1;
     cout<<"\n\t\t\tEnter Second Number: ";
     cin>>num2;
     }

    if(option!='+'&&option!='-'&&option!='/'
      &&option!='*'&&option!='e'&&option!='E')
    {
       cout<<"\n\t\t\tSELECT the VALID OPTION\n";
    }
    else if(option=='+')
    {
       result=addition(num1,num2);
       cout<<"\n\t\t\t"<<num1<<" + "<<num2<<" = "<<result<<endl;
    }
    else if(option=='-')
    {
       result=subtraction(num1,num2);
       cout<<"\n\t\t\t"<<num1<<" - "<<num2<<" = "<<result<<endl;
    }
    else if(option=='/')
    {
       result=division(num1,num2);
       cout<<"\n\t\t\t"<<num1<<" / "<<num2<<" = "<<result<<endl;

    }
    else if(option=='*')
    {
          result=multiplication(num1,num2);
       cout<<"\n\t\t\t"<<num1<<" X "<<num2<<" = "<<result<<endl;

    }



    }while(option!='e'&&option!='E');


 cout<<"\n\t\t\tProgram EXIT Successfully.......\n\t\t\t";
    return 0;
}
int addition(int num1, int num2)
{
    return (num1+num2);
}

int subtraction(int num1, int num2)
{

    return (num1-num2);
}

int division(int num1, int num2)
{
      if(num2==0)
      {
          cout<<"\n\t\t\tDivide by ZERO not allowed :"<<endl;
          return 0;
      }
    return (num1/num2);
}

int multiplication(int num1, int num2)
{
    return (num1*num2);
}

Share:

C++ Program of detecting a keypress and ASCII code

  1. #include<iostream>
  2. #include<conio.h>
  3. using namespace std;
  4. int main()
  5. {
  6.     char key_press;
  7.     int ascii_value;
  8.     cout<<"\n\t\t\tPress Any Key To Check  Its ASCIValue\n\n\t\t\tPress ESC to EXIT\n\n\n";
  9.     while(1)
  10.     {
  11.     key_press=getch();
  12.     ascii_value=key_press;
  13.     if(ascii_value==27) // For ESC
  14.      break;
  15.    cout<<"\t\t\tKEY Pressed-> \" "<<key_press<<" \" Ascii Value =  "<<ascii_value<<"\n\n";
  16.     }
  17.     return 0;
  18. }
Share:

Sunday, 8 May 2016

C++ Program to Find Max and Min Number in Array

#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;
int main()
{
const int size=50;
// array with size of 50
int array[size];
// for random numbers
srand(time(0));
    for(int i=0;i<size;i++){
    // Initializing Array number is less than 100
    array[i]=rand()%100;
    // Displaying array value
    cout<<array[i]<<endl;
    }
            // initializing max, min
                int max=array[0];
                int min=array[0];
/* scanning array to find
   minimum and maximum
   number  */
for(int i=0;i<size;i++){
    // finding minimum number in array
      if(min>array[i]){
         min=array[i];
      }
      //finding maximum number in array
      if(max<array[i]){
      max=array[i];
      }
}
       // displaying output
        cout<<"Maximum Number is :"<<max<<endl;
        cout<<"Minimum Number  is:"<<min<<endl;

return 0;
}

Share:

C++ Program of Multiplying Two Matrices using 2D Array

#include<iostream>
using namespace std;
int main(){
//Using const int for array size
    const int row=2,col=2;
// if not use const error found
cout<<"Size of Matrices : "<<row<<" X "<<col<<endl;
cout<<"Enter Value For FirstMatrix Matrix:"<<endl;

    int firstMatrix[row][col];
    int secondMatrix[row][col];
    int resultantMatrix[row][col], var;

int i,j;
    for( i=0;i<row;i++){
        cout<<"Enter value for row number: "<<i+1<<endl;
        for( j=0;j<col;j++){
            cin>>firstMatrix[i][j];
        }
    }
cout<<"\n\n\nEnter Value For SecondMatrix Matrix:"<<endl;
    for( i=0;i<row;i++){
        cout<<"Enter value for row number: "<<i+1<<endl;
        for( j=0;j<col;j++){
            cin>>secondMatrix[i][j];
        }
    }
var=0;
        // ResultantMatrixant Matrix
        for( i=0;i<row;i++){
            for( j=0;j<col;j++){
                for(int k=0;k<row;k++){
                   var=var+(firstMatrix[i][k]*secondMatrix[k][j]);
                   cout<<var<<endl;
                    }
                 resultantMatrix[i][j]=var;
                 var=0;
            }
        }
cout<<"\n\n\t\tResultant Matrix:"<<endl;

    for( i=0;i<row;i++){
        cout<< endl;
        for( j=0;j<col;j++){
            cout<<"\t\t"<<resultantMatrix[i][j]<<"    ";
        }
    }
return 0;
}

Share:

C++ Program to add two matrix using 2-D arrays

#include<iostream>
using namespace std;

int main(){

//Using const int for array size
    const int row=2,col=2;
// if not use const error found

cout<<"Size of Matrices : "<<row<<" X "<<col<<endl;
cout<<"Enter Value For First Matrix:"<<endl;

int first[row][col], second[row][col];

int i,j;
for( i=0;i<row;i++){

    cout<<"Enter value for row number: "<<i+1<<endl;
    for( j=0;j<col;j++){
        cin>>first[i][j];
    }

}


cout<<"\n\n\nEnter Value For Second Matrix:"<<endl;
for( i=0;i<row;i++){

    cout<<"Enter value for row number: "<<i+1<<endl;

    for( j=0;j<col;j++){
        cin>>second[i][j];

    }

}

// Resultant Matrix
for( i=0;i<row;i++){

    for( j=0;j<col;j++){
        first[i][j]=first[i][j]+second[i][j];

    }

}

cout<<"\n\n\t\tResultant Matrix:"<<endl;
for( i=0;i<row;i++){
    cout<< endl;
    for( j=0;j<col;j++){
        cout<<"\t\t"<<first[i][j]<<"    ";

    }

}
return 0;
}
Share:

C++ Program to find the Area and Perimeter of a Rectangle



#include<iostream>

using namespace std;

int main()

{

    int width,height,area,perimeter;

    cout<<"Enter  Width of Rectangle = ";

    cin>>width;

    cout<<"Enter  Height of Rectangle = ";

    cin>>height;

    area=height*width;

    cout<<"Area of Rectangle ="<<area<<endl;

    perimeter=2*(height+width);

cout<<" Perimeter of rectangle are = "<<perimeter<<endl;

return 0;

}


Share:

C++ Program to Find Reverse of Number

#include<iostream>
using namespace std;

int main() {
int number, reverse = 0;
cout<<"Input a Number to Reverse and press Enter: ";
 cin>> number;     // Taking Input Number in variable number

   for( ; number!= 0 ; )
   {
      reverse = reverse * 10;
      reverse = reverse + number%10;
      number = number/10;
   }
   cout<<"New Reversed Number is:  "<<reverse;
   return 0;

}






Share:

C++ Program to Find Greatest Common Divisor (GCD) of two numbers

#include<iostream>
using namespace std;

int main() {

int first_number;
cout<<"Enter First Number : ";cin>>first_number;

int  second_number;
cout<<"Enter Second Number: ";cin>>second_number;

int  gcd;
for(int i=1;i<=first_number&&i<=second_number;i++){


     if(first_number%i==&& second_number%i == ){

                     gcd=i;

   }

}

cout<<"Greatest Common Divison (GCD):"<<gcd<<endl;
return 0;
}
Share: