C++ Sample Codes (Data Structure)

2,517 views

Published on

C++ Sample Codes with Output

  • Be the first to comment

C++ Sample Codes (Data Structure)

  1. 1. comscicomscicomscicomscicomscicomsci comscicomscicomscicomscicomscicomsci comscicomscicomscicomscicomscicomsci comscicomscicomscicomscicomscicomsci comscicomscicomscicomscicomscicomsci comscicomscicomscicomscicomscicomsci comscicomscicomscicomscicomscicomsci comscicomscicomscicomscicomscicomsci comscicomscicomscicomscicomscicomsci comscicomscicomscicomscicomscicomsci comscicomscicomscicomscicomscicomsci comscicomscicomscicomscicomscicomsci comscicomscicomscicomscicomscicomsci comscicomscicomscicomscicomscicomsci comscicomscicomscicomscicomscicomsci comscicomscicomscicomscicomscicomsci comscicomscicomscicomscicomscicomsci comscicomscicomscicomscicomscicomsci comscicomscicomscicomscicomscicomsci comscicomscicomscicomscicomscicomsci PROJECT in COSC 60 Data Structure Jenelyn B. Pañoso BSCS II
  2. 2. SIMPLE PROGRAMS  ARITHMETIC OPERATIONS  CODES: #include<iostream.h> #include<conio.h> main( ) { clrscr( ); int n1, n2, a, s, m; float d; cout<<“Enter two integersn”; cin>>n1; cin>>n2; a = n1 + n2; s = n1 - n2; m = n1 * n2; d = n1 / (float)n2; cout<<“nSumt = “<<a; cout<<“nDifference = “<<s; cout<<“nProductt = “<<m; cout<<“nQoutient = “<<d; getch( ); return 0; } OUTPUT:
  3. 3.  VOLUME OF CYLINDER  CODES: #include<iostream.h> #include<conio.h> #define Pi 3.1416 main( ) { clrscr( ); float VOLUME,radius,height; cout<<“This program will compute the VOLUME of CYLINDER with an input of height and radius.n”; cout<<“Enter radius of CYLINDER: “; cin>>radius; cout<<“Enter height of CYLINDER: “; cin>>height; VOLUME = Pi*height*radius*radius; cout<<“nVOLUME:”<<VOLUME; getch( ); return 0; } OUTPUT:
  4. 4.  MASS  CODES: #include<iostream.h> #include<conio.h> main( ) { clrscr( ); int a,b,c,gram; float d,e,f; cout<<“Enter GRAM: “; cin>>gram; a= 1000*gram; b= 100*gram; c= 10*gram; d= 0.1*gram; e= 0.01*gram; f= 0.001*gram; cout<<“Milligramt= “<<a; cout<<“nCentigramt= “<<b; cout<<“nDecigramt= “<<c; cout<<“nDecagramt= “<<d; cout<<“nHectogramt= “<<e; cout<<“nKilogramt= “<<f; getch( ); return 0; } OUTPUT:
  5. 5.  VOLUME OF A CONE  CODES: #include<iostream.h> #include<conio.h> #define Pi 3.1416 main( ) { clrscr( ); float VOLUME,radius,height; cout<<“This program will compute the VOLUME of CONE with an input of height and radius.n”; cout<<“Enter radius of CONE: “; cin>>radius; cout<<“Enter height of CONE: “; cin>>height; VOLUME = Pi*height*radius+Pi*radius*radius; cout<<“nVOLUME:”<<VOLUME; getch( ); return 0; } OUTPUT:
  6. 6. IF ELSE STATEMENT  NEGATIVE OR POSITIVE  CODES: #include<iostream.h> #include<conio.h> main( ) { clrscr( ); int n; cout<<"Enter a number: "; cin>>n; if(n>=0) cout<<"POSITIVE"; else cout<<"NEGATIVE"; getch( ); return 0; } OUTPUT:
  7. 7. NESTED IF ELSE STATEMENT  COMPARISON  CODES: #include <iostream.h> #include <conio.h> main( ) { clrscr( ); int num1 , num2; cout<<"Enter two numbers:n"; cin>>num1>>num2; if (num1>num2) cout<<num1<<" is greater than "<<num2; else if (num1<num2) cout<<num1<<" is less than "<<num2; else if (num1==num2) cout<<num1<<" is equal to "<<num2; else cout<<"INVALID INPUT"; getch( ); return 0; } OUTPUT:
  8. 8. SWITCH-CASE STATEMENT  HOROSCOPE  CODES: #include<iostream.h> #include<conio.h> main( ) { clrscr( ); int z; cout<<"[1] t ARIES t March 21 - April 19n[2] t TAURUS t April 20 - May 20n[3] t GEMINI t May 21 - June 20n[4] t CANCER t June 21 - July 22n[5] t LEO t t July 23 - August 22 n[6] t VIRGO t t August 23 - September 22n[7] t LIBRA t t September 23 - October 22n[8] t SCORPIO t October 23 - November 21n[9] t SAGITTARIUS t November 22 - December 21n[10] t CAPRICON t December 22 - January 19n[11] t AQUARIUS t January 20 - February 18n[12] t PISCES t February 19 - March 20nn"; cout<<"Choose your Zodiac Sign in numbers from [1-12]: "; cin>>z; switch(z) { case 1: cout<<"nARIES t March 21 - April 19nCharacteristics: Active, Demanding, Determined, Effective, Ambitious"; break; case 2: cout<<"nTAURUS t April 20 - May 20nCharacteristics: Security, Subtle strength, Appreciation, Instruction, Patience"; break; case 3: cout<<"nGEMINI t May 21 - June 20nCharacteristics: Communication, Indecision, Inquisitive, Intelligent, Changeable"; break; case 4: cout<<"nCANCER t June 21 - July 22nCharacteristics: Emotion, Diplomatic, Intensity, Impulsive, Selective"; break; case 5: cout<<"nLEO t July 23 - August 22 nCharacteristics: Ruling, Warmth, Generosity, Faithful, Initiative"; break; case 6: cout<<"nVIRGO t August 23 - September 22nCharacteristics: Analyzing, Practical, Reflective, Observation, Thoughtful"; break; case 7:
  9. 9. cout<<"nLIBRA t September 23 - October 22nCharacteristics: Balance, Justice, Truth, Beauty, Perfection"; break; case 8: cout<<"nSCORPIO t October 23 - November 21nCharacteristics: Transient, Self-Willed, Purposeful, Unyielding"; break; case 9: cout<<"nSAGITTARIUS t November 22 - December 21nCharacteristics: Philosophical, Motion, Experimentation, Optimism"; break; case 10: cout<<"nCAPRICON t December 22 - January 19nCharacteristics: Determination, Dominance, Perservering, Practical, Willful";break; case 11: cout<<"nAQUARIUS t January 20 - February 18nCharacteristics: Knowledge, Humanitarian, Serious, Insightful, Duplicitous"; break; case 12: cout<<"nPISCES t February 19 - March 20nCharacteristics: Fluctuation, Depth, Imagination, Reactive, Indecisive"; break; default: cout<<"nINVALID INPUT"; } getch( ); return 0; } OUTPUT:
  10. 10. WHILE LOOP STATEMENT  SQUARED and CUBE  CODES: #include<iostream.h> #include<conio.h> #include<dos.h> main( ) { clrscr( ); int n,s,c; cout<<" SQUARED CUBE"; n=1; while(n<=10){ cout<<"n"<<n; s=n*n; cout<<"t"<<s; c=n*n*n; cout<<"t"<<c; n++; delay(1000); } return 0; getch( ); } OUTPUT:
  11. 11. DO-WHILE LOOP STATEMENT  MULTIPLICATION TABLE  CODES: #include<iostream.h> #include<conio.h> main( ) { clrscr( ); int n1,n2,n3,n4,n5,n6,n7,n8,n9,n10; n1 = 1; cout<<"ttttMULTIPLICATION TABLEnn"; do{ cout<<"t"<<n1; n1 +=1; } while(n1<=9); cout<<"nn"; n2 = 2; do{ cout<<"t"<<n2; n2+=2; } while(n2<=18); cout<<"nn"; n3=3; do{ cout<<"t"<<n3; n3 +=3; } while(n3<=27); cout<<"nn"; n4 = 4; do{ cout<<"t"<<n4; n4 += 4; } while(n4<=36); cout<<"nn"; n5 = 5; do{ cout<<"t"<<n5; n5 += 5; } while(n5<=45); cout<<"nn"; n6 = 6; do{ cout<<"t"<<n6; n6 += 6; } while(n6<=54); cout<<"nn"; n7 = 7; do{ cout<<"t"<<n7; n7 += 7; } while(n7<=63); cout<<"nn"; n8 = 8; do{ cout<<"t"<<n8; n8 += 8; } while(n8<=72);
  12. 12. cout<<"nn"; n9 = 9; do{ cout<<"t"<<n9; n9+=9; } while(n9<=81); cout<<"nn"; n10 = 10; do{ cout<<"t"<<n10; n10 += 10; } while(n10<=90); getch( ); return 0; } OUTPUT:
  13. 13. FOR LOOP STATEMENT  PYRAMID  CODES: #include<iostream.h> #include<conio.h> main( ) { clrscr( ); int row, c, n, temp; cout<<"Enter number of rows: "; cin>>n; temp = n; for ( row = 1 ; row <= n ; row++ ) { for ( c = 1 ; c < temp ; c++ ) cout<<" "; temp--; for ( c = 1 ; c <= 2*row - 1 ; c++ ) cout<<"*"; cout<<"n"; } getch( ); return 0; } OUTPUT:

×