25
Exercise 5 Exercise 5 1

Exercise 5 1. Write C++ program to find and print the value of Z from the following equation: Write C++ program to find and print the value of Z from

Embed Size (px)

Citation preview

Exercise 5Exercise 5

11

Write C++ program to find and print the Write C++ program to find and print the value of Z from the following equation: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; }

Write C++ program to read N numbers (from the user), then the program find and print the summation and average of these numbers.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 ;  }

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;{

Write C++ program to read a two positive integer numbers X & Y Write C++ program to read a two positive integer numbers X & Y (from the user), then the program calculate and print the power of (from the user), then the program calculate and print the power of these numbers.(Note: Xthese numbers.(Note: XYY = X = X00××XX11××XX22…..…..××XXY Y ).).

#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; }

Write C++ program to Write C++ program to createcreate a matrix a matrix A [5]A [5] of integer of integer numbers by numbers by readingsreadings its elements from the user, then its elements from the user, then the program the program printprint this matrix as list (one row). 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;  }

77

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"; }cout<<endl;}

88

#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

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

99

Write C++ program to Write C++ program to createcreate a matrix a matrix M[5][4]M[5][4] of integer numbers by of integer numbers by readingsreadings its elements from the user, then the program its elements from the user, then the program printprint this this matrix ( row by column) . 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++) {

for(j=0 ; j<4 ; j++) cout<<M[i][j]<<" " ;   cout<<endl;

} }

Write a program to swap Write a program to swap between two integer between two integer numbers.numbers.

1111

// - 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,

=====================================================*/

Write C++ program to print the following output:

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; }}

Write C++ function called Write C++ function called RectangleArea RectangleArea which takes which takes two double values length and width then the function two double values length and width then the function calculates and returns the area of rectangle.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;

}

Write C++ four functions. Write C++ four functions. Sum, Sub, Mul, Div Sum, Sub, Mul, Div which takes two integer which takes two integer numbers x and y then numbers x and y then the functions calculate the functions calculate and return the result.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; }

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

1616

1717

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

Write C++ function called Write C++ function called Fact Fact which takes an which takes an integer value x then the integer value x then the function calculates and function calculates and returns the factorial of returns the factorial of the value x.the value x.

#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 ;  }

Example: Write a program to calculate the area for a Example: Write a program to calculate the area for a triangle.triangle.

- The area of triangle= ½*base*heightThe area of triangle= ½*base*height

1919

Write C++ function called Write C++ function called Power Power which takes two which takes two integer numbers x and y integer numbers x and y then the function then the function calculates and returns the calculates and returns the result.result.

#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 Write C++ function called Factorial Factorial which takes an unsigned long which takes an unsigned long integer value number then the function calculates and returns the integer value number then the function calculates and returns the factorial of the value x. factorial of the value x. (using Recursion).(using Recursion).

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

2222

Write a program that build a matrix of 5 rows and 3 columns. As the user to 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 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 out the sum of the diagonal items. (Use function called print_matrix to print the items of matrix)print the items of matrix)

2323

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

2424

Write C++ program to Write C++ program to readread a list of 10 integer numbers, then the a list of 10 integer numbers, then the program program findfind and and printprint the the maximummaximum and and minimumminimum number number between.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; }