Docsity

its c++ programs to help you understand, Exercises of Computer science

Toggle sidebar
bg1
Exercise 1
We want to pack N eggs in NB boxes of 12 eggs each. Determine, without using any division,
the number NB of boxes (completely filled, to be sealed), as well as the remaining number of
free eggs (the division will be simulated by successive subtractions).
Answer
#include <iostream>
using namespace std;
int main()
{
int N, NB = 0;
cout << "Enter number of eggs" << endl;
cin >> N;
while (N >= 12) {
N -= 12; // N = N -12;
NB++; // NB = NB +1;
}
cout << "we have " << NB << " completely filled boxes";
cout << "\n The remaining number of eggs = " << N<<endl;
Return 0;
}
Exercises 2
Write a C++ program to create an array which size should be a constant, and perform a
circular permutation to the array then display it after permutation.
Answer
#include <iostream>
using namespace std;
pf3

Partial preview of the text

Download its c++ programs to help you understand and more Exercises Computer science in PDF only on Docsity!

Exercise 1 We want to pack N eggs in NB boxes of 12 eggs each. Determine, without using any division, the number NB of boxes (completely filled, to be sealed), as well as the remaining number of free eggs (the division will be simulated by successive subtractions). Answer #include using namespace std; int main() { int N, NB = 0; cout << "Enter number of eggs" << endl; cin >> N; while (N >= 12) { N - = 12; // N = N - 12; NB++; // NB = NB +1; } cout << "we have " << NB << " completely filled boxes"; cout << "\n The remaining number of eggs = " << N<<endl; Return 0; } Exercises 2 Write a C++ program to create an array which size should be a constant, and perform a circular permutation to the array then display it after permutation. Answer #include using namespace std;

int main() { const int n =10; int a[n]= {5,9,-4,0,3,7,-2,6,1,4}; cout<<"Before permutation the array was: "<<endl; for(int i=0;i<n;i++) cout<<a[i]<<" "; int first_elt = a[0]; for(int i=0; i<n-1;i++) a[i]=a[i+1]; a[n-1] = first_elt; cout<<"\nAfter permutation the array is: "<<endl; for(int i=0;i<n;i++) cout<<a[i]<<" "; return 0; } Exercise 3 Write a function that display a number entered by the user if it’s EVEN or ODD Answer include using namespace std int evenodd(int x){ if (x%2==0){ return " the number is even"; } else