Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lab 5.

Similar presentations


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

1 Lab 5

2 Ex1 Write a C++ program that calculate the factorial of a number n that the user will enter. Where the factorial(n!) for nonnegative integer(n) = n * (n-1) * (n-2) * …. * 2 * 1

3 Ex.1 #include<iostream.h> int main() { int x,fact=1; cout<<"Please enter number : "; cin>> x; for(int i=x; i>0;i--) fact=fact*i; cout<<" The factorial of " << x << " = " <<fact<<endl; system("pause"); return 0; }

4 Ex.2 An integer number is said to be a perfect number if its factors, including 1 (but not the number itself),sum to the number. For example: ”6” is a perfect number because 6= Write a program that determines if parameter number is a perfect number.

5 Ex.2 Perfect number: (هو عدد موجب يساوى مجموع الارقام التى تقبل القسمة عليه بما فيها الواحد مثل: 6= فرقم 6 يقبل القسمة على 1 وعلى 2 وعلى 3 فهذه الارقام تسمى factors اى معاملاته التى يقبل القسمة عليها وبالمثل: 28= فى هذه المسألة نريد التأكد من شيئين : الاول ان نقسم العدد مثلا 6 على كل الارقام من 1 وحتى 6 والذى نجد باقى القسمة بتاعه صفر "يعنى ان ال 6 يقبل القسمة عليه " ناخده ونضعه فى sum وكذلك نظل نجمع على ال sum كل الاعداد التى تقبل القسمة على رقمنا الذى نختبره . ثانى شئ : نشوف هل sum تساوى الرقم الذى نختبره ام لا.

6 Ex.2 # include <iostream.h> int main() { int num, sum=0; cout<<"Enter the number that you will check : "; cin>>num; cout<<endl; for(int i=1 ; i<num ; i++) if(num%i==0) sum+=i; } if(sum==num) cout<<num<<" is aperfect number\n"; else cout<<num<<" isn't aperfect number\n"; return 0;

7 Ex.3 An integer is said to be prime if it is divisible(يقبل القسمة) only by 1 and itself. For example 2,3,5,7 and 11 are prime, but 4,6,8,9 are not prime. Write an algorithm and a program that determines if a number is prime.

8 Ex.3 Answer: In this example you should draw the flowchart (an algorithm) in the first and then write the program. The program: اولا : اى رقم يقبل القسمة على الواحد وكذلك على نفسه ففى هذه المسألة لازم نختبر الرقم لو يقبل القسمة على اى رقم اخر غير الواحد او نفسه نستبعده (يعنى لو باقى القسمة كان بيساوى صفر دة معناه ان الرقم مش (prime مثلا: 4 طبعا كباقى الارقام تقبل القسمة على نفسها وعلى الواحد ولكنها ايضا تقبل القسمة على 2 يعنى عندما نختبر باقى قسمة 4 على 2 سينتج صفر لذلك رقم 4 مش هيكون prime ملحوظة: الواحد مش prime

9 Ex.3 # include <iostream.h> int main() { cout<<"Enter the number that you will check : "; int num ; int count=0; //عداد نقدر من خلاله نعرف هل الرقم الذى نختبره يقبل القسمة على اى عدد غير الواحد ونفسه ام لا cin>>num; cout<<endl; for(int i=2 ; i<num ; i++) //بدأنا ب 2 لان اى رقم بيقبل القسمة على واحد if(num%i==0) //لو الشرط تحقق count++; //هيزيد واحد ودة معناه ان الرقم الذى بنختبره بيقبل القسمة على عدد تانى غير"الواحد ونفسه" لذلك نستبعده counter } if(num==1 || num==0) cout<<" This number can't describe as prime\n"; else if(count>0) //يعنى العداد زاد عندما العدد قبل القسمة على رقم غير نفسه والواحد cout<<num<<" is not prime number\n"; else cout<<num<<" is a prime number\n"; return 0;

10 Arrays

11 Notes When we need to group or list many data items of the same type into one group we use the “Array”. An array can have different values since each item has different location in the array but the array can’t store many different types of values , (such int, char and float) How to declare the array? int num[4]; where “int”  type of array “num”  array name “ [4] ”  array size int num[ 4 ] = { 10 , 23 , 12 , 17 }; int num[ ] = { 10 , 23 , 12 , 17 }; // we don’t need to declare the size

12 Notes The items in an array are called elements.
The index of the first elements is 0 while the index of the last item is size-1 Ex. The first element  age[0] The last element  age[3] int arr[5]={1,4,6}; That mean: arr[0] =1 arr[1] =4 arr[2]=6 arr[3]=0 arr[4]=0 The two dimensional array is array of arrays.  arr[x][y] arr[3][3]  #include<iostream.h> int main() { int arr[5]={1,4,6}; for(int i=0 ;i<5;i++) cout << "arr[" << i << "] = " << arr[i] << endl; return 0; } y x

13 Ex.1 Write a C++ program that accepts 7 integers from the user, stores them in an array.

14 Ex.1 Answer #include<iostream.h> int main() { int arr[7] , sum=0 ; for(int i=0;i<7;i++) // this "for loop" for entering numbers and storing it in the array arr[ ] cout<<"Enter number: "<<endl; cin>>arr[i]; } return 0;

15 Ex.2 Write a C++ program that accepts 7 integers from the user, stores them in an array, and then calculates the summation of them.

16 Ex.2 Answer #include<iostream.h> int main() { int arr[7] , sum=0 ; for(int i=0;i<7;i++) // this "for loop" for entering numbers and storing it in the array arr[ ] cout<<"Enter number: "<<endl; cin>>arr[i]; sum+=arr[i]; } cout<<"The summation = "<<sum<<endl; return 0;

17 Ex.3 Write a C++ program that accepts 10 integers from the user, stores them in an array, and then calculates the summation and the average of the even numbers.

18 Ex.3 answer #include<iostream.h> int main() { int arr[10] , count=0; float sum=0 ; float avrg=0; for(int i=0;i<10;i++) cout<<"enter number: " ; cin>>arr[i]; if(arr[i]%2==0) sum+=arr[i]; count++; } avrg=sum/count; cout<<"the summation is: " << sum <<endl; cout<<"The average is: " << avrg << endl; return 0;

19 Ex.4 Write C++ program that accepts 3 numbers then prints them in a reverse order.

20 Ex.4 Answer #include<iostream.h> int main() { int arr[3] ; for(int i=0;i<3;i++) // this "for loop" for entering numbers and storing it in the array arr[ ] cout<<"Enter number: "<<endl; cin>>arr[i]; } for(i=2;i>=0;i--) cout<< arr[i] << endl; return 0;

21 Ex.5 Write a program that accepts 10 integers from the user, stores them in an array, and then calculates and prints the max and min number.

22 Ex.5 #include<iostream.h> int main() { int arr[10] , max , min ; for(int i=0;i<10;i++) // this "for loop" for entering numbers and storing it in the array arr[ ] cout<<"Enter a number: "<<endl; cin>>arr[i]; } max = arr[0]; min= arr[0]; for(i=0;i<10;i++) if(arr[i] > max) max=arr[i]; else if(arr[i]<min) min=arr[i]; cout<<"The max number = "<<max<<endl <<"The min number = " << min<< endl; return 0;

23 Nested for loobs Astrerisks

24 Write a program that prints the following patterns:

25 #include<iostream
#include<iostream.h> int main() { for(int line =1;line<=5;line++) for(int s=line;s<=4;s++) cout<< " "; } for(int ast=1;ast<=2*line-1;ast++) cout<<"*"; cout<<endl; for(int line =4;line>=1;line--) system("pause"); return 0;


Download ppt "Lab 5."

Similar presentations


Ads by Google