You are on page 1of 6

1.

Write a program that inputs a series of integers and passes them one at a time to
function even, which uses the modulus operator to determine whether an integer is
even. The function should take an integer argument and return true if the integer is
even and false otherwise.

#include <iostream.h>

bool even( int a )


{
return !( a % 2 );
}
int main()
{
int n, x;
cout<<"enter n"<<endl;
cin>>n;

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


cout << "Enter an integer: ";
cin >> x;

if ( even( x ) )
cout << x << " is an even integer\n\n";
else
cout << x << " is an odd integer\n\n";
}
cout << endl;
return 0;
}
2. Write a program to store and calculate the sum of 5 numbers entered by the user using
array.

#include <iostream.h>
int sum (int a[100],int n)
{

int sum = 0;

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


{

sum += a[i];
}

return sum;
}
int main()
{

int n, numbers[100], s = 0;

cout << "Enter count of numbers: ";


cin>>n;
cout << "Enter numbers: ";

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


{
cin >> numbers[i];

}
s=sum(numbers,n);

cout << "Sum = " << s << endl;

return 0;
}

3. Write C++ Program that find factorial using function .

#include <iostream.h>

int fact (int n)

int f = 1;

for (int i = n; i >1; i--)

f *= i;

return f;

int main()

int n, fa;
cout << "Enter a number : ";

cin>>n;

fa=fact(n);

cout << "factorial = " << fa << endl;

return 0;

4. Write a program that add Two Matrices using Multi-dimensional Arrays using
function .

#include <iostream.h>
void sum_two_matrix( int m1[100][100],int m2[100][100],int
s[100][100],int r,int c)
{

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


for(int j = 0; j < c; j++)
s[i][j] =( m1[i][j] + m2[i][j]);

int main()
{
int r, c, a[100][100], b[100][100], sum[100][100], i,
j;

cout << "Enter number of rows (between 1 and 100): ";


cin >> r;

cout << "Enter number of columns (between 1 and 100):


";
cin >> c;

cout << endl << "Enter elements of 1st matrix: " <<
endl;

// Storing elements of first matrix entered by user.


for(i = 0; i < r; i++)
for(j = 0; j < c; j++)
{
cout << "Enter element a" << i + 1 << j + 1 << "
: ";
cin >> a[i][j];
}

// Storing elements of second matrix entered by user.


cout << endl << "Enter elements of 2nd matrix: " <<
endl;
for(i = 0; i < r; i++)
for(j = 0; j < c; j++)
{
cout << "Enter element b" << i + 1 << j + 1 << "
: ";
cin >> b[i][j];
}

cout<<sizeof(sum);
sum_two_matrix( a, b, sum,r, c);

// Displaying the resultant sum matrix.


cout << endl << "Sum of two matrix is: " << endl;
for(i = 0; i < r; i++)
{
for(j = 0; j < c; j++)

{
cout << sum[i][j] << " ";
}
cout<<"\n";
}
return 0;
}

You might also like