diff --git a/Mozhgan/HW2/ch5-19.cpp b/Mozhgan/HW2/ch5-19.cpp new file mode 100644 index 0000000..37deb85 --- /dev/null +++ b/Mozhgan/HW2/ch5-19.cpp @@ -0,0 +1,21 @@ +#include +#include +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 "< +#include +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< +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; + } +} diff --git a/Mozhgan/HW2/ch5-25.cpp b/Mozhgan/HW2/ch5-25.cpp new file mode 100644 index 0000000..f21d8a0 --- /dev/null +++ b/Mozhgan/HW2/ch5-25.cpp @@ -0,0 +1,17 @@ +#include +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; +} diff --git a/Mozhgan/HW3/ch6-16.cpp b/Mozhgan/HW3/ch6-16.cpp new file mode 100644 index 0000000..86fa407 --- /dev/null +++ b/Mozhgan/HW3/ch6-16.cpp @@ -0,0 +1,14 @@ +#include +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; +} diff --git a/Mozhgan/HW3/ch6-17.cpp b/Mozhgan/HW3/ch6-17.cpp new file mode 100644 index 0000000..8d48158 --- /dev/null +++ b/Mozhgan/HW3/ch6-17.cpp @@ -0,0 +1,13 @@ +#include +using std::cout; +using std:: endl; +#include +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; +} diff --git a/Mozhgan/HW3/ch6-18.cpp b/Mozhgan/HW3/ch6-18.cpp new file mode 100644 index 0000000..e86f351 --- /dev/null +++ b/Mozhgan/HW3/ch6-18.cpp @@ -0,0 +1,27 @@ +#include +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; +} diff --git a/Mozhgan/HW3/ch6-26.cpp b/Mozhgan/HW3/ch6-26.cpp new file mode 100644 index 0000000..a374ec7 --- /dev/null +++ b/Mozhgan/HW3/ch6-26.cpp @@ -0,0 +1,29 @@ +#include +#include +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; +} diff --git a/Mozhgan/HW3/ch6-28.cpp b/Mozhgan/HW3/ch6-28.cpp new file mode 100644 index 0000000..5feef9e --- /dev/null +++ b/Mozhgan/HW3/ch6-28.cpp @@ -0,0 +1,34 @@ +#include +#include +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; + } + } +} diff --git a/Mozhgan/HW3/ch6-29-a.cpp b/Mozhgan/HW3/ch6-29-a.cpp new file mode 100644 index 0000000..a6a204d --- /dev/null +++ b/Mozhgan/HW3/ch6-29-a.cpp @@ -0,0 +1,30 @@ +#include +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"; +} diff --git a/Mozhgan/HW3/ch6-29-b.cpp b/Mozhgan/HW3/ch6-29-b.cpp new file mode 100644 index 0000000..7a0ed48 --- /dev/null +++ b/Mozhgan/HW3/ch6-29-b.cpp @@ -0,0 +1,30 @@ +#include +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; +} diff --git a/Mozhgan/HW3/ch6-29-c.cpp b/Mozhgan/HW3/ch6-29-c.cpp new file mode 100644 index 0000000..133013e --- /dev/null +++ b/Mozhgan/HW3/ch6-29-c.cpp @@ -0,0 +1,32 @@ +#include +using std::cout; +using std::cin; +using std::endl; + +#include + +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; +} diff --git a/Mozhgan/HW3/ch6-31.cpp b/Mozhgan/HW3/ch6-31.cpp new file mode 100644 index 0000000..e148d13 --- /dev/null +++ b/Mozhgan/HW3/ch6-31.cpp @@ -0,0 +1,42 @@ +#include +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; + } + +} diff --git a/Mozhgan/HW3/ch6-36.cpp b/Mozhgan/HW3/ch6-36.cpp new file mode 100644 index 0000000..14c1e80 --- /dev/null +++ b/Mozhgan/HW3/ch6-36.cpp @@ -0,0 +1,26 @@ +#include +using std::cout; +using std::cin; +using std::endl; + +int power(int, int); + +int main() +{ + int base, exponent; + + cout << "Enter base and exponent: "; + cin >> base >> exponent; + cout << "Result is: " << power(base, exponent); + + return 0; +} + +int power(int base, int exponent) +{ + while(exponent != 1) + { + return base * power(base, (exponent - 1)); + } + return base; +} diff --git a/Mozhgan/HW3/ch6-37.cpp b/Mozhgan/HW3/ch6-37.cpp new file mode 100644 index 0000000..271915a --- /dev/null +++ b/Mozhgan/HW3/ch6-37.cpp @@ -0,0 +1,27 @@ +#include + +double fibonacci ( int a ) ; + +int main ( void ) { + int number ; + for ( number = 0 ; number <= 1000; number++ ) + fibonacci (number ) ; + return 0 ; +} + +double fibonacci ( int a ) { + double b , c ; + int i ; + b = 0 ; + c = 1 ; + int result = 0 ; + + for ( i = 1 ; i <= a; i++) { + result = b + c ; + b = c ; + c = result ; + } + printf ( "Fibonni result of %5d th is : %10.2f \n" , a , b ) ; + + return 0 ; +} diff --git a/Mozhgan/HW3/ch6-50.cpp b/Mozhgan/HW3/ch6-50.cpp new file mode 100644 index 0000000..17f4b66 --- /dev/null +++ b/Mozhgan/HW3/ch6-50.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +using namespace std; + +class Circle { +private: + float radius; + +public: + inline void circleArea(float) + + void input() { + cout<<"\nEnter the radius of circle"; + cin>>radius; + } + + Circle() { + float radius=0.0; + } +}; + +void circleArea() +{ + float a,r; + a=(3.14*r*r); +} + +int main() +{ + Circle c; + c.input(); + c.circleArea(); +}