Skip to content
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

Mozhgan-HW2 #5

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
21 changes: 21 additions & 0 deletions Mozhgan/HW2/ch5-19.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <iostream>
#include <math.h>
using namespace std;

int main()
{

double PI = 0;
int i = 0;

for(i=0; i<=4000; i++)
{
PI += 4*pow(-1, i)/(2*i+1);
if(i%1000 == 0 && i != 0 )
{
cout<<"PI estimation after "<<i<<" terms is: "<<PI<<endl;
}


}
}
28 changes: 28 additions & 0 deletions Mozhgan/HW2/ch5-20.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <iostream>
#include <math.h>
using namespace std;

int main()
{

int side1 , side2, hypotenuse, x1, x2, x3 = 0;


for(side1=1; side1<=500; side1++)
{
for(side2=1; side2<=500; side2++)
{
for(hypotenuse=1; hypotenuse<=500; hypotenuse++)
{
x1 = side1*side1;
x2 = side2*side2;
x3 = hypotenuse*hypotenuse;
if (x3 == x1 + x2)
{
cout<<side1<<", "<<side2<<", "<<hypotenuse<<endl;
}
}

}
}
}
57 changes: 57 additions & 0 deletions Mozhgan/HW2/ch5-22.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <iostream>
using namespace std;

int main()
{
// a:
int x;
int y;
cout << "Enter x and y for check !( x < 5 ) && !( y >= 7 ) [-1 -1 for exit]:";
cin >> x >> y;
while ((x != -1) or (y != -1))
{
bool condition1 = !( x < 5 ) && !( y >= 7 );
bool condition2 = ! (( x < 5 ) || ( y >= 7 ));
cout << condition1 << " equivalent " << condition2 << endl;
cout << "Enter x and y for check !( x < 5 ) && !( y >= 7 ) [-1 -1 for exit]:";
cin >> x >> y;
}

// b:
int a, b, g;
cout << "Enter a, b and g for check !( a == b) || !( g != 5 ) [-1 -1 -1 for exit]:";
cin >> a >> b >> g;
while ((x != -1) or (y != -1) or (g != -1))
{
bool condition1 = !( a == b) || !( g != 5 );
bool condition2 = ! (( a == b ) && ( g != 5 ));
cout << condition1 << " equivalent " << condition2 << endl;
cout << "Enter a, b and g for check !( a == b) || !( g != 5 ) [-1 -1 -1 for exit]:";
cin >> a >> b >> g;
}

// c:
cout << "Enter x and y for check !(( x <= 8 ) && ( y > 4 )) [-1 -1 for exit]:";
cin >> x >> y;
while ((x != -1) or (y != -1))
{
bool condition1 = !(( x <= 8 ) && ( y > 4 ));
bool condition2 = !( x <= 8 ) || !( y > 4 );
cout << condition1 << " equivalent " << condition2 << endl;
cout << "Enter x and y for check !(( x <= 8 ) && ( y > 4 )) [-1 -1 for exit]:";
cin >> x >> y;
}

// d:
int i, j;
cout << "Enter i and j for check !(( i > 4 ) || ( j <= 6 )) [-1 -1 for exit]:";
cin >> i >> j;
while ((i != -1) or (j != -1))
{
bool condition1 = !(( i > 4 ) || ( j <= 6 ));
bool condition2 = !( i > 4 ) && !( j <= 6 );
cout << condition1 << " equivalent " << condition2 << endl;
cout << "Enter i and j for check !(( i > 4 ) || ( j <= 6 )) [-1 -1 for exit]:";
cin >> i >> j;
}
}
17 changes: 17 additions & 0 deletions Mozhgan/HW2/ch5-25.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <iostream>
using namespace std;

int main()
{
int count;
bool stop = false;
for ( count = 1; count <= 10, !stop; ++count )
{
if ( count == 4 )
stop = true;

cout << count << " ";
}

cout << "\nBroke out of loop at count = " << count << endl;
}
14 changes: 14 additions & 0 deletions Mozhgan/HW3/ch6-16.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <iostream>
using std::cout;
using std:: endl;
using std::rand;

int main()
{
cout << "Random number for 1 <= n <= 2 is " << 1 + rand() % 2 << endl;
cout << "Random number for 1 <= n <= 100 is " << 1 + rand() % 100 << endl;
cout << "Random number for 0 <= n <= 9 is " << rand() % 10 << endl;
cout << "Random number for 1000 <= n <= 1112 is " << 1000 + rand() % 113 << endl;
cout << "Random number for -1 <= n <= 1 is " << -1 + rand() % 3 << endl;
cout << "Random number for -3 <= n <= 11 is " << -3 + rand() % 15 << endl;
}
13 changes: 13 additions & 0 deletions Mozhgan/HW3/ch6-17.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <iostream>
using std::cout;
using std:: endl;
#include <cstdlib>
using std::rand;


int main()
{
cout << "Random number for a) 2, 4, 6, 8, 10 is " << ( 1 + rand() % 5 ) * 2 << endl;
cout << "Random number for b) 3, 5, 7, 9, 11 is " << ( 1 + rand() % 5 ) * 2 + 1 << endl;
cout << "Random number for c) 6, 10, 14, 18, 22 is " << (( 1 + rand() % 5 ) * 2 + 1) * 2 << endl;
}
27 changes: 27 additions & 0 deletions Mozhgan/HW3/ch6-18.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <iostream>
using std::cout;
using std::cin;

int integerPower( int, int );

int base, exponent;

int main()
{
cout << "Enter base and exponent: ";
cin >> base >> exponent;
cout << "Result: " << integerPower(base, exponent);

return 0;
}

int integerPower( int base, int exponent)
{
int result = 1;
while( exponent > 0)
{
result *= base;
exponent--;
}
return result;
}
29 changes: 29 additions & 0 deletions Mozhgan/HW3/ch6-26.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <iostream>
#include <iomanip>
using namespace std;

int fahrenheit (int celsius)
{
return celsius * 9 / 5 + 32;
}

int celsius (int fahrenheit)
{
return (fahrenheit - 32) * 5 / 9;
}

int main()
{
cout << "Fahrenheit equivalents of all Celsius temperatures from 0 to 100: " << endl;
for (int c = 1; c <= 100; c++)
{
cout << setw(8) << fahrenheit(c);
}
cout << endl;
cout << "Celsius equivalents of all Fahrenheit temperatures from 32 to 212: " << endl;
for (int f = 32; f <= 212; f++)
{
cout << setw(8) << celsius(f);
}
cout << endl;
}
34 changes: 34 additions & 0 deletions Mozhgan/HW3/ch6-28.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <iostream>
#include <iomanip>
using namespace std;

bool isPerfect(int number)
{
// 1 is always divisor, so initial sum is 1 and loop started from 2
// maximum divisor not equal to number not larger than number/2
int sumOfDivisors = 1;
for (int i = 2; i <= number / 2; i++)
{
if ((number % i) == 0)
sumOfDivisors += i;
}
return (sumOfDivisors == number);
}

int main()
{
cout << "Perfect numbers between 1 and 1000 (first number, then divisors: " << endl;
for (int number = 1; number <= 1000; number++)
{
if (isPerfect(number))
{
cout << setw(8) << number << " = " << setw(5) << "1";
for (int i = 2; i <= number / 2; i++)
{
if ((number % i) == 0)
cout << " + " << setw(5) << i;
}
cout << endl;
}
}
}
30 changes: 30 additions & 0 deletions Mozhgan/HW3/ch6-29-a.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

void primeNumber(int);

int main()
{
int num;
cout << "Enter an integer: ";
cin >> num;
primeNumber(num);

return 0;
}
void primeNumber(int num)
{
int prime = 0;
for(int j = 2; j < num; j++)
{
if(num % j == 0)
prime++;

}
if(prime > 0)
cout << "The integer is not prime";
else
cout << "The integer is prime";
}
30 changes: 30 additions & 0 deletions Mozhgan/HW3/ch6-29-b.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int primeNumber(int);

int main()
{
for(int x = 1; x <= 10000; x += 2)
{
if( x == primeNumber(x))
cout << x << endl;
}

return 0;
}
int primeNumber(int x)
{
int prime = 0;

for(int j = 2; j < x; j++)
{
if(x % j == 0)
prime++;

}
if(prime == 0)
return x;
}
32 changes: 32 additions & 0 deletions Mozhgan/HW3/ch6-29-c.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <cmath>

int primeNumber(int);

int main()
{
for(int x = 1; x <= 100000; x += 2)
{
if( x == primeNumber(x))
cout << x << endl;
}

return 0;
}
int primeNumber(int x)
{
int prime = 0;

for(int j = 2; j < sqrt(x); j++)
{
if(x % j == 0)
prime++;

}
if(prime == 0)
return x;
}
42 changes: 42 additions & 0 deletions Mozhgan/HW3/ch6-31.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int gcd1(int, int);

int main()
{
int a, b;

cout << "Enter two integers: ";
cin >> a >> b;
cout << "GCD (Euclidian) is: " << gcd1(a, b) << endl;

return 0;
}

//Euclidian
int gcd1(int a, int b)
{
if(a == b)
return a;

while(b!= 0 || a!= 0)
{
if( b != 0)
{
a %= b;
}
else
return a;

if( a != 0)
{
b %= a;
}
else
return b;
}

}
Loading