Write a C++ Program to Multiply every member by k using class

By | 01.01.2017

Multiply every member by k using class


Write a C++ Program to Multiply every member by k using class. Here’s a Simple Program to Multiply every member by k using class in C++ Programming Language.


What is Class and Objects in C++?


  • The classes are the most important feature of C++ that leads to Object Oriented programming.
  • Class is a user defined data type, which holds its own data members and member functions, which can be accessed and used by creating instance of that class.
  • The variables inside class definition are called as data members and the functions are called member functions.
  • Class is just a blue print, which declares and defines characteristics and behavior, namely data members and member functions respectively. And all objects of this class will share these characteristics and behavior.
  • Objects are instances of class, which holds the data variables declared in class and the member functions work on these class objects.
  • Each object has different data variables. Objects are initialized using special class functions called Constructors.

Below is the source code for C++ Program to Multiply every member by k using class which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/* C++ Program to Multiply every member by k using class  */

#include<iostream>
using namespace std;

class array
{
     public:
         int n;
         void readarray();
         void multiply();
};

void array::readarray()
{
        int a[100];
        cout<<"Enter No. of elements u want :: ";
        cin>>n;
        for(int i=0;i<n;i++)
    {
        cout<<"\nEnter [ "<<i+1<<" ] element :: ";
        cin>>a[i];
    }

}

void array::multiply()
{
        int i,j,a[100],temp[100],k;

        cout<<"\nEnter the no. which u want to multiply :: ";
        cin>>k;

        for(i=0;i<n;i++)
        {
                for(j=0;j<n;j++)
                temp[i]=(a[i]*k);
        }
        cout<<"\nAfter Multiply every member by k = [ "<<k<<" ], Resultant array is :: "<<endl;
        for(int i=0;i<n;i++)
    {
        cout<<"\n [ "<<i+1<<" ] element :: "<<temp[i]<<"\n";
    }

}

int main()
{
        array mul;

        mul.readarray();
        mul.multiply();

        return 0;
}

OUTPUT : :


/* C++ Program to Multiply every member by k using class  */

Enter No. of elements u want :: 6

Enter [ 1 ] element :: 1

Enter [ 2 ] element :: 2

Enter [ 3 ] element :: 3

Enter [ 4 ] element :: 4

Enter [ 5 ] element :: 5

Enter [ 6 ] element :: 6

Enter the no. which u want to multiply :: 5

After Multiply every member by k = [ 5 ], Resultant array is ::

 [ 1 ] element :: 5

 [ 2 ] element :: 10

 [ 3 ] element :: 15

 [ 4 ] element :: 20

 [ 5 ] element :: 25

 [ 6 ] element :: 30

Process returned 0

Above is the source code and output for C++ Program to Multiply every member by k using class which is successfully compiled and run on Windows System to produce desired output.

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach upto you in the short interval.


Thanks for reading the post….

1 3 votes
Article Rating
Subscribe
Notify of
guest

4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
atharv

wrong progran

prince

please write correct program in comment!

Eram

#include <stdio.h>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>

using namespace std;

class Array{
  private:
    int arr[100];
    int size;
  public:
    void input();
    void mult();
     
};

void Array::input(){
  int i;
  cout<<“Enter the size of the array: “;
  cin>>size;

  for(i=0;i<size;i++){
    cout<<“Enter the arr[“<<i<<“] element : “;
    cin>>arr[i]; 
  }
}

void Array::mult(){
  int k;
  cout<<“Enter the number you wish to multiply the element with : “;
  cin>>k;
  int i, temp[100];
  for(i=0;i<size;i++){
    temp[i]=arr[i]*k;
  }
   for(i=0;i<size;i++){
    cout<<“The new array is :”<<temp[i]<<endl;
   }
}

int main()
{
    Array mul;

    mul.input();
    mul.mult();

    return 0;
}

Eram

Enter the size of the array: 3
Enter the arr[0] element : 2
Enter the arr[1] element : 3
Enter the arr[2] element : 1
Enter the number you wish to multiply the element with : 2
The new array is :4
The new array is :6
The new array is :2