Sunday, 19 March 2017

MULTIPLICATION OF MATRIX IN C++

Tags

Program : Multiply two matrix in C++.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int sum=0,a1[30][30],a2[30][30],a3[30][30],i,j,k;
cout<<"Enter first Matrix";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>a1[i][j];
}
}
cout<<"Enter element of second matrix";
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>a2[i][j];
}
}
}
cout<<"\nMultiplication of two matrix..\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
sum=0;
for(k=0;k<3;k++)
{
sum=sum+a1[i][k]*a2[k][j];
}
a3[i][j]=sum;
}
//a3[i][j]=sum;
}
cout<<"\nResultant Matrix..\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<m3[i][j]<<" ";
}
cout<<"\n";
}
getch();

}


Wednesday, 15 February 2017

program to find prime number after giving number in C++

Tags
#include<iostream>
using namespace std;
int main ()
{
int i,j,num;
cout<<"Enter number:-";
cin>>num;
cout<<"next prime number:-";
for(i=num+1;i<2000;i++)
{
for(j=2;j<i;j++)
{
if(i%j==0)
{
//cout<<"no prime no:-";
break;
}

}
if(i==j||i==0)
{
cout<<"\t"<<i;
break;
}

return 0;

}
}

program to show prime number in C++.

Tags
//Prime number program in dev C++.

#include<iostream>

using namespace std;

int main()
{

int i,num;
cout<<"Enter number:-\n";
cin>>num;
if(num==1)
{
cout<<"1 is not a prime number and smallest prime number is 2";
}


for(i=2;i<num;i++)
{

if(num%i==0)
{

cout<<"number is not prime number";
break;

}

}
if(num==i)

{

cout<<"number is prime number";

}

return 0;

}

Tuesday, 14 February 2017

swap two number without using third variable in Dev c++

Tags
// program to swap two number without using third variable.

#include<iostream>
using namespace std;
int main()
{
int a,b;
cout<<"\n enter a:-";
cin>>a;
cout<<"\n enter b:-";
cin>>b;
a=a+b;
b=a-b;
a=a-b;
cout<<"\n after swapping: a:-"<<a<<"\n b"<<b;
return 0;

}

Swap two number in Turbo C+

Tags

//swap two numbers using three variables.


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

void main()
{

int a,b,c;
clrscr();

cout<<"a=";
cin>>a;
cout<<"b=";
cin>>b;

c=a;//assigning value in c and c is temporary variable.

a=b;

b=c;//assigning value of c  in b.

cout<<"after swapping a:"<<a<<"\nb:"<<b;

getch();
}

program to check odd or even number in TurboC++

Tags

After doing this program you assure that, how to use if and else condition in programs.

//program to check even or odd no:



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

void main()
{

int no;
clrscr();

cout<<"enter no:-";
cin>>no;

//checking if no is divisible by two or not.
if(no%2==0)

{

cout<<"\n even no";

}
else
{
cout<<"\n odd no";
}

getch();
}










Tuesday, 7 February 2017

Simple Multiplication table in C++

Tags

//Multiplication table in C++ and understand concept of for loop.


#include<iostream.h>
#include<conio.h>
void main()
{
int n,i;

cout<<"\n\t\ This program prints multiplication table:"<<"\n";
cout<<"Enter the value";
cin>>n;
for(i=1;i<=10;i++)
{
cout<<n;
cout<<"*";
cout<<i;
cout<<"=";
cout<<n*i;
cout<<"\n";

}
getch();
}


Monday, 6 February 2017

switch control in C and C++.

Tags

Switch condition


switch condition is used to print multiple value as  a code or showing different code in same programme.

syntax;

switch(expression)
{
case constant:
code;
break;
case constant:
code;
break;
case constant:
code;
break;
default:
code;
}

different cases are used to show different result.
constant are used to identify each code and it is not compulsory to have constant as a number or character we can take constant as a integer or character as we wish but we did not take constant as a float , like 1.2,1.3 etc.
it is also not compulsory to have constant on a sequence.
 we can use switch condition in place of nested-if statement, we can not use nested -if statement in longer programme because it is difficult to read programme.

Saturday, 4 February 2017

Loops in programming languages.

Tags
Loops are used to repeat set of codes or a single code. There are three types of loops explained in below;

1)for loop

    This loop is very commonly used and syntax is very easy.

    syntax;

    for(var initialization; var condition; increament of variable)
    for example;

    for(i=0;i<=10;i++)
    {

      cout<<"";
      or
       printf("");

     }

2)while loop
  
   while loop is very simple and syntax is ;
    while(condition)
{
}
   
    while(x<=10)
     {
}

3)do-while loop
syntax;

do{
printf();
}
while();

  

Friday, 27 January 2017

program to add two complex number using constructor overloading in C++

Tags

Q.6 Write a program to perform addition of two complex numbers using constructor overloading. The first constructor which takes no arguments is used to create objects which are not initialized. Second, which takes one argument is used to initialize real and imaginary parts to equal values and third which takes two arguments is used to initialize real and imaginary parts to two different values.

 //Program to perform addition of two complex numbers using constructor overloading.

# include<conio.h>

# include<iostream.h>

class Complex

{

int real,imag;

public:

   Complex()                         //Default Constructor.

   {

   }

   Complex(int r)                // Parameterised constructor for equal values.

   {

   real=r;

   imag=r;

   }

   Complex(int r,int i)       // Parameterised constructor for different values.

   {

   real=r;

   imag=i;

   }

   Complex(Complex &c)       //Copy Constructor.

   {

    real=c.real;

    imag=c.imag;

   }

   void print()

   {

   cout<<"\n The sum of two complex nos. is "<<real<<"+"<<imag<<"i.";

   }

   friend Complex sum(Complex,Complex);     //Declaration of friend function.

};

Complex sum(Complex obj1,Complex obj2)

{

 Complex obj3;

 obj3.real=obj1.real+obj2.real;

 obj3.imag=obj1.imag+obj2.imag;

 return obj3;

}

void main()

{

 clrscr();

 int a,b,c;

 Complex c1;                      //Calling default constructor.



 cout<<"\n\t\tPROGAM TO PERFORM ADDITION OF TWO COMPLEX NUMBERS USING CONSTRUCTOR OVERLOADING\t\t\n";



 cout<<"\n\t\tFor equal values :\t";

 cout<<"\n\tEnter the equal value of real and imaginary part of number 1:\n\t";

 cin>>a;

 Complex c2(a);                //Calling parameterised constructor for equal values.



 cout<<"\n\t\tFor different values :\t";

 cout<<"\n\tEnter the real and imaginary part of number 2:\n\t";

 cin>>b>>c;

 Complex c3(b,c);                         //Calling parameterised constructor for different values.



 Complex c4=sum(c2,c3);          //Calling copy constructor.

 c4.print();

 getch();

 }


Output:



















Saturday, 14 January 2017

program to explain class in C++ and print marks of student

Tags

Program to make a class student to print result of student.



#include<iostream.h>

#include<conio.h>



class student

{

int rollno;

char name[6];

int classstud;

int marks[5];

float per;

float calculate();

public:void readmarks();

       void displaymarks();

};

float student::calculate()

{

per=0;

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

per+=marks[i];

per=(per/5);

return per;

}

void student::readmarks()

{

cout<<"enter rollno no.:";

cin>>rollno;

cout<<"enter the name:";

cin>>name;

cout<<"enter the class standing in:";

cin>>classstud;

cout<<"enter the marks:"<<endl;

for(int j=0;j<5;j++){

cout<<"\t enter marks"<<j+1<<":";

cin>>marks[j];

}

}

void student::displaymarks()

{

cout<<"Rollno:"<<rollno<<endl;

cout<<"name:"<<name<<endl;

cout<<"class:"<<classstud<<endl;

cout<<"percentage:"<<calculate()<<endl;

}

int main()

{

student s1;

s1.readmarks();

s1.displaymarks();

getch();

}









OUTPUT


Monday, 9 January 2017

Comparison between C and C++.

Tags
  1. C++ is super set of C language.
  2. C++ program can use existing C software libraries.
  3. C follows top down approach and C++ follows bottom up approach.
  4. C adopt procedure oriented language and C++ follows object oriented language as well as procedure oriented language.
  5. Both are middle level language.
  6. In C++ data is secured because of class uses different access specifier like private and protected and C only have public access specifier.
  7.  C does not have friend and virtual function but C++ supports friend and virtual function.

Saturday, 7 January 2017

Introduction and history of C++

Tags
  • C++ was invented by Bjarne stroustrup.
  • It was invented in 1979.
  • In 1979 ,it was called C with classes . But its name was changed in 1983 and currently its called C++.
  • C++ is middle level language because its a combination of low level language and high level language.
  • low level language means machine dependent language and we can only built  system software with the help of low level language like device drivers.
  • High level language is used to build application softwares.
  • so, middle level language is includes low level as well  as high level language. so with use of C++ and C we can build system software as well as application software.
  • C++ joins three separate programming traditions .
                                                  - procedural language.
                                                   -object oriented language.
                                                   -generic programming with use of templates.
  •    oops was invented by Alan Kay and simula 16 was the first object oriented language.

multiplication of matrix in c

Tags

#include<stdio.h>

#include<conio.h>

void main()

{
int a[10][10],b[10][10],multi,r1,c1,r2,c2,i,j,k;

clrscr();

printf(“enter row and column for first matrix:”);

scanf(“%d%d”,&r1,&c1);

printf(“enter row and column for second matrix:”);

while(c1!=r2)

{
printf(“error! column of first matrix not equal to row of second:”);

printf(“enter row and column for first matrix:”

scanf(“%d%d”,&r1,&c1);

printf (“enter row and column for second matrix:”);

scanf(“%d%d”,&r1,&c2);

}

printf(“\n enter elements of matix 1:\n”);

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

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

{

printf(“enter elements  a%d%d:”,i+1,J+1);

scanf(“%d”,&a[i][j]);

}



printf(“\n enter elements of matix 2:\n”);

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

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

{

printf(“enter elements  b%d%d:”,i+1,J+1);

scanf(“%d”,&b[i][j]);

}

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

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

{

mult[i][j]=0;

}

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

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

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

{

mult[i][j]+=a[i][k]*b[k][j];

}

printf(“\n output matrix:\n”);

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

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

{

printf(“%d   ”,mult[i][j]);

if(j==c2-1)

printf(“\n\n”);

}

getch();

}