Admin-Luchifer Admin
Posts : 10 Join date : 30/01/2011
| Subject: belajar asas c++ Tue Mar 01, 2011 5:43 pm | |
| - Code:
-
#include<iostream.h> void main() { int a; cout<<âSila Masukkan Satu Nombor:â; cin>>a; cout<<âNombor yang dimasukkan ialah:â<<a; cout<<endl; } - Quote :
- #include<iostream.h> - kepala fail (header file) dan mesti ada untuk memulakan sesuatu program (preprocessor).
void main() - arahan fungsi utama (directive function main).
{ - arahan blok fungsi dan kod aturcara akan ditulis didalamnya.(function block)
int a; - isytihar pembolehubah integer a.
cout<<âSila Masukkan Satu Nombor:â; - paparkan kepada pengguna untuk memasukkan satu nombor.
cin>>a; - nombor yang dimasukkan tadi diletakkan sebagai integer a.
cout<<âNombor yang dimasukkan ialah:â<<a; - paparan keluaran bagi integer a.
cout<<endl; - paparan pada baris berikutnya (new line).
} - penutup arahan blok fungsi (end block) yang mana segala kod aturcara yang telah ditulis didalam blok ini ialah hasilnya/program yang telah dihasilkan. - Code:
-
#include<iostream.h> // preprocessor
void main() // main function
{ // starting function body char name[10]; const int Year=2009; // define variable int YearBorn,Age;
cout<<"This program is use to calculate your age"<<endl; // show output to user "This program is use to calculate your age"
cout<<"and determine whether you are qualify to vote"<<endl; // show output to user "and determine whether you are qualify to vote"
cout<<endl<<"Please key-in your name : "; // endl is use to show output in new line // show output to user "Please key-in your name"
cin.getline(name,20); // data will key in to variable name and cannot over than 20 letters
cout<<endl<<"YearBorn : "; // show output to user "YearBorn"
cin>>YearBorn; // data will key in to variable YearBorn cin.get(); // cin.get is use for variable interger and to recall back data
Age=Year-YearBorn; // calculate age
cout<<endl<<"Hello, "<<name<<endl<<"You are "<<Age<<" years old."<<endl; // show output to user "Hello" and recall data from variable name and in new line show output "You are" and recall data from variable age and show output " years old."
if(Age>21) // selection structure where's age must over than 21
cout<<"You are qualify to vote!"<<endl; // show output to user "You are qualify to vote!"
else // selection structure else if user under 21
cout<<"Sorry, You are not qualify to vote!"<<endl; // show output to user "Sorry, You are not qualify to vote!"
cout<<"Thank you for using this program!"; // show output to user "Thanks you for using the program!"
} // ending function body - Code:
-
#include<iostream.h> // preprocessor void main() // main function { // beginning function body int choice; // define variable
cout<<"This program will display a help message."<<endl; // show output to user "This program will display a help message".endl is use to show output in new line. cout<<endl<<"Please choose between 1-3 :"<<endl; // show output to user Please choose between 1-3
cout<<endl<<"1. for"; // show output to user 1. for cout<<endl<<"2. if"; // show output to user 2. if cout<<endl<<"3. switch"; // show output to user 3. switch
cout<<endl<<"Please enter your choice : "; // show output to user Please enter your choice
cin>>choice; // data will key in to variable choice cin.get(); // cin.get is use for recall back data variable int
switch(choice) // selection structure choice { // beginning function body for selection structure case 1: // first choice that user choose
cout<<endl<<"for is c++ most versatile loop."<<endl; // show output to user "for is c++ most versatile loop"
cout<<endl<<"Hope this message will help you."<<endl; // show output to user "Hope this message will help you."
break; // to break case 1
case 2: // second choice that user choose
cout<<endl<<"if is c++ conditional branch statement."<<endl; // show output to user "if is c++ conditional branch statement."
cout<<endl<<"Hope this program will help you."<<endl; // show output to user "hope this program will help you."
break; // to break case 2
case 3: // third choice that user choose
cout<<endl<<"Switch is c++ multiway branch statement."<<endl; // show output to user "Switch is c++ multiway branch statement."
cout<<endl<<"Get more help in c++ with this program."<<endl; // show output to user "Get more help in c++ with this program."
break; // to break case 3
default: // others choice that user choose
cout<<endl<<"You must enter a choice between 1 to 3.\a"; // show output to user "You must enter a choice between 1 to 3.".\a is for alert .
cout<<endl;
} // ending function body for selection structure
cout<<endl<<"Thank you for using this program!"<<endl; // show output to user "Thank you for using this program"
} // ending function body | |
|
Admin-Luchifer Admin
Posts : 10 Join date : 30/01/2011
| Subject: Re: belajar asas c++ Tue Mar 01, 2011 5:45 pm | |
| - Code:
-
#include<iostream.h> // preprocessor
int main() // main function { // beginning function body int nPembilang=1; // define variable
while(nPembilang<=10) // repetition structure and limit data is equal and not over than 10
{ // begining function body for repetition structure cout<<" "<<nPembilang; // display output to user for data nPembilang ++nPembilang; // data nPembilang will added by 1 till the limit } // ending function body for repetition
return 0; // no returning value } // ending function body - Code:
-
#include<iostream.h> // preprocessor
int main() // main function { // beginnign function body int nsisi; // define variable
cout<<"Masukkan panjang sisi kubus : \n"<<"(Taip 0 untuk tamatkan atur cara)\n"; //display output to user "Masukkan panjang sisi kubus" and in new line display (Taip 0 untuk tamatkan atur cara). \n is use to show output in new line.
cin>>nsisi; // data will key in to variable nsisi
while(nsisi>0) // repetition structure where's nsisi must over than 0 { // beginning function body for repetition structure
cout<<"Isipadu kubus adalah : "<<nsisi*nsisi*nsisi<<'\n'; // display output to user "Isipadu kubus adalah" and calculate data nsisi
cin>>nsisi; // data will key in to variable nsisi } // ending function body for repetition structure
return 0; // no returning value } | |
|