Cpp c++ 1

354 views
246 views

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
354
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Cpp c++ 1

  1. 1. #include<iostream.h> void main() { int x; cin>>x; if(x<=50) x=x+1; x=x+1; if(x<=5 { x++; x++; } cout<<"x :"<<x<<endl; } #include<iostream.h> void main () { int r ; cout<<" you can enter any value to exit enter 0 "; while ( r!=0) { cout<<"n"; cin>>r; } } For addtation For aganst
  2. 2. Adobe Acrobat Document Adobe Acrobat Document ńȁȋơƧƾƷȂdzơ ƨǤǴƥƨůŐdzơńƛƨ ƾǬǷ ǷC++ 1.0 :ƧƾƷȂdzơǽǀǿƨȇƢȀǼƥ ♦Ʋ ơǂƥƨƥƢƬǯǺǺǰ ƬƬLJǷ Ƿ ǸC++.ƨǘȈLjƥ ♦.ƱơǂƻȍơȁDZƢƻƽȍơƩơǁƢƦǟǵơƾƼƬLJơǺ Ǻǰ ƬƬLJǷ Ǹ ♦ĿƨȈLJƢLJȋơƩƢǻƢȈƦdzơǝơȂǻƗȄǴǟǥǂǠƬƬLJC++. ♦ĿƨȈƥƢLjūơDzơȂǠdzơDZƢǠƬLJƛǺǺǰ ƬƬLJǷ Ǹ Ƿ ǸC++. ♦ĿƨȈǬƟȐǠdzơDzơȂǠdzơȄǴǟǥǂǠƬƬLJǷC++. OmerAhmedSaeed Î-ÏȆǴȈdzƾdzơǦǴŭơ #include iostream.h ŐƬǠƫƨǤdzCƼǓƗǶƨǤdzǮdzƿȁŐǰdzƢȀƴƷǸƽƾǠƫȁƢēƢƦƬǰ Ƿ ƾǼǟDzƢǠƬdzơǷƢȀǠǷǺǷDZȐƻƔƢnjǻƛƲƢǻǂƥǷƢǷƬȇǶDzƢǠƬdzơǷƨƥƢƬǰ ƥƩƢǨǴŭơƨȈǴȈdzƾdzơ ƨȈLJƗǂdzơȁHeader FilesȆLjƫǸƆƢǔȇƗƩƢǨǴŠś ǔƬdzơǸInclude Files ƬȇǶǮdzƿĿƨǤdzCǺǷDZȐƻƧǁƢƦǠdzơƨȈdzƢƬdzơ řǠƫDzȈ ŢǸǦǴŭơȄǴȈdzƾdzơiostream.hńƛƧǂǯơƿƣȂLJƢūơŕƷ ŘLjƬȇƳǂƬǴdzǶ ǸƨǧǂǠǷȁƗƨŦǂƫƩƢȈǴǠdzơǸƨǏ ƢŬơDZƢƻƽȍƢƥƱơǂƻȍơȁ ǚƷȐǻƗǹơǀǿǦǴŭơǾdzLJơǶƾȇƾƬdzơǸ.hǁƢǐ Ƭƻơºdzheader file ƽƽơƾǟơśǴȈǼdzơƨǠƢƳƾȈǠLJ ƾŧ ơǂ ǟǷ Ǹ 1)Γή΋΋ΩΔΣΎδΔϋΎΒσΏΎδΤΞ ΎήΑΐΘ΋΋ϣ ϭ ϣϧϟϛarea = PI * r * r( )Ύτ ΤϬ ϣϭϴcircumference = 2 * PI * r.( )Ύ ήτ μ ΋Ζ ϋ΋Ϋ·ϫ ϒ ϧϥ ϤϠϗrΎδϱϭ ϳ (5.2΋΋ϥ ϭPI = 3.1415926 #include iostream.h main() { float r, PI; r = 5.2; PI = 3.1415926; coutarea = PI * r * rn; coutcircumference = 2 * PI * r; } 2) σ ΎΧΩΞ ΎήΑΐΘ ΋Ϯ ϣϧϝ ϝ Ϲ ϛlength)νήϋ ϭ(width. τ ΘδϞ ϣϴ ( ) τΘδ ΋Δ ΎΣϊΒτ ΐδΤϞ Ϥ ϓ ϳϭ ϳϭϴ ϟperimeter = 2*(length+width)) ΘΣΎδϪ ϣϭ(area=length*width( #include iostream.h main() { int length, width; coutlength = ; cinlength; coutwidth = ; cinwidth; coutperimeter = 2*(length+width)n; coutarea = length*width; } 3)Ω΋ ϊΑέ΋ΕΎΟέΩΓ΋΋ή Ξ ΎήΑΐΘ ΋Ϯϣ ϣϧϘϟ ϛm1, m2, m3, m4( ) Ϊό ΋ϊΒτ ΐδΤ ΛϤ ϳϭ ϳϢϝ ϟavg = (m1+m2+m3+m4)/4( #include iostream.h main() { int m1, m2, m3, m4; coutEnter marks n; cinm1m2m3m4; coutavg = (m1+m2+m3+m4)/4; } 4ήτδ ΋ γ΋ΔϋΎΒτΞ ΎήΑΐΘ΋΋ϲϓϚϤ ϣϧϟ ϟϛ΍ϭϝ ϷΔ Π ΋Ϭ Ϧϣϟήδ ΍ϯ ϴϟ΋ϋ΋΋ϲϓϚϧ ϮϨϭ ˬςγ ϭήτδ ΋ϟΚ ΎΜ΍ϟϟΔ δ Π ΋Ϩ ϭϴ ϟ ˬ ΋Δ Π ΋ϰϨϤ Ϭ ϲϓϴϟ ϟ #include iostream.h main() { coutAbdulla Awad nnttt; coutBaitBoss zone ttt Yemeni; } α1ΎΩΪϋΔΤΤλΩ΋Ϊϋ΋Ε΋Ϋ Θ μ ήλΎ ϋΓ΋΋ή Ξ ΎήΑΐΘ ΋ϫ Ϧ ϓϮϔϣ Ϩ ϣϧϴ ϴϘ ϟ ϛ . ΋Ϊ ΋΋ΪΨΘγΎΑϭϝ ϟϡ5ΛϢ . )Δ Ο ΰ ΋ήλΎ ό ΋ΔϋΎΒσϭ Ϩϴ ϟ ϟEven)Δ Ωή ΋ϳ ϔϭϟ (Odd.Δ μϓϮϔϣϞϜϟ( #include iostream.h read(int ar[]) { coutEnter 5 numbers:; for(int i=0; i=4; i++) cinar[i]; } isEven(int x) { if(x % 2 == 0) return 1; return 0; } isOdd(int x) { return (!isEven(x)); } printEven(int ar[]) { coutEven numbers: ; for(int i=0; i=4; i++) if(isEven(ar[i])) coutar[i] ; coutendl; } printOdd(int ar[]) { coutOdd numbers: ; for(int i=0; i=4; i++) if(isOdd(ar[i])) coutar[i] ; coutendl; } main() { int a[5]; read(a); printEven(a); printOdd(a); int b[5]; read(b); printEven(b); printOdd(b); } #includeiostream using namespace std; int larger(int,int); void main() { int a=2,b=6; coutlarger(a,b)is largeendl; } int larger(int x,int y) { if(xy) return x; else return y; } #includeiostream using namespace std; int larger(); void main() { coutlarger()is largerendl; } int larger() { int x=2,int y=6; if(xy) return x; else return y; } 1) #includeiostream using namespace std; int larger(); void main() { coutlarger()is largerendl; } int larger() { int x=2,int y=6; if(xy) return x; else return y; } #includeiostream using namespace std; void line(char,int); int main()
  3. 3. #include iostream.h void main() { int num1,num2; char ch; cinnum1; coutya doye daxel bka; cinnum2; coutenter aletter; cinch; if((ch=='A')||(ch=='a')) coutdiffrence is :num1+num2endl; else coutInvalid letterendl; } This is number additation and litter
  4. 4. #include iostream.h void main() { int x; for(x=1; x=15 ; x=x+2) coutxendl; } 1,3,5,7,9,11,13,15 #include iostream.h void main() { for(int x=2; x=10 ; x++) coutxendl; } 2,3,….10 #include iostream.h void main() { int x,i; cin x; i=2; while (xi) { if (x%i==0) { coutthe number in not prime; i=x+1; } else if(x % i !=0) i++; } if(x==i) coutthe number is prime; }
  5. 5. #includeiostream.h void main() { int a,b,c,i; char s=' '; a=b=1; coutasbs; for(i=3;i=10;i++) { c=a+b; coutcs; a=b; b=c; } } 1 1 2 5 8 13 21 34 55 Fibonacci Series #includeiostream.h void main() { int a,b,c,i,n; char s=' '; a=b=1; cinn; coutasbs; for(i=3;i=n;i++) { c=a+b; coutcs; a=b; b=c; }} insert n 1 1 2 5 8 13 21 34 55
  6. 6. #includeiostream.h void main () { float sum=0,average,count; count=0; for(float i=10.5;i=99.5;i=i+10) { sum=sum+i; count++; } coutsumendl; average=sum/count; coutaverageendl; }
  7. 7. #includeiostream.h void main () { int flag=0,n; cinn; for(int i=2;inflag==0;i++) if (n%i==0) flag=1; if(flag==1) coutprimeendl; else cout not primeendl; }
  8. 8. #includeiostream.h void main () { int flag=0,n; cinn; for(int i=0;in;i++) { if (n%i==0) flag=1; } if(flag==0) coutprimeendl; else cout not primeendl; }
  9. 9. #include iostream.h int sum(int); void main () { int x ; cout sumion is :- sum (x) ; } int sum (int a) { int su =0 ; for(int i=1 ; i=50 ;i++) su+=i ; return su ; } #includeiostream.h void main () { int sum=0; for(int i=0; i=50; i++) { sum+=i; } cout the sum of number from 0 to 50 is endl; coutsumendl; }
  10. 10. #include iostream.h void main () { int x ; coutEnter one number 't'; cinx ; for (int i=1 ;i=x ;i++) { for (int j=1 ;j=x ;j++) couti*j = i*j endl ; cout endl ; } coutendl ; cout-------------- ; } ‫ن*ن‬ ‫ت‬ ‫جيكه‬ ‫دي‬ ‫ضربي‬ ‫جدول‬ #include iostream.h int po (int ,int) ; int main () { int x ,y ; coutEnter x ,y ; cinxy ; coutnx to po y=po(x,y)endl ; return 0 ; } int po (int a , int b ) { int p= 1 ; for (int i=1 ; i=b ;i++) p=p*a ; return p ; } Enter x,y 5 6 X to po y =15625
  11. 11. #include iostream.h void main ( ) } int x; cin x; cout x endl; cout x *2endl; cout x *3endl; cout x *4endl; cout x *5endl; cout x *6endl; cout x *7endl; cout x *8endl; cout x *9endl; {
  12. 12. #include iostream.h void main () { int x ; coutEnter number x endl ; cinx ; if ( x%2 == 0) coutNumber is even:- x ; else if ( x%2 != 0) coutNumber is odd :- x ; }
  13. 13. #include iostream.h void main ( ) { // an integer number int Num,i; // numebr of numbers int Num_Num; // total of numbers int Sum = 0; cout Enter the number of numbers : ; // get the number of numbers from user cin Num_Num; // loop from 1 to Num_Num. for (int Counter = 1; Counter = Num_Num ; i++ ) { cout Enter number ( Counter ) :; // get the number from user cin Num; // put the number in sum Sum += Num; } // end for // print the result cout The Result is : Sum ; } // end main
  14. 14. #include iostream.h int Pluse ( int, int ); int Minus ( int, int ); int Div ( int, int ); int Mult ( int, int ); void main ( ) { int Fir_Num; int Sec_Num; char Ope; cout Enter the first number : ; cin Fir_Num; cout Enter the second number : ; cin Sec_Num; cout Enter ur operation : ; cin Ope; switch (Ope) { case ' + ' : cout The result is : ; break; case ' - ' : cout The result is : Minus ( Fir_Num, Sec_Num ); break; case '*' : cout The result is : Mult ( Fir_Num, Sec_Num ); case ' / ' : cout The result is : Div ( Fir_Num , Sec_Num ); break ; default : cout Sorry ur operation is wrong ; } } int Pluse ( int x, int y ) { return x + y ; } int Minus ( int x, int y ) { return x - y; } int Div ( int x, int y ) { if ( y == 0 ) { cout Error division by zero ; return 0; } else return x / y; } int Mult ( int x, int y ) { return x * y ; }
  15. 15. #include iostream.h void main( ) { int Fir_Num; int Sec_Num; cout Enter the first number : ; cin Fir_Num ; cout Enter the second number : ; cin Sec_Num; cout The Result is : Fir_Num + Sec_Num ; }
  16. 16. #include iostream.h void main ( ) { int Num_Of_Sub; int Grade; int Total = 0; int Average; cout Plz enter number of subjects : ; cin Num_Of_Sub; for (int Counter = 1; Counter = Num_Of_Sub; Counter++) { cout Enter grade number ( Counter ) : ; cin Grade; if (Grade = 50 Grade = 100) Total += Grade; else { cout Sorry ...grade is out of range !!!!! try againn; Counter--; } } Average = Total / Num_Of_Sub ; cout The average is : Average ; }
  17. 17. #includeiostream.h void main() { long d1 , d2; char let; do{ coutenter the first numbern; cind1; coutenter the second numbern; cind2; cout the result of multiple two numbers is :n; coutd1*d2; coutn if you want to do again press any letter or number unless a n; cinlet; coutn; }while(let!='a'); } B y t h is y ou c a n (n u m *n u m ) a n d y ou a g a in a n y t im e in t e r (c h ,n u m )
  18. 18. #includeiostream.h void main () { int k; cout enter your number : n ; cin k; if ( k50) cout the number is less than 50 ; else coutava darkatendl; } If and else
  19. 19. #include iostream.h void main() { int z,y; cout Inter your longendl; cin z ; cout Inter friend's longendl; cin y; cout --------------------endl; if(zy) { cout You longerendl; cout Your long is zendl; coutendl; cout The average longs are ; cout (z+y)/2; } else { cout Your friend is longerendl; cout He's long is yendl; cout y*2 = y*2endl; cout y/2 = y/2; } }
  20. 20. #includeiostream.h void main() { for(int i=1;i7;i++) { for(int j=1;j10;j++) { cout*; } coutn; } } ********** ********** ********** ********** ********** ********** ********** #includeiostream.h void main() { int sum; for(int i=1;i=8;i++) { for(int j=i;j=9;j++) { cout=; } sum+=j; coutn; } } ======== ======= ====== ===== ==== === == = + ++ +++ +++++ ++++++ +++++++ ++++++++
  21. 21. #include iostream.h void main() { int x; cin x; switch (x) { case 1: cout welcomeendl; break; case 2: cout hello studentendl; break; case 3: cout introdaction to c++endl; break; default: cout bye bye; } }
  22. 22. cout its a Saturnendl; break; case 7: cout its a URANUSendl; break; case 8: cout its a NEPTUNEendl; break; case 9: cout its a PLUTOendl; break; case 10: cout its a senda endl; break; default: cout err no plant to resemble this numberendl; break; } } #include iostream.h void main() { int x; cout inter number a planet endl; cin x; switch (x) { case 1: cout its a Mercuryendl; break; case 2: cout its a Venusendl; break; case 3: cout its a earthendl; break; case 4: cout its a Marsendl; break; case 5: cout its a Jupiterendl; break; case 6: About switch
  23. 23. #includeiostream.h main() { int x; coutenter the decimal number:; cin x; while(x=12) {switch(x) { case 1: {coutIendl; coutenter the decimal number:;} break; case 2: {coutIIendl; coutenter the decimal number:;} break; case 3: {coutIIIendl; coutenter the decimal number:;} break; case 4: {coutIVendl; cout enter the decimal number:;} break; case 5: {coutVendl; coutenter the decimal number:;} break; case 6: {coutVIendl; coutenter the decimal number:;} case 8: {coutVIIIendl; coutenter the decimal number;} break; case 9: {coutIXendl; coutenter the decimal number;} break; case 10: {coutXIendl; coutenter the decimal number;} break; case 11: {coutXIIendl; coutenter the decimal number;} break; case 12: {coutXIIIendl; coutenter the decimal number:;} break; default: coutNOOOO; break; } cinx; } } break; case 7: {coutVIIendl; coutenter the decimal number:;} break;
  24. 24. #include iostream.h void main() { int x, sum; sum = 0; do { cinx; sum+=x; }while (x!=0); coutsum of number is =sum; }
  25. 25. #include iostream.h void main () { for (char x=65;x=90;x++) cout x; } a……..z #include iostream.h void main () { for (int x='A' ;x='Z' ;x++) cout x; } 65……..90 #include iostream.h void main () { float L, M; char ch; do { cout nEnter L: ; cin L; cout Enter M: ; cin M; cout nL+M= L+M; cout nL-M= L-M; cout nL*M= L*M; if (M != 0) cout nL/M= L/M; cout n Do Again (y/n): ; cin ch; } while (ch != 'n'); } Other more programming W hile for again
  26. 26. #include iostream.h void main ( ) { for (int i = 0; i 15; i++) cout i * i ; } #include iostream.h void main () { int x = 10; cout x = x endl; cout n ++x = ++x endl; cout x++ = x++ endl; cout n x = x endl ; cout n --x = --x endl; cout x-- = x-- endl; cout n x = x endl ; } For information
  27. 27. #include iostream.h #include math.h void main () { int x, y, P ; cout Enter a number: ; cin x ; cout Enter a power : ; cin y ; P = pow (x,y) ; cout The result is : P endl ; } #include iostream.h #include math.h void main ( ) { float num, ans; cout Enter a number: ; cin num; ans = sqrt(num) ; cout square root is: ans endl; } Calculus example
  28. 28. #include iostream.h int sqr (int n) { int r; r=n*n; return r; } void main() { int k; cink; coutsqr(k)endl; } Function #include iostream.h int sqr (int); void main() { int k; cink; coutsqr(k)endl; } int sqr (int n) { int r; r=n*n; return r; }
  29. 29. #include iostream.h void fun() { int i=10; coutiendl; } void main() { int i=5; fun(); coutiendl; } 10 5 #include iostream.h void azo() { int i=10; coutiendl; } void main() { int i=5; coutiendl; azo(); coutiendl; } 5 10 5 #include iostream.h void azo(int n) { n++; coutnendl; } void main() { int n=5; azo(n); coutnendl; } 6 5 #include iostream.h void main() { int x=100,y=5; int temp; temp=x; x=y; y=temp; coutxyendl; }
  30. 30. #includeiostream.h int fact (int); void main () { int sum=0,i,n; cinn; for(i=0;i=n;i++) sum+=fact(i); coutsum = sum; } int fact (int n) { int i,s; s=1; for(i=n;i=1;i--) s*=i; return s; } 2—sum=4,3—sum=10 4—sum=34,5—sum=154 Sum = 1 + x^1 + x^2 + x^3 + x^4 + x^5 #includeiostream.h #includemath.h void main() { int sum=0,x,i; cinx; for(i=5;i=1;i--) sum+=pow(x,i); coutsum; }
  31. 31. #include stdio.h void main ( ) { FILE * f; f=fopen(d:stdudent.txt,azad lesson); fprintf(f, welcome to C++); fclose(f); } file #includeiostream.h #includestdio.h #includestdlib.h void main() { FILE *out; int score; if((out=fopen(test.txt,w))==NULL) { coutcan not open filen; exit(-1); } coutenter a test score (0 terminate input); cinscore; while(score !=0) { fprintf(out,%dn,score); cout enter anather score; cinscore; } fclose(out); }

×