1 / 10

C++ programming I Lab#5

C++ programming I Lab#5. Functions Instructor: Eng. Ruba A. Salamah Tuseday: 10/12/2010. Exercise #1: a) Write a function that computes the sum of digits in an integer. Use the following function header: i nt SumDigits ( long n)

franz
Download Presentation

C++ programming I Lab#5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ programming ILab#5 Functions Instructor: Eng. Ruba A. Salamah Tuseday: 10/12/2010

  2. Exercise #1: a) Write a function that computes the sum of digits in an integer. Use the following function header: intSumDigits( long n) b) Write a program that reads an integer and prints the sum of its digits using the function in (a).

  3. EX1: solution #include <iostream> using namespace std; int SumDigits(long n) {int sum=0; while(n!=0) {sum += n% 10; n=n/10;} return sum; } int main() {long x; cout<<"enter an integer"; cin>>x; cout<< "the sum of your integer digits is: "<<SumDigits(x); system("pause"); return 0; }

  4. Exercise #7: The value for π (PI) can be determined by the series equation write an interactive program that asks the user how many terms of series equation to use in approximating PI. Then calculate and display the approximation. Here is a sample run:

  5. EX7 : solution #include <iostream> #include <cmath> using namespace std; int main() { double sum = 0; double item ; cout<<"PI Approximation Program\nHow many terms of the series should be included?\n"; cin>>item; for (int i = 1; i <= item; i+=2) sum=sum+1.0/(2*i-1)-1.0/(2*i+1); cout<<"Approximation value of pi is "<<4*sum<<endl; system("pause"); return 0; }

  6. 18_a #include <iostream> #include <iomanip> using namespace std; int main() { for(int i=1;i<=6;i++) { for(int j=1;j<=i;j++) cout<<j; cout<<endl; } system("pause"); return 0; }

  7. 18_b include <iostream> #include <iomanip> using namespace std; int main() { for(int i=6;i>=1;i--) { for(int j=1;j<=i;j++) cout<<j; cout<<endl; } system("pause"); return 0; }

  8. 18_c #include <iostream> #include <iomanip> using namespace std; int main() { for(int i=1;i<=6;i++) {//cout<<setw(7-i); For(int s=1;s<=6-I;s++) Cout<<“ “; for(int j=1;j<=i;j++) cout<<j; cout<<endl; } system("pause"); return 0; }

  9. Quiz

More Related