Monday 10 February 2020

Simple cpp program with if else statement to check if the numbers are equal or not

Source Code:

#include<iostream>
using namespace std;
int main()
{
int x,y,z;
cout<<"Enter X: ";
cin>>x;
cout<<"Enter Y: ";
cin>>y;
cout<<"Enter Z: ";
cin>>z;
if(x=y==z)
{
cout<<"The numbers are equal";
}
else
{
cout<<"The universe has collapsed";
}
return 0;
}

Program Output:

CPP program to subtract two matrix

Source Code:

#include<iostream>
using namespace std;
int main()
{
int a[2][2],b[2][2],c[2][2],i,j;
cout<<"Enter first matrix: ";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cin>>a[i][j];
}
}
cout<<"Enter second matrix: ";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cin>>b[i][j];
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
c[i][j]=a[i][j]-b[i][j];
}
}
cout<<"The subtracted matrix is: ";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cout<<c[i][j]<<endl;
}
}
return 0;
}

Program Output:

CPP program to print the reverse of a string

Source Code:

#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char a[10];
int i;
cout<<"Enter a string: ";
cin.getline(a,10);
cout<<"The reversed name is: ";
for(i=10;i>=0;i--)
{
cout<<a[i];
}

return 0;
}


Program Output:

CPP program to print the prime number elements in an array

Source Code:

#include<iostream>
using namespace std;
int main()
{
int a[10]={3,5,6,7,8},j,p;
int i;
cout<<"The prime numbers are: "<<endl;
for(i=0;i<5;i++)
{
j=2;
p=1;
while(j<a[i])
{
if(a[i]%j==0)
{
p=0;
break;
}
j++;
}
if(p==1)
{
cout<<a [i]<<" ";
}
}

return 0;
}

Program Output:

CPP program to print the input numbers in an array

Source Code:

#include<iostream>
using namespace std;
int main()
{
  int n[10];
  cout<<"Enter the numbers: ";
  for(int i=1;i<=10;i++)
  {
  cin>>n[i];
  }
  cout<<"The numbers are: ";
  for(int i=1;i<=10;i++)
  {
    cout<<n[i]<<endl;
  }
  return 0;
}

Program Output:

CPP program to print the factorial of a number

Source Code:

#include<iostream>
using namespace std;
int main()
{
int i,factorial=1,n;
cout<<"Enter a number: ";
cin>>n;
for(i=1;i<=n;i++)
{
factorial=factorial*i;
}
cout<<"The factorial of the number is: ";
cout<<factorial;

return 0;

}


Program Output:

CPP program to print only odd number elements in an array

Source Code:

#include<iostream>
using namespace std;
int main()
{
int a[10]={0,1,2,3,4,5,6,7,8,9};
cout<<"The array elements are: ";
for(int i=0;i<10;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
cout<<"The odd elements of the array are: ";
for(int i=0;i<10;i++)
{
if(a[i]%2!=0)
{
cout<<a[i]<<" ";
}
}
return 0;
}

Program Output:

CPP program to print only even number elements in an array

Source Code:
#include<iostream>
using namespace std;
int main()
{
int a[10]={0,1,2,3,4,5,6,7,8,9};
cout<<"The array elements are: ";
for(int i=0;i<10;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
cout<<"The even elements of the array are: ";
for(int i=0;i<10;i++)
{
if(a[i]%2==0)
{
cout<<a[i]<<" ";
}
}
return 0;
}


Program Output:


CPP program to print A to Z

Source Code:
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char c;
for(c='A';c<='Z';c++)
{
cout<<c<<" ";
}

return 0;
}

Program Output:

CPP program to multiply two matrix

Source Code:
#include<iostream>
using namespace std;
int main()
{
int a[2][2],b[2][2],c[2][2],i,j;
cout<<"Enter first matrix: ";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cin>>a[i][j];
}
}

cout<<"Enter second matrix: ";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cin>>b[i][j];
}
}

for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
c[i][j]=a[i][j]*b[i][j];
}
}

cout<<"The product of the two matrix is: ";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cout<<c[i][j]<<endl;
}
}
return 0;
}


Program Output:

CPP program to implement binary search in an array

Source Code:

#include<iostream>
using namespace std;
int main()
{
int count,i,arr[30],num,first,last,middle;
cout<<"how many elements would you like to enter?: ";
cin>>count;
for(i=0;i<count;i++)
{
cout<<"Enter number"<<(i+1)<<":";
cin>>arr[i];
}
cout<<"Enter number that you want to search: ";
cin>>num;
first=0;
last=count-1;
middle=(first+last)/2;
while(first<=last)
{
if(arr[middle]<num)
{
first=middle+1;
}
else if(arr[middle]==num)
{
cout<<num<<" "<<"found in the array at the location"<<" "<<middle+1<<"\n";
break;
}
else
{
last=middle-1;
}
middle=(first+last)/2;
}

if(first>last)
{
cout<<num<<" "<<"not found in the array";
}

return 0;
}

Program Output:

CPP program to find out a number is prime or not using function

Source Code:
#include<iostream>
using namespace std;
void isprime(int n)
{
int i,flag=0,m=0;
m=n/2;
for(i=2;i<=m;i++)
{
if(n%i==0)
{
cout<<"Number is not Prime."<<endl;
flag=1;
break;
}
}
if(flag==0)
  cout<<"Number is Prime"<<endl;
}

int main()
{
int x;
cout<<"Enter a number: ";
cin>>x;
isprime(x);
return 0;
}

Program Output:

CPP program to find maximum element of an array with less time complexity

Source Code:
#include<iostream>
using namespace std;
int main()
{
int a[10],n,i;
cout<<"Enter the no.of elements you wish to store in an array: ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter element"<<" "<<(i+1)<<":";
cin>>a[i];
}
for(i=1;i<n;i++)
{
if(a[0]<a[i])
{
a[0]=a[i];
}
}

cout<<"Therefore the maximum element of the array is:  "<<a[0];
}

Program Output:

CPP program to divide one matrix by the other

Source Code:
#include<iostream>
using namespace std;
int main()
{
int a[2][2],b[2][2],c[2][2],i,j;
cout<<"Enter first matrix: ";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cin>>a[i][j];
}
}

cout<<"Enter second matrix: ";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cin>>b[i][j];
}
}

for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
c[i][j]=a[i][j]/b[i][j];
}
}

cout<<"The product of the two matrix is: ";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cout<<c[i][j]<<endl;
}
}
return 0;
}

Program Output:


CPP program to display the odd and even elements separately in an array

Source Code:
#include<iostream>
using namespace std;
int main()
{
int c[10],even[10],odd[10],j=0,l=0,no;
cout<<"Enter the size of array: ";
cin>>no;
cout<<"Enter any "<<" "<<no<<" "<<"elements in Array: ";
for(int k=0;k<no;k++)
{
cin>>c[k];
}
for(int k=0;k<no;k++)
{
if(c[k]%2==0)
{
even[j]=c[k];
j++;
}
else
{
odd[l]=c[k];
l++;
}
}
cout<<"Even elements: ";
for(int k=0;k<j;k++)
{
cout<<even[k]<<" "<<endl;
}
cout<<"Odd elements: ";
for(int k=0;k<l;k++)
{
cout<<odd[k]<<" "<<endl;
}
return 0;
}

Program Output:

CPP program to display the 5 elements of an array of size 10 using loop

Source Code:
#include<iostream>
using namespace std;
int main()
{
int a[10]={0,1,2,3,4,5,6,7,8,9};
cout<<"To display upto the 5th element of the array: ";
for(int i=1;i<=5;i++)
{
cout<<a[i];
}
return 0;
}

Program Output:

CPP program to display the 5 elements of an array of size 10 using loop from backwards

Source Code:
#include<iostream>
using namespace std;
int main()
{
int b[10]={0,1,2,3,4,5,6,7,8,9};
cout<<"view the numbers upto 5 from backwards: ";
for(int j=9;j>=5;j--)
{
cout<<b[j];
}
return 0;
}

Program Output:

Program to concatenate two strings in CPP

Source Code:
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
string c1,c2,c3;
cout<<"Enter first string: ";
getline(cin,c1);
cout<<"Enter second string: ";
getline(cin,c2);
c3 = c1 + c2;
cout<<"The concatenated string is: ";
cout<<c3;
return 0;
}

Program Output:

Program to calculate area of triangle in CPP

Source Code:
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int a,b,c,sum;
float s,area;
cout<<"Enter the values of a,b,c: ";
cin>>a>>b>>c;
sum=a+b+c;
s=float(sum)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
cout<<"Area of the triangle: "<<area;
return 0;
}

Program Output:

Program to add two matrix in CPP

Source Code:
#include<iostream>
using namespace std;
int main()
{
int a[2][2],b[2][2],c[2][2],i,j;
cout<<"Enter first matrix: ";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cin>>a[i][j];
}
}

cout<<"Enter second matrix: ";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cin>>b[i][j];
}
}

for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}

cout<<"The added  matrix is: ";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cout<<endl<<c[i][j]<<endl;
}
}

return 0;
}

Program Output: