Skip to content

Added Some cpp programs #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Add-nums-with-bitwise.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <stdio.h>
int main()
{
int number,number2,sum,carry;

do{
printf("Enter 1st Number: ");
scanf("%d",&number);

printf("Enter 2nd Number: ");
scanf("%d",&number2);


while(number2!=0){
sum= number ^ number2;
carry= (number & number2)<<1;
number=sum;
number2=carry;
}

printf("Addition is: %d\n",number);


}while(true);
return 0;

}
128 changes: 128 additions & 0 deletions matrix calculator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#include<iostream>
#include<conio.h>
using namespace std;

void setmatrix(int array[3][3])
{
cout<<"Enter Values:\n";
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<"("<<i<<","<<j<<")= ";
cin>>array[i][j];
}
}
}

void showmatrix(int array[3][3])
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{cout<<array[i][j]<<" ";}
cout<<endl;
}
}

void matrixaddition(int array1[3][3],int array2[3][3])
{
int tempvariable;

for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
tempvariable=array1[i][j]+array2[i][j];
cout<<tempvariable<<" ";
}
cout<<endl;
}
}
void matrixmultiplication(int array1[3][3],int array2[3][3])
{
int tempvariable;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{ int sum=0;
for(int k=0;k<3;k++)
{
sum+=array1[i][k]*array2[k][j];
}
tempvariable=sum;
cout<<tempvariable<<" ";
}
cout<<endl;
}
}

void matrixsubtraction(int array1[3][3],int array2[3][3])
{
int tempvariable;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
tempvariable=array1[i][j]-array1[i][j];
cout<<tempvariable<<" ";
}
cout<<endl;
}
}

void matrixdivision(int array1[3][3],int array2[3][3])
{
cout<<"\nCant perform Division in matrices\n";

}
int main()
{
char c,doagain=' ';
int A[3][3], B[3][3]; //Two matrices

cout<<"Enter first matrix: \n";
setmatrix(A);

cout<<"\nEnter 2nd matrix: \n";
setmatrix(B);

cout<<"The two matrices are: \n";
showmatrix(A);

cout<<endl<<endl;
showmatrix(B);

while(doagain!='n')
{

cout<<"\nWhich operation do you want to perform(+,-,*,/)? : ";
cin>>c;
switch(c)
{
case '*':
matrixmultiplication(A,B);
break;

case '+':
matrixaddition(A,B);
break;

case '-':
matrixsubtraction(A,B);
break;

case '/':
matrixdivision(A,B);
break;

default:
cout<<"Invalid operator!!!";

}
cout<<"\nDo u want to perform another operation again(y/n)? :";
cin>>doagain;
}


}
15 changes: 15 additions & 0 deletions pointers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include<iostream>
using namespace std;
int main()
{ int h=5;
char c='Z';
int* v;
v=&h;
char *b;
b=&c;
//b=&h; //error1= cannot convert 'int*' to 'char*' in assignment
cout<<"h= "<<h<<endl<<"c= "<<c
<<"\n*v= "<<*v
<<"\n*b= "<<*b<<endl;
cout<<"Address of h= "<<v<<"\naddress of c= "<<b;
}
18 changes: 18 additions & 0 deletions prime number.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{ int y;
bool a=true;
cout<<"Enter any number= ";
cin>>y;
for (int x=2;x<y;x++){
if(y%x==0)
{
a=false;
cout<<"is not prime bcoz it is divisible by "<<x;
break;}
}
if(a==true)cout<<"Is prime.";
getch();
}
45 changes: 45 additions & 0 deletions reading files.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream reading("C:/Users/zohaib hassan/Desktop/tuesday/filing/zhs.txt");
char data[200];
while(!reading.eof()) //or while(reading
{
reading.getline(data,200);
cout<<data;
}

}
/* line by line reading:
string line;
while(getline(reading,line));
or
//or while(reading

or
reading.read(data,200);
reading.getline(data,sizeof(data);
*/


/*
word by word searching:

string word;
ifstream readiing("D:....")
while(reading>>word)
{
if(word=="stored")
{
cout<<word<<endl;
break;
}
}

reading.good() //good returns 1 when file is not ended and 0 when file is ended;



*/
23 changes: 23 additions & 0 deletions switch statement.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include<iostream>
using namespace std;
int main()
{ int a;
cout<<"enter any integer =";
cin>>a;
switch(a)
{ case 60:
case 50:
// if cases r not identified in input then default will be executed.
case 40:
cout<<"A grade";
break;
case 30:
case 20:
cout<<"D grade.";
break;
default:
cout<<"Any other grade than \"A\" or \"D\"";
}


}
14 changes: 14 additions & 0 deletions time conversion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{ int hours,secs,mins;
cout<<"Enter no of hours= ";
cin>>hours;
mins=hours*60;
cout<<"Mins in given hours= "<<mins<<endl;
secs=mins*60;
cout<<"Seconds in given hours= "<<secs<<endl;

getch();
}
12 changes: 12 additions & 0 deletions turnory operator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include<iostream>
#include<conio.h>
using namespace std;
int main()
//turnory operator works same as if else does but the difference is
//that if else is a control structure and this is an operator and it
//works on three operands i.e the condition, true statement and the false statement simultaneously.
{ int x;
cout<<"Enter an number= ";
cin>>x;
(x<=9)? cout<<"It is a singe digit.":cout<<"It is not a single digit.";
}