>X ;   cout<< " Enter the value of Y : " << endl;   cin>> Y ;   if ( X>= Y ) Z = X + y; else   if ( X< Y ) Z = X - y ; cout<< " the value of Z = " << Z << endl; }"> >X ;   cout<< " Enter the value of Y : " << endl;   cin>> Y ;   if ( X>= Y ) Z = X + y; else   if ( X< Y ) Z = X - y ; cout<< " the value of Z = " << Z << endl; }">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exercise 5.

Similar presentations


Presentation on theme: "Exercise 5."— Presentation transcript:

1 Exercise 5

2 Write C++ program to find and print the value of Z from the following equation:
#include<iostream >  using namespace std ;  void main () { int X , Y , Z ; cout<< " Enter the value of X : " << endl; cin>>X ;   cout<< " Enter the value of Y : " << endl;   cin>> Y ;   if ( X>= Y ) Z = X + y; else   if ( X< Y ) Z = X - y ; cout<< " the value of Z = " << Z << endl; }

3 Write C++ program to read N numbers (from the user), then the program find and print the summation and average of these numbers. #include<iostream> using namespace std ;  void main() {   int n ,x, sum ; int i ;   float avg ;   sum = 0 ;   cout <<"please enter how many numbers ?" ;   cin>> n ;   for(i=1 ; i<= n ; i++)   { cout<< " enter number" << i <<" " ; cin>> x ;   sum = sum+ x ; }  avg = float(sum ) / n ; cout << "sum= "<< sum <<endl<< "avg=" << avg << endl ;  }

4 Write C++ program to formulate a solution to calculate and print the factorial of number (n)?
Note: Using For loop, number (n) accept from user #include<iostream> using namespace std; int main() { int n,fact=1; cout<<"Please enter integer number \n"; cin>>n; if (n<0) cout<<"Enter positive number only"; else for (int i=1; i<=n; i++) fact *=i; } cout<<"The fact of number = "<<fact<<"\n"; return 0;

5 Write C++ program to read a two positive integer numbers X & Y (from the user), then the program calculate and print the power of these numbers.(Note: XY = X0×X1×X2…..×XY ). #include <iostream> Using namespace std ;  void main () { int x , y , i , p ;   p= 1 ; cout<<" this program calculate Power of any two positive numbers X of y "<<endl;   cout<<"please , Enter X number : " << endl;   cin >>x ;   cout<<"please , Enter Y number : " << endl;   cin >>y ; if ( y >= 0) for(i= 1 ; i<= y ; i++) p = p * x ; cout<< " the Power X of y = " << p <<endl; } else cout<< "the value of y< 0 "<< endl;

6 Write C++ program to create a matrix A [5] of integer numbers by readings its elements from the user, then the program print this matrix as list (one row). #include<iostream> using namespace std ; void main() { int A[5] ; int i;   for(i= 0 ; i<5 ; i++) cout<<"A[" << i << "]=" ; cin>>A[i] ; } for(i= 0 ; i<5 ; i++) cout<<A[i]<< " " ; cout<<endl;

7 Write c++ program that transfer elements of array to another array.
#include<iostream> using namespace std; int main() { int list1[5]; int list2[5]; for (int i=0; i<5; i++) cout<<"Please enter the value of list["<<i<<"] : "; cin>>list1[i]; } cout<<"The elements of array 1 \n"; for (int c=0; c<5; c++) cout<<list1[c]<<"\t"; cout<<endl; ///********************************************************************* for (int j=0; j<5; j++) list2[j]=list1[j]; cout<<"The elements of array 2 \n"; for (int k=0; k<5; k++) cout<<list2[k]<<"\t";

8 #include<iostream>
using namespace std; bool find_item(int arr[], int size, int searchnum); int main() { int x; int item[5]; for (int i=0; i<5; i++) cout<<"Please enter the value of array["<<i<<"] : "; cin>>item[i]; } cout<<endl; cout<<"Please enter the number to search for \n"; cin>>x; if (find_item(item,5,x)==true) cout<<"The item is found \n"; else cout<<"The item is not found \n"; bool find_item(int arr[], int size, int searchnum) bool find= false; for (int i=0; i<size; i++ ) if (arr[i]== searchnum) find=true; return find; Write C++ program to read five numbers from the user and create a list item of these numbers. Then the program read a value x from the user and search for this value in the list item. the program print “x is found” if the value x is in the list item, or print “x is not found” if the value x is not in the list item

9 Write a program that ask the user to enter 10 employee salaries and store them, then add a bonus of 10% for each employee and print out the average salary value.

10 Write C++ program to create a matrix M[5][4] of integer numbers by readings its elements from the user, then the program print this matrix ( row by column) . #include<iostream> using namespace std ; void main() { int M[5][4] ; int i , j ;   for(i= 0 ; i<5 ; i++) for(j=0 ; j<4 ; j++)   { cout<<"M["<< i << "]["<< j << "]=" ; cin>>M[i][j] ; } for(i= 0 ; i<5 ; i++) cout<<M[i][j]<<" " ;   cout<<endl;  }

11 Write a program to swap between two integer numbers.

12 Write C++ program to print the following output:
// - Count backwards using a for loop. #include <iostream> using namespace std; int main() { for (int count = 15; count > -1; count--) cout << count << ", "; } cout << endl; return 0; /*=================[output]========================== 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, =====================================================*/

13 Write C++ program to print the following form:
#include<iostream> using namespace std; #include<iomanip> void main() { for (int i=10; i>=1; i--) for (int j=1; j<=i; j++ ) cout<<“*”<<“ “ cout<<endl; } * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

14 Write C++ function called RectangleArea which takes two double values length and width then the function calculates and returns the area of rectangle. #include<iostream> using namespace std; double RectangleArea (double a,double b); int main() { double length,width;   cout<<“This program find area of rectangle by function"<<endl;   cout<<"Enter your length"<<endl; cin>> length;   cout<<"Enter your width"<<endl; cin>> width;   cout<<"the area of your rectangle="<<RectangleArea(length,width)<<endl;   } double RectangleArea (double a,double b) double area; area= a*b; return area; }

15 Write C++ four functions
Write C++ four functions. Sum, Sub, Mul, Div which takes two integer numbers x and y then the functions calculate and return the result. #include<iostream> using namespace std; int Sum(int a,int b) { return a+b;} int Sub(int a,int b) { return a-b;}  int Mul(int a,int b) { return a*b;}  int Div(int a,int b) { return a/b;}  void main() { int x,y ;   cout<<"this program calculates two numbers by four functions"<<endl; cout<<"please, Enter first number:"<<endl; cin>>x; cout<<"please, Enter second number:"<<endl; cin>>y; cout<<"the summation of two numbers ="<<Sum(x,y)<<endl; cout<<"the subtraction of two numbers ="<<Sub(x,y)<<endl; cout<<"the Multiplication of two numbers ="<<Mul(x,y)<<endl; cout<<"the Divition of two numbers ="<<Div(x,y)<<endl; }

16 Write a program that ask the user to enter 3 integer numbers and print out their sum and average

17 Write a program that ask the user to enter 2 integer numbers and print out the larger of them

18 #include <iostream >
using namespace std ; int FACT(int x ) ;  int main () { int x ; cout<<“ This program calculate factorial of any positive number "<<endl; cout<<"please , Enter positive number : " << endl; cin >>x ; if ( x > 0) cout<< " the Factorial of " << x << " = " << FACT(x)<<endl; else cout<< "the value of x<= 0 "<< endl; return 0; } int FACT(int x ) int i , f=1 ; for(i= x ; i>= 1 ; i--) f= f * i ; return f ;   } Write C++ function called Fact which takes an integer value x then the function calculates and returns the factorial of the value x.

19 Example: Write a program to calculate the area for a triangle.
The area of triangle= ½*base*height

20 #include <iostream>
using namespace std ;   int POWER(int x, int y); void main () { int x , y ; cout<<" this program calculate Power of any two positive numbers X of y "<<endl;   cout<<"please , Enter X number : " << endl;   cin >>x ;   cout<<"please , Enter Y number : " << endl;   cin >>y ; if ( y >= 0) cout<< " the Power X of y = " << POWER(x,y) <<endl; } else cout<< "the value of y< 0 "<< endl; int POWER(int x, int y) int i,p=1;   for(i= 1 ; i<= y ; i++)   p = p * x ; return p ; Write C++ function called Power which takes two integer numbers x and y then the function calculates and returns the result.

21 Write C++ function called Factorial which takes an unsigned long integer value number then the function calculates and returns the factorial of the value x. (using Recursion).

22 Example: Write a program to calculate the area and volume for a shape.
The area of sphere= 4*pi*radius* radius The Volume of sphere= ¾*pi*radius*radius*radius Note: pi=3.14

23 Write a program that build a matrix of 5 rows and 3 columns
Write a program that build a matrix of 5 rows and 3 columns. As the user to enter values for all the matrix item, print out the sum of all matrix items and print out the sum of the diagonal items. (Use function called print_matrix to print the items of matrix)

24 What is the output for each of the following statements: Where:

25 Write C++ program to read a list of 10 integer numbers, then the program find and print the maximum and minimum number between. #include<iostream> using namespace std ; void main() { int L[10] ; int i ,min,max ;   for(i= 0 ; i<10 ; i++) cout<<"L[" << i << "]=" ; cin>>L[i] ;   } min=max=L[0]; for(i= 0 ; i<10 ; i++) if (L[i]> max)   max=L[i];   if (L[i]< min)   min=L[i]; }   cout<<"The maximum number:"<<max<<endl;   cout<<"The minimum number:"<<min<<endl;  }


Download ppt "Exercise 5."

Similar presentations


Ads by Google