Sunday, 7 February 2021

WAP in C++ to find the sum of two compatible matrices.

 //To find the sum of two compatible matrices

#include<iostream>

using namespace std;

main()

{

    int r1,c1,r2,c2,i,j,sum[50][50];

    cout<<"Enter the order of the first matrix\n";

    cin>>r1>>c1;

    int arr1[r1][c1];

    cout<<"Enter the elements of the first matrix\n";

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

    {

        for(j=0;j<c1;j++)

        cin>>arr1[i][j];

    }

    

    cout<<"Enter the order of the second matrix\n";

    cin>>r2>>c2;

    int arr2[r2][c2];

    cout<<"Enter the elements of the second matrix\n";

    

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

    {

        for(j=0;j<c2;j++)

        cin>>arr2[i][j];

    }

    

    if(r1==r2&&c1==c2)

    {

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

    {

      for(j=0;j<c1;j++)

      sum[i][j]=arr1[i][j]+arr2[i][j];

    }

    cout<<"The resultant matrix is:\n";

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

    {

        for(j=0;j<c1;j++)

        cout<<sum[i][j]<<" ";

        cout<<"\n";

    }

    }

    else 

    cout<<"Matrices are not compatible …";

}

Friday, 5 February 2021

Program to find factorial of a number using class in C++

 #include<iostream>

using namespace std;

class Factorial

{

    public:

    int f=1,n,i;

    

   void Fact()

   {

       cout<<"Enter a Number\n";

       cin>>n;

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

       f*=i;

       cout<<"Factorial of "<<n<<" is "<<f;

   }

};

    main()

    {

        Factorial obj;

        obj.Fact();

    }

Program to find sum of 2 numbers using class in C++.

 #include<iostream>

using namespace std;

class Sum

{

    public:

    int a,b,c;

    

    void input()

    {

        cout<<"Enter 2 Numbers\n";

        cin>>a>>b;

    }

    

    void Calculate()

    {

        c=a+b;

    }

    

    void Display()

    {

        cout<<"Sum = "<<c;

    }

};

    main()

    {

        Sum obj;

        obj.input();

        obj.Calculate();

        obj. Display();

    }

Tuesday, 2 February 2021

PROGRAM FOR DEFINITION OF FUNCTION in C++

#include<iostream>

using namespace std;

main()

{

  int SUM(int,int);

  int a=10,b=20,sum;

  sum=SUM(a,b);

  cout<<"Sum of 2 numbers = "<<sum;

}

int SUM(int x,int y)

{

  return(x+y);

}


Java Program to count the total number of characters in a string

 //Java Program to count the total number of characters in a string import java.util.*; public class CountChars {     public static void mai...