The University of Babylon Department of Software LECTURE NOTES ON PROGRAMMING FUNDAMENTALS II USING C++ LANGUAGE By Dr. Samaher Hussein Ali Collage of Information Technology, University of Babylon, Iraq dr.samaher.hussein@gmail.com 31 March 2013 1 Examples of Functions Q1// program find the root of integer number enter by user #include<iostream.h> #include<math.h> #include<conio.h> float root(int x) { float y; y=sqrt(x); return y; } void main() { clrscr(); int x; cout<<"enter the number to find root to it"<<endl; cin>>x; cout<<root(x) } 31 March 2013 Dr. Samaher Hussein Ali 2 Examples of Functions Q2//program find the factorial of integer number enter by user #include<iostream.h> #include<conio.h> int fact(int x) { int y=1; for(int i=1;i<=x;i++) { y=y*i; cout<<i<<"*"; } return y; } void main() { clrscr(); int x; cout<<"enter the number to find factorial to it"<<endl; cin>>x; cout<<"factoria of "<<x<<"="; cout<<"="<<fact(x); } 31 March 2013 Dr. Samaher Hussein Ali 3 Examples of Functions Q3//program generat random number the array #include<iostream.h> #include<conio.h> #include<stdlib.h> void rand_function(int n) { int seed,x[100]; seed=time(NULL); srand(seed); for(int i=0;i<n;i++) { x[i]=random(10); cout<<x[i]; } } void main() { int n; cout<<"enter the length of array:"<<endl; cin>>n; rand_function(n); } 31 March 2013 Dr. Samaher Hussein Ali 4 Examples of Functions Q4//program cancel repeat number in array #include<iostream.h> #include<conio.h> #include<stdlib.h> int x[100]; void cancel_repeat(int n) { for (int i=0;i<n;i++) for(int j=i+1;j<n;j++) if (x[i]==x[j]) x[j]=0; } void main() { int n; cout<<"enter the length of array:"<<endl; cin>>n; cout<<"enter the arry"<<endl; for(int i=0;i<n;i++) cin>>x[i]; cancel_repeat(n); for(i=0;i<n;i++) { if(x[i]!=0) cout<<x[i]<<" "; } } 31 March 2013 Dr. Samaher Hussein Ali 5 Examples of Functions Q5//program print the number in array that accept divided on it #include<iostream.h> #include<conio.h> int x[100],a[50]; void divition_function(int n) { for (int i=0;i<n;i++) { int count=0; for(int j=0;j<n;j++) if (x[j]%x[i]==0) count++; a[i]=count; } } void main() { int n; cout<<"enter the length of array:"<<endl; cin>>n; cout<<"enter the arry"<<endl; for(int i=0;i<n;i++) cin>>x[i]; divition_function(n); for(i=0;i<n;i++) { if(a[i]==n) cout<<x[i]<<"the array accept divided on it "<<endl; else cout<<x[i]<<"no accept divition array on it"<<endl; } 31 March 2013 Dr. Samaher Hussein Ali 6 Examples of Functions Q6//program print sum of 1......x #include<iostream.h> #include<conio.h> int sumation(int n) { int sum=0; for (int i=1;i<=n;i++) sum=sum+i; return sum; } void main() { int x; cout<<"enter the integer number"<<endl; cin>>x; cout<<"the sumation of "<<x<<endl; cout<<sumation(x); } 31 March 2013 Dr. Samaher Hussein Ali 7 Examples of Functions Q7 // write the function to find the power #include<iostream.h> #include<conio.h> int power(int m,int n) { int f=1; for(int i=0;i<n;i++) f=f*m; return f; } void main() { clrscr(); cout<<power(2,10); getch(); } 31 March 2013 Dr. Samaher Hussein Ali 8 Examples of Functions Q8 //check number is prime or not #include<iostream.h> #include<conio.h> int prime(int n) { int f=1; for(int i=2;i<n-1;i++) if(n%i==0) f=0; return f; } void main() { clrscr(); int x=prime(20); if(x==0) cout<<"not prime"; else cout<<"prime"; getch(); } 31 March 2013 Dr. Samaher Hussein Ali 9 Examples of Functions Q9//write fuction to find the series of febonatshi #include<iostream.h> #include<conio.h> void febonatshi(int n) { int x=0,y=1,z; for(int i=0;i<n;i++) { z=x+y; x=y; y=z; cout<<z<<" "; } } void main() { clrscr(); febonatshi(10); getch(); 31 March 2013 Dr. Samaher Hussein Ali 10 Examples of Functions Q10 //find factorial from number #include<iostream.h> #include<conio.h> inline int factoral(int n) { int f=1; for(int i=1;i<=n;i++) f=f*i; return f; } void main() { clrscr(); cout<<factoral(4); getch(); } 31 March 2013 Dr. Samaher Hussein Ali 11 Examples of Functions Q11 //check the number if even or odd #include<iostream.h> #include<conio.h> void even_odd(int n) { if(n%2==0) cout<<"even"; else cout<<"odd"; } void main() { clrscr(); even_odd(993); getch(); } 31 March 2013 Dr. Samaher Hussein Ali 12 Examples of Functions Q12//find the max number from 10 number #include<iostream.h> #include<conio.h> int max_number(int n) { int x; cin>>x; int max=x; for(int i=1;i<n;i++) { cin>>x; if(x>max) max=x; } return max; } void main() { clrscr(); cout<<"max="<<max_number(4); getch(); } 31 March 2013 Dr. Samaher Hussein Ali 13 Spatial Functions 1. Strlen(S1) Output the length of Sequence ex: char s[10] s=“ali” int y==strlen(s) 2. Strcmp(S1,S2) Use to compare between two string from the similarity code of spatial chat 3. Strcpy(S1,S2) Copy the contain of string and put it on the second string. 4. Strlwr(s1) Covert the upper letter of sting to lower 5. Strcat(S1,S2) To merge between two sequences 6. Strrev(S1) Use to reverse the order of letter in sequence 31 March 2013 Dr. Samaher Hussein Ali 14