C++ Program of Sum, Multiplication, Average Program in Array

In this program we will learn how to calculate sum, multiply and average of numbers stored in array.

The code is given below.

//Code Starts

#include<iostream>

#include<conio.h>

using namespace std;

int main()

{

  int n,a[n],sum=0,mul=1;

  float avg;

 

  cout<<“Enter Size of Array : “;

  cin>>n;

 

  for(int i=0;i<n;i++)

  {

    cout<<“Enter Number at “<<i<<” index : “;

    cin>>a[i];

  

    sum=sum+a[i];

    mul=mul*a[i];

  }

 cout<<“Sum = “<<sum<<endl;

 cout<<“Multiplication = “<<mul<<endl;

 avg=sum/n;

 cout<<“Average = “<<avg;

 getch();

}

//Code Ends

OUTPUT:

Advertisements

C++ structure program of person

In this program we will create structure of person enter data in structure and also show the entered data.

The code is given below.

//Code Starts

#include<iostream>

#include<conio.h>

using namespace std;

struct person

{

char name[15];

char age[2];

char father_name[20];

char adress[40];

char phone[11];

};

int main()

{

person p;

cout<<“Enter Name “<<endl;

cin>>p.name;

cout<<“Enter Father’s Name “<<endl;

cin>>p.father_name;

cout<<“Enter Age “<<endl;

cin>>p.age;

cout<<“Enter Adress “<<endl;

cin>>p.adress;

cout<<“Enter Mobile Number “<<endl;

cin>>p.phone;

cout<<“Your Entered Data :”<<endl;

cout<<p.name<<endl;

cout<<p.age<<endl;

cout<<p.father_name<<endl;

cout<<p.adress<<endl;

cout<<p.phone<<endl;

getch();

}

//Code Ends

OUTPUT:

C++ program of bubble sorting array

In this program we will learn how  to sort elements of array in bubble sorting order.

The code is given below.

//Code starts

#include<iostream>

#include<conio.h>

using namespace std;

int main()

{

int a[50],n,i,j,temp;

cout<<“Enter the size of array: “;

cin>>n;

for(i=0;i<n;++i)

{

cout<<“Enter number at index [“<<i<<“] : “;

cin>>a[i];

}

for(i=1;i<n;++i)

{

for(j=0;j<(n-i);++j)

{

if(a[j]>a[j+1])

{

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

}

}

}

cout<<“Array after bubble sort:”;

for(i=0;i<n;++i)

cout<<” “<<a[i];

getch();

}

//Code ends

OUTPUT:

C++ Program to find ASCII Code

In this C++ program we will find ASCII value of an alphabet and also find an ASCII alphabet of given value

//Code starts

#include <iostream>
#include<conio.h>
using namespace std;
int main()
{
 char ascii;
 int numeric;
 cout<< “Give character: “;
 cin >> ascii;
 cout << “Its ascii value is: ” << (int) ascii << endl;
 cout << “Give a number to convert to ascii: “;
 cin >> numeric;
 cout << “The ascii value of ” << numeric << ” is ” << (char) numeric;
 getch ();
}
//Code Ends
 
OUTPUT:

C++ program of finding area of circle

In this C++ program we will find area of circle by entering its radius or diameter

//Code Starts

#include <iostream>

#define PI 3.14159

using namespace std;

float AreaOfCircle(float radius);

float AreaWithDiameter(float diameter);

int main()

{

    float radius,diameter,circleArea;

    char choice=’0′;

    cout<<“\n\t\t\tFind Area Of Circle:”<<endl;

    for(;choice!=’1’&&choice!=’2′;)

        {

         cout<<“\nEnter 1 to Enter Radius OR 2 to Enter Diameter: “;

         cin>>choice;

         if(choice!=’1’&&choice!=’2′)

         cout<<“\n\t\tEnter a VALID Option “;

        }

    if(choice==’1′)

    {

    cout <<“\n\t\tEnter Radius To Find Area: “;

    cin>>radius;

    circleArea=AreaOfCircle(radius);

    }

    else if(choice==’2′)

    {

    cout <<“\n\t\tEnter Diameter To Find Area: “;

    cin>>diameter;

    circleArea=AreaWithDiameter(diameter);

    }

    cout<<“\n\n\t\tArea of Circle is:->> “<<circleArea<<endl;

    return 0;

}

float AreaOfCircle(float radius)

{

    return (PI*(radius*radius));

}

float AreaWithDiameter(float diameter)

{

    return (AreaOfCircle(diameter/2));

}

//Code Ends

OUTPUT: