Entri Populer

Selasa, 25 Januari 2011

This Beautiful Models for Achievement in Korea



VIVAnews -
 Barbie Ernesta won the award 'Best Asian Super Models 2011' in the event '2011 Asia Model Festival Award '. The event was organized by the Model Center International and the Association of Korean models.    


                                                                                                                                      Nadila Ernesta (Gestina/VIVAnews)
                                                                                                                                                                                                   
"Get off the stage, my knees felt weak, and all the words that had been neatly arranged so dispersed. This is the first experience for me," said Barbie Ernesta at Graha Obor, Jalan Bangka Raya in South Jakarta.
The event was held from 18 to 22 January 2011 in 
the JW Marriott Hotel, Seoul, Korea, followed by models from 12 countries in Asia.Besides                                                                                                                        
Barbie, the star of the movie 'Shutter' Ananda Everingham of Thailand also attended this event.
"Actually there are two events. All I follow, I was interviewed by the Koreans in Jakarta. From there the result was brought to Korea. No I can phone him when told to come to attend the event appreciation in Korea," said movie stars 'Miracle'.
Barbie did not think could get an award at this prestigious event.Moreover, he had a lot to compete with super models from 12 Asian countries, including Japan, Thailand, India and China.
"Hopefully this award can be a trigger of spirits and to pave the way to go international," he said.
Barbie Ernesta began his career in the entertainment world as a model. But the girl was born February 4, 1988 better known as a television movie actress in Indonesia. His name was also recorded once starred in several big screen including 'Miracle', and 'House Ampera'. Through the prestigious event in Asia that, Barbie hopes he can realize the career footsteps of other countries. (Pet)

Program to enter three integers and output the smallest integer using IF.


#include <iostream.h>
#include <conio.h>
int main()
{
clrscr();
int x,y,z,smallest;
cout << "Enter 3 integers : ";
cin>>x>>y>>z;
smallest=x;
if(y<smallest)
smallest=y;
if(z<smallest)
smallest=z;
cout << "The smallest integer out of the 3 integers you typed ";
cout << x << ", " << y << " & " << z << " is : " << "\n" << smallest << "\n";
getch();
return 0;
}
This program takes in three integers x, y and z as a screen input from the user.
It then determines the smallest integer of the three and prints it out using the 'cout' command.

Program to find the sum of either of the diagonals of a 4 x 4 matrix.


#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int x;
int A[4][4],sum=0; //Reading the matrix.
cout << "Enter the elements of the matrix : " << endl;
for(int y=0;y<4;y++)
for (int x=0;x<4;x++)
{
cout << "Element " << x+1 << ", " << y+1 << " : ";
cin>>A[x][y];
}
//Sum of either of the diagonal elements.
for(x=0;x<4;x++)
for(y=0;y<4;y++)
if(x==y)
sum+=A[x][y];
else if(y==4-(1+1));
sum+=A[x][y];
cout << "Sum of either of the diagonal elements is : " << sum;
getch();
}
This program takes in the elements A[x][y] of the 4 x 4 matrix as a screen input from the user.
It then calculates the sum of either of its diagonals and outputs it using the 'cout' command.

Program to enter 10 integers in a single-dimension array and then print out the array in ascending order.


#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int array[10],t;
for(int x=0;x<10;x++)
{
cout << "Enter Integer No. " << x+1 << " : " << endl;
cin>>array[x];
}
for (x=0;x<10;x++)
{
for(int y=0;y<9;y++)
{
if(array[y]>array[y+1])
{
t=array[y];
array[y]=array[y+1];
array[y+1]=t;
}
}
}
cout << "Array in ascending order is : ";
for (x=0;x<10;x++)
cout << endl << array[x];
getch();
}
This program takes in the ten integers array[x] to be stored in the single-dimensional array as a screen input from the user.
It then sorts out these ten integers into ascending order and prints them out using the 'cout' command.

Program to enter a sentence and output the number of uppercase & lowercase consonants, uppercase & lowercase vowels in sentence..


#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
char line[80];
int number_of_vowels,uc,lc,uv,lv;
uc=lc=uv=lv=0;
cout << "Enter your sentence : " << endl;
cin.getline(line,80);
for(int x=0; line[x]!='\0';x++)
{
if(line[x]=='A'||line[x]=='E'||line[x]=='I'||line[x]=='O'||line[x]=='U')
uv++;
else if(line[x]=='a'||line[x]=='e'||line[x]=='i'||line[x]=='o'||line[x]=='u')
lv++;
else if(line[x]>+65&&line[x]<=90)
uc++;
else if (line[x]>=97&&line[x]<=122)
lc++;
}
//Printing the output.
cout << "Uppercase Consonants = " << uc << "." << endl;
cout << "Lowercase Consonants = " << lc << "." << endl;
cout << "Uppercase Vowels = " << uv << "." << endl;
cout << "Lowercase Vowels = " << lv << "." << endl;
number_of_vowels=uv+lv;
cout << "Number of vowels = " << number_of_vowels << endl;
getch();
}
This program takes in a sentence as a screen input from the user.
It then computes the number of uppercase and lowercase consonants, uppercase and lowercase vowels and the total number of vowels.
It then outputs it using the 'cout' command.

Program to convert temperatures from Celsius to Fahrenheit and vice versa.


#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int choice;
float ctemp,ftemp;
cout << "1.Celsius to Fahrenheit" << endl;
cout << "2.Fahrenheit to Celsius" << endl;
cout << "Choose between 1 & 2 : " << endl;
cin>>choice;
if (choice==1)
{
cout << "Enter the temperature in Celsius : " << endl;
cin>>ctemp;
ftemp=(1.8*ctemp)+32;
cout << "Temperature in Fahrenheit = " << ftemp << endl;
}
else
{
cout << "Enter the temperature in Fahrenheit : " << endl;
cin>>ftemp;
ctemp=(ftemp-32)/1.8;
cout << "Temperature in Celsius = " << ctemp << endl;
}
getch();
}
This program takes in the user's choice choice as a screen input from the user.
It then asks the user for a temperature in Celsius or Fahrenheit depending on the choice.
It then converts the Celsius temperature to Fahrenheit or vice versa and prints it out using the 'cout' command.

Program to print the first 10 lines of pascal's triangle.


#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
long triangle(int x,int y);
int main()
{
clrscr();
const lines=10;
for (int i=0;i<lines;i++)
for (int j=1;j<lines-i;j++)
cout << setw(2) << " ";
for (int j=0;j<=i;j++)
cout << setw(4) << triangle(i,j);
cout << endl;
getch();
}
long triangle(int x,int y)
{
if(x<0||y<0||y>x)
return 0;
long c=1;
for (int i=1;i<=y;i++,x--)
c=c*x/i;
return c;
}
This program does not take in any screen inputs from the user.
It just prints out the first ten lines of the pascal's triangle using the 'cout' command.

Program to enter a character and output its ASCII code.


#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
char charac;
cout << "Enter the character : " << endl;
cin>>charac;
int num1=charac;
cout << "The ASCII code for " << charac << " is " << num1 << "." << endl;
getch();
}
This program takes in any character charac as a screen input from the user.
It then finds out its ASCII code and outputs it using the 'cout' command.

Program to enter a letter and output the next 2 letters


#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
char charac;
cout << "Enter your letter : " << endl;
cin>>charac;
cout << "The 2 letters are : " << endl;
cout << (char)(charac-1) << endl;
cout << (char)(charac+1) << endl;
getch();
}
This program takes in a letter charac of the English alphabet as a screen input from the user.
It then determines its previous letter and next letter and prints it out using the 'cout' command.

Program to identify if an input is a symbol, digit or character.


#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
char charac;
cout << "Enter your input : " << endl;
cin>>charac;
if(((charac>='A')&&(charac<='Z'))||((charac>='a')&&(charac<='z')))
cout << "Your input " << charac << " is a character." << endl;
else if((charac>='0')&&(charac<='9'))
cout << "Your input " << charac << " is a digit." << endl;
else
cout << "Your input " << charac << " is a symbol." << endl;
getch();
}
This program takes in a character, a digit or a symbol charac as a screen input from the user.
It then identifies whether the input is a symbol, a digit or a character and outputs the appropriate message using the 'cout' command.

Program to compute the fibonacci series.


#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int a,b,x,y,num1,ct;
a=0;
b=1;
cout << "Enter the number of terms (less than 25) : " << endl;
cin>>num1;
cout << a << endl;
cout << b << endl;
for(ct=1;ct<=num1-2;ct++)
{
x=a+b;
cout << x << endl;
y=a;
a=b;
b=x;
}
getch();
}
This program takes in the number of terms num1 in the fibonacci series (less than 25) as a screen input from the user.
It then computes the fibonacci series and prints it out using the 'cout' command.

Program to find the total days in the year till date.


#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int day,month,total;
int days_per_month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
cout << "Enter the month : " << endl;
cin>>month;
cout << "Enter the day : " << endl;
cin>>day;
total=day;
for(int x=0;x<month-1;x++)
total+=days_per_month[x];
cout << "The number of days in this year till date = " << total << endl;
getch();
}
This program takes in the current day and month as a screen input from the user.
It then calculates the total number of days in the current year till date and outputs it using the 'cout' command.

Program to convert days into years and weeks.


#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int days,years,weeks,num1;
cout << "Enter the number of days : " << endl;
cin>>days;
years=days/365;
num1=days-(years*365);
weeks=days/7;
num1=days-(weeks*7);
cout << days << " days = " << endl;
cout << weeks << " weeks OR " << endl;
cout << years << " years." << endl;
getch();
}
This program takes in the number of days days as a screen input from the user.
It then converts the days into years as well as weeks and outputs it using the 'cout' command.

Program to convert 2-digit octal number into binary number and print it


#include <iostream.h>
#include <conio.h>
void octobin(int);

void main()
{
clrscr();
int a;
cout << "Enter a 2-digit octal number : ";
cin>>a;
octobin(a);
getch();
}
void octobin(int oct)
{
long bnum=0;
int A[6];
//Each octal digit is converted into 3 bits, 2 octal digits = 6 bits.
int a1,a2,quo,rem;
a2=oct/10;
a1=oct-a2*10;
for(int x=0;x<6;x++)
{
A[x]=0;
}
//Storing the remainders of the one's octal digit in the array.
for (x=0;x<3;x++)
{
quo=a1/2;
rem=a1%2;
A[x]=rem;
a1=quo;
}
//Storing the remainders of the ten's octal digit in the array.
for(x=3;x<6;x++)
{
quo=a2/2;
rem=a2%2;
A[x]=rem;
a2=quo;
}
//Obtaining the binary number from the remainders.
for(x=x-1;x>=0;x--)
{
bnum*=10;
bnum+=A[x];
}
cout << "The binary number for the octal number " << oct << " is " << bnum << "." << endl;
}
This program takes in a two-digit octal number a as a screen input from the user.
It then converts the octal number into a binary number and outputs it using the 'cout' command.

Program to find the sum of each row & column of a matrix of size n x m and if matrix is square, find the sum of the diagonals also..


#include <iostream.h>
#include <conio.h>
int main()
{
clrscr();
int A[10][10],m,n,x,y,sum=0;
//Create a Matrix A
cout << "Enter number of rows and columns in Matrix A : \n";
cin>>n>>m;
cout << "Enter elements of Matrix A : \n";
for(x=1;x<n+1;++x)
for(y=1;y<m+1;++y)
cin>>A[x][y];
//Find sum of each row
for(x=1;x<n+1;++x)
{
A[x][m+1]=0;
for(y=1;y<m+1;++y)
A[x][m+1]=A[x][m+1]+A[x][y];
}
//Find sum of each column
for(y=1;y<m+1;++y)
{
A[n+1][y]=0;
for(x=1;x<n+1;++x)
A[n+1][y]+=A[x][y];
}
cout << "\nMatrix A, Row Sum (Last Column)" << " and Column Sum (Last Row) : \n";
for(x=1;x<n+1;++x)
{
for(y=1;y<m+2;++y)
cout << A[x][y] << "     ";
cout << "\n";
}
//Print sum of each column
x=n+1;
for(y=1;y<m+1;++y)
cout << A[x][y] << "     ";
cout << "\n";
if(m==n)
{
for(x=1;x<m+1;x++)
for(y=1;y<n+1;y++)
if(x==y)
sum+=A[x][y];
else
if(y==m-(x+1))
sum+=A[x][y];
}
cout << "Sum of diagonal elements is : " << sum << endl;
getch();
return 0;
}
This program takes in the number of rows (n) and columns (m) as well as the elements as a screen input in a matrix n x m.
It then calculates the sum of each row and each column and outputs it using the 'cout' command.
Also, if it is a square matrix, it calculates the sum of diagonal elements and prints it out

Program to enter an integer and print its total value based on the formula 'x - 1/3!x3 + 1/5!x5 - 1/7!x7 + 1/9!x9'.


#include <iostream.h>
#include <conio.h>
#include <math.h>
int main()
{
clrscr();
float factorial=1;
float num,tot,term,total;
int i,n=20,index,j=1;
cout << "Enter a single-digit integer : \n";
cin>>num;
tot=num;
total=num;
for(i=2,index=3;i<=n;i++,index+=2)
{
for(j=1,factorial=1;j<=index;j++)
factorial*=j;
tot=tot*pow((double)(-1),(double)(2*i-1))*num*num;
term=tot/factorial;
total+=term;
}
cout << "Total = " << total << endl;
getch();
return 0;
}
This program takes in an integer num as a screen input from the user.
It then calculates the total value of the integer based on the formula x - 1/3!x^3 + 1/5!x^5 - 1/7!x^7 + 1/9!x^9.
It then outputs the final answer using the 'cout' command

Program to enter an integer and output it in the reversed form.


#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
long int num1,num2,rnum=0;
cout << "Enter an integer : " << endl;
cin>>num1;
num2=num1;
do
{
rnum=rnum*10;
int digit=num1%10;
rnum+=digit;
num1/=10;
}
while(num1);
cout << "The integer you typed is " << num2 << "." << endl;
cout << "The reversed integer is " << rnum << "." << endl;
getch();
}
This program takes in an integer num1 as a screen input from the user.
It then outputs the integer in its reversed form using the 'cout' command.

Program to find the roots of a quadratic equation.


#include <iostream.h>
#include <conio.h>
#include <math.h>
int main()
{
clrscr();
float a,b,c,d,root1,root2;
cout << "Enter the 3 coefficients a, b, c : " << endl;
cin>>a>>b>>c;
if(!a){
if(!b)
cout << "Both a and b cannot be 0 in ax^2 + bx + c = 0" << "\n";
else
{
d=-c/b;
cout << "The solution of the linear equation is : " << d << endl;
}
}
else
{
d=b*b-4*a*c;
if(d>0)
root1=(-b+sqrt(d))/(2*a);
root2=(-b-sqrt(d))/(2*a);
cout << "The first root = " << root1 << endl;
cout << "The second root = " << root2 << endl;
}
getch();
return 0;
}
This program takes in the values of the three coefficients a, b, and c as a screen input from the user.
It then determines the roots of the quadratic equation using the formula ax^2 + bx + c = 0.
The two roots are then outputted using the 'cout' command.

Program to enter salary and output income tax and net salary.


#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int itrate;
float salary,itax,nsalary=0;
cout << "Enter the salary : ";
cin>>salary;
if(salary>15000)
{
itax=salary*30/100;
itrate=30;
}
else if(salary>=7000)
{
itax=salary*20/100;
itrate=20;
}
else
{
itax=salary*10/100;
itrate=10;
}
nsalary=salary-itax;
cout << "Salary = $" << salary << endl;
cout << "Your income tax @ " << itrate << "% = $" << itax << endl;
cout << "Your net salary = $" << nsalary << endl;
getch();
}
This program takes in the salary of the employee as a screen input from the user.
It then deducts the income tax from the salary on the following basis :
30% income tax if the salary is above $15000.
20% income tax if the salary is between $7000 and $15000.
10% income tax if the salary is below $7000.
The salary, income tax and the net salary is then outputted using the 'cout' command

Program to count the number of words and characters in a sentence.


#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int countch=0;
int countwd=1;
cout << "Enter your sentence in lowercase: " << endl;
char ch='a';
while(ch!='\r')
{
ch=getche();
if(ch==' ')
countwd++;
else
countch++;
}
cout << "\n Words = " << countwd << endl;
cout << "Characters = " << countch-1 << endl;
getch();
}
This program takes in a sentence as a screen input from the user.
It then determines the number of words and characters in the sentence using the 'WHILE' loop and outputs them using the 'cout' command.

Program to enter the unit reading and output the customer's telephone bill.


#include <iostream.h>
#include <conio.h>
int main()
{
clrscr();
long int units,charge=0;
float total;
const int rent=25;
cout << "Enter the number of units used : ";
cin>>units;
if(units>200)
charge=(units-200)*20+150*40+50*60;
else if(units>50)
charge=(units-50)*40+50*60;
else
charge=units*60;
total=0.01*charge+rent;
cout << "You have used " << units << " units." << endl;
cout << "Your total telephone bill is $" << total;
getch();
return 0;
}
This program takes in the number of units used ('units') as a screen input from the user.
It then calculates the total telephone bill for the customer on the following basis :
A compulsory fee of $25, plus
60 cents per unit for the first 50 units,
40 cents per unit for the next 150 units,
20 cents per unit for anything above 200 units.
It then outputs the bill using the 'cout' command.

Program to enter an integer and output its 15 multiples


#include <iostream.h>
#include <conio.h>
int main()
{
clrscr();
int x;
cout << "Enter an integer less than 2185 : ";
cin>>x;
cout << "The first 15 multiples of " << x << " are : ";
for(int y=1;y<16;y++)
cout << "\n" << x << "x" << y << "=" << x*y;
getch();
return 0;
}
This program takes in an integer x as a screen input from the user.
It then calculates the first fifteen multiples of that integer and outputs it using the 'cout' command.

Program to enter three integers and output the biggest integer using IF.


#include <iostream.h>
#include <conio.h>
int main()
{
clrscr();
int x,y,z,biggest;
cout << "Enter 3 integers : ";
cin>>x>>y>>z;
biggest=x;
if(y>biggest)
biggest=y;
if(z>biggest)
biggest=z;
cout << "The biggest integer out of the 3 integers you typed ";
cout << x << ", " << y << " & " << z << " is : " << "\n" << biggest << "\n";
getch();
return 0;
}
This program takes in three integers x, y and z as a screen input from the user.
It then determines the biggest integer of the three using the 'IF' statement.
It then outputs the biggest integer using the 'cout' command.

Program to enter three integers and output the biggest integer.


#include <iostream.h>
#include <conio.h>
int main()
{
clrscr();
int x,y,z,biggest;
cout << "Enter 3 integers : ";
cin>>x>>y>>z;
biggest=x>y?(x>z?x:z):(y>z?y:z);
cout << "The biggest integer out of the 3 integers you typed ";
cout << x << ", " << y << " & " << z << " is : " << "\n" << biggest << "\n";
getch();
return 0;
}
This program takes in three integers x, y and z as a screen input from the user.
It then determines the biggest integer of the three and outputs it using the 'cout' command.

Program to enter an integer and find out if it is even or odd.


#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int x;
cout << "Enter an integer : ";
cin>>x;
if(x%2==0)
cout << "The number " << x << " is even.";
else
cout << "The number " << x << " is odd.";
getch();
}
This program takes in an integer x as a screen input from the user.
It then determines whether the integer is odd or even and outputs the appropriate message using the 'cout' command.

Program to enter two integers and print the quotient and remainder.


#include <iostream.h>
#include <conio.h>
int main()
{
clrscr();
int x,y,quotient,remainder;
cout << "Enter 2 integers greater than 0 : ";
cin>>x>>y;
quotient=x/y;
remainder=x-(quotient*y);
cout << "Quotient of " << x << " & " << y << " = " << quotient << "\n";
cout << "Remainder" << " = " << remainder << "\n";
getch();
return 0;
}
This program takes in two integers x and y as a screen input from the user.
It then calculates their quotient and remainder and outputs them using the 'cout' command.

Program to plot pixels.


#include <conio.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>

void main (int)
{
int gdriver=DETECT,gmode,errorcode; //Requesting auto-detection.
int midx,midy,x;
//Initializing graphics and local variables.
initgraph(&gdriver,&gmode,"d:\\bc3\\bgi");
//Reading result of initialization.
errorcode=graphresult();
if(errorcode!=grOk)
//An error occurred.
{
printf("Graphics error occurred : %s \n",grapherrormsg(errorcode));
printf("Press any key to stop : ");
getch();
exit(1); //Terminate the program due to error.
}
for(x=40;x<120;x=x+10)
{
putpixel(x,200,14); //Plots 1 pixel at position(x,200) with YELLOW color(14)
getch();
}
}
This graphics program plots eight yellow-colored pixels on the screen using the 'putpixel' command.

Program to construct a 3-dimensional bar.


#include <conio.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>

void main (int)
{
int gdriver=DETECT,gmode,errorcode; //Requesting auto-detection.
int midx,midy,x;
//Initializing graphics and local variables.
initgraph(&gdriver,&gmode,"d:\\bc3\\bgi");
//Reading result of initialization.
errorcode=graphresult();
if(errorcode!=grOk)
//An error occurred.
{
printf("Graphics error occurred : %s \n",grapherrormsg(errorcode));
printf("Press any key to stop : ");
getch();
exit(1); //Terminate the program due to error.
}

setfillstyle(EMPTY_FILL,0);
/*The above statement means that the setfillstyle function is used to
set the fill style of the 3-d bar as a blank.*/
bar3d(200,200,300,450,10,1);
getch();
closegraph();
}
This graphics program outputs a rectangular slab (cuboid) on the screen using the 'bar3d' command.

Program to write in different fonts on the screen.


#include <conio.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>

void main (int)
{
int gdriver=DETECT,gmode,errorcode; //Requesting auto-detection.
int midx,midy,fstyle;
//Initializing graphics and local variables.
initgraph(&gdriver,&gmode,"d:\\bc3\\bgi");
//Reading result of initialization.
errorcode=graphresult();
if(errorcode!=grOk)
//An error occurred.
{
printf("Graphics error occurred : %s \n",grapherrormsg(errorcode));
printf("Press any key to stop : ");
getch();
exit(1); //Terminate the program due to error.
}
//Changing the font styles using a loop.
cleardevice();
settextstyle(DEFAULT_FONT,HORIZ_DIR,4);
/*The above statement means that it is the default font in the horizontal
direction and the font size is 4.*/
//Outputting a message.
outtextxy(200,200,"Default font");
getch();
cleardevice();
settextstyle(TRIPLEX_FONT,VERT_DIR,5);
/*The above statement means that it is the triplex font in the vertical
direction and the font size is 5.*/
//Outputting a message.
outtextxy(200,200,"Triplex font");
getch();
cleardevice();
settextstyle(GOTHIC_FONT,HORIZ_DIR,5);
/*The above statement means that it is the default font in the horizontal
direction and the font size is 2.*/
//Outputting a message.
outtextxy(200,200,"Gothic font");
getch();
cleardevice();
settextstyle(SMALL_FONT,VERT_DIR,5);
/*The above statement means that it is the small font in the vertical
direction and the font size is 5.*/
//Outputting a message.
outtextxy(200,200,"Small font");
getch();
cleardevice();
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,5);
/*The above statement means that it is the sans serif font in the horizontal
direction and the font size is 5.*/
//Outputting a message.
outtextxy(200,200,"Sans Serif font");
getch();
closegraph();
}
This graphics program switches between default font, triplex font, gothic font, small font and sans serif font using the 'settextstyle' command.

Program to change the foreground colors and draw circles on the screen.


#include <conio.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>

void main (int)
{
int gdriver=DETECT,gmode,errorcode; //Requesting auto-detection.
int midx,midy,x;
//Initializing graphics and local variables.
initgraph(&gdriver,&gmode,"d:\\bc3\\bgi");
//Reading result of initialization.
errorcode=graphresult();
if(errorcode!=grOk)
//An error occurred.
{
printf("Graphics error occurred : %s \n",grapherrormsg(errorcode));
printf("Press any key to stop : ");
getch();
exit(1); //Terminate the program due to error.
}
/*Changing the foreground color.
Note : Press enter to exit the last screen as it is black and
it may appear as if the program has stopped running.*/
for(x=15;x>=0;x--)
{
setcolor(x);
circle(20+(x*40),200,15);/*Changing x-coordinate by 50 each time so that
the circles do not overlap.*/
getch();
}
cleardevice(); //Clearing the screen in the graphics mode.
circle(200,200,50);
getch();
closegraph();
}
This graphics program changes the foreground colors on the screen gradually from white to black, in-turn drawing circles of that foreground color, using the 'setcolor' command.

Program to change the background colors on the screen.


#include <conio.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>

void main (int)
{
int gdriver=DETECT,gmode,errorcode; //Requesting auto-detection.
int midx,midy,x;
//Initializing graphics and local variables.
initgraph(&gdriver,&gmode,"d:\\bc3\\bgi");
//Reading result of initialization.
errorcode=graphresult();
if(errorcode!=grOk)
//An error occurred.
{
printf("Graphics error occurred : %s \n",grapherrormsg(errorcode));
printf("Press any key to stop : ");
getch();
exit(1); //Terminate the program due to error.
}
/*Changing the background color.
Note : Press enter to see the first screen as it is black and
it may appear as if the program has stopped running.*/
for(x=0;x<=15;x++)
{
setbkcolor(x);
getch();
}
closegraph();
}
This graphics program changes the background colors on the screen gradually from black to white using the 'setbkcolor' command.

Program to draw circles.


#include <iostream.h>
#include <conio.h>
#include <graphics.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>

void main()
{
clrscr();
int gd=DETECT,gm,errorcode; //Requesting auto-detection.
//Initializing graphics and local variables.
initgraph(&gd,&gm,"d:\\bc3\\bgi"); //Path where graphics drivers are installed
//Reading result of initialization.
errorcode=graphresult();
//An error occured.
if (errorcode!=grOk)
{
cout << "Graphics error occured : \n" << grapherrormsg(errorcode) << endl;
cout << "Press any key to stop : ";
getch();
exit(1);
}
circle(200,200,50); //Drawing a circle having center(200,200) and radius(50).
getch();
circle(300,203,40); //Drawing a circle having center(300,203) and radius(40).
getch();
circle(500,303,80); //Drawing a circle having center(500,303) and radius(80).
getch();
closegraph();
}
This graphics program draws three circles on the screen.

Program to draw 2 rectangles and fill 1 of them.


#include <iostream.h>
#include <conio.h>
#include <graphics.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>

void main()
{
  clrscr();
  int gd = DETECT,gm,errorcode; //Requesting auto-detection.

  //Initializing graphics and local variables.
  initgraph (&gd, &gm, "d:\\bc3\\bgi"); //Path where graphics drivers are installed

  //Read result of initialization.
  errorcode = graphresult();

  //An error occured.
  if (errorcode != grOk)
    {
      cout << "Graphics error occured : \n" << grapherrormsg(errorcode) << endl;
      cout << "Press any key to stop : ";
      getch();
      exit(1);
    }

  /*Drawing a rectangle having top LHS vertex at (300, 300)
  and bottom RHS vertex at (600, 400)*/
  rectangle(300, 300, 600, 400);
  rectangle(100, 100, 200, 200);
  getch();
  floodfill(120, 120, WHITE);
  getch();
  closegraph();
}
This graphics program draws two rectangles, but fills in only one of them with a white color using the 'floodfill' command

Program to enter an integer and print out its successor.


#include <iostream.h>
#include <conio.h>
void value(int);

void main()
{
clrscr();
int x;
cout << "Enter an integer : ";
cin>>x;
cout << "The successor of " << x << " is ";
value(x);
getch();
}
void value(int x)
{
x++;
cout << x << "." << endl;
}
This program takes in an integer x as a screen input from the user.
It then determines the successor of the integer and outputs it using the 'cout' command.

Program to enter an integer and output the cube of that integer.


#include <iostream.h>
#include <conio.h>
int cube(int x); //The prototype.

void main()
{
clrscr();
int a;
cout << "Enter an integer : ";
cin>>a;
cout << "The cube of " << a << " is : " << cube(a) << endl; //Call the function cube(a).
getch();
}
//Defining the function.
int cube(int x)
{
int y;
y=x*x*x;
return(y);
}
This program takes in an integer a as a screen input from the user.
It then determines the integer's cube and outputs it using the 'cout' command.

Program to enter a string and find its length.


#include <iostream.h>
#include <conio.h>
#include <string.h>

void main()
{
clrscr();
int slength;
char x[81]; //Allowing the user to input a maximum of 80 characters.
cout << "Enter the string : " << endl;
cin>>x;
slength=strlen(x);
cout << "The length of the string " << x << " is " << slength << "." << endl;
getch();
}
This program takes in a string x as a screen input from the user.
It then determines the length of the string using the 'strlen' function.
This length is finally outputted using the 'cout' command.

Program to enter an integer and print if it is prime or composite.


#include <iostream.h>
#include <conio.h>
#include <process.h>

void main()
{
clrscr();
int num1,x;
cout << "Enter an integer : " << endl;
cin>>num1;
for(x=2;x<num1;x++)
{
if(num1%x==0)
{
cout << num1 << " is a composite number." << endl;
getch();
exit(0);
}
else
{
cout << num1 << " is a prime number." << endl;
getch();
exit(0);
}
}
}
This program takes in an integer num1 as a screen input from the user.
It then determines whether the integer is prime or composite.
It finally outputs the approriate message by writing to the 'cout' stream.

Program to enter the principal, rate & time and print the simple interest.


#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int x;
float sinterest,principal,rate,time;
for(x=4;x>=0;x--)
{
cout << "Enter the principal, rate & time : " << endl;
cin>>principal>>rate>>time;
sinterest=(principal*rate*time)/100;
cout << "Principal = $" << principal << endl;
cout << "Rate = " << rate << "%" << endl;
cout << "Time = " << time << " years" << endl;
cout << "Simple Interest = $" << sinterest << endl;
}
getch();
}
This program takes in the prinicipal, rate and time as a screen input from the user.
The program is executed (run) 5 times using the 'FOR' loop.
It calculates the simple interest using the formula I = PTR/100.
The principal, rate, time and the simple interest are then outputted using the 'cout' command.

Program to switch between different cases.


#include <iostream.h>
#include <conio.h>
int main()
{
clrscr();
int choice;
cout << "1. Talk" << endl;
cout << "2. Eat" << endl;
cout << "3. Play" << endl;
cout << "4. Sleep" << endl;
cout << "Enter your choice : " << endl;
cin>>choice;
switch(choice)
{
case 1 : cout << "You chose to talk...talking too much is a bad habit." << endl;
break;
case 2 : cout << "You chose to eat...eating healthy foodstuff is good." << endl;
break;
case 3 : cout << "You chose to play...playing too much everyday is bad." << endl;
break;
case 4 : cout << "You chose to sleep...sleeping enough is a good habit." << endl;
break;
default : cout << "You did not choose anything...so exit this program." << endl;
}
getch();
}
This program takes in the user's choice as a screen input.
Depending on the user's choice, it switches between the different cases.
The appropriate message is then outputted using the 'cout' command.

Program to enter the sale value and print the agent's commission.


#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
long int svalue;
float commission;
cout << "Enter the total sale value : " << endl;
cin>>svalue;
if(svalue<=10000)
{
commission=svalue*5/100;
cout << "For a total sale value of $" << svalue << ", ";
cout << "the agent's commission is $" << commission;
}
else if(svalue<=25000)
{
commission=svalue*10/100;
cout << "For a total sale value of $" << svalue << ", ";
cout << "the agent's commission is $" << commission;
}
else if(svalue>25000)
{
commission=svalue*20/100;
cout << "For a total sale value of $" << svalue << ", ";
cout << "the agent's commission is $" << commission;
}
getch();
}
This program takes in the total sale value as a screen input from the user.
The program then calculates the agent's commission with the help of the 'IF-ELSE' command as follows :
5% if the total sale value is less than or equal to $10000.
10% if the total sale value is less than or equal to $25000.
20% if the total sale value is greater than $25000. It then outputs the agent's commission using the 'cout' command.
Click here for SAMPLE INPUT

Program to enter an integer and print if it is greater or less than 100


#include <iostream.h>
#include <conio.h>

void main(){
clrscr();
int x;
cout << "Enter an integer : " << endl;
cin>>x;
if(x>100)
{
cout << x << " is greater than 100." << endl;
}
else
{
cout << x << " is less than 100." << endl;
}
getch();
}
This program takes in an integer x as a screen input from the user.
The program tells the user whether that integer is greater than 100 or less than 100.
It then prints out the appropriate message using the 'cout' command.

Program to enter your age and print if you should be in grade 10.


#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int age;
cout << "Enter your present age : " << endl;
cin>>age;
if(age==16)
{
cout << "Your present age is " << age << " years." << endl;
cout << "You are of the right age for joining grade 10 !" << endl;
}
else
{
cout << "Your present age is " << age << " years." << endl;
cout << "You are not of the right age for joining grade 10 !" << endl;
}
getch();
}
This program takes in the age as a screen input from the user.
The program tells the user whether he/she should be in grade 10 or not by using the 'IF-ELSE' command.
It then prints out the appropriate message using the 'cout' command.

C++ Program to find the sum, difference, product and quotient of two integers


#include <iostream.h>
#include <conio.h>

void main()
{
  clrscr();
  int x = 10;
  int y = 2;
  int sum, difference, product, quotient;
  sum = x + y;
  difference = x - y;
  product = x * y;
  quotient = x / y;
  cout << "The sum of " << x << " & " << y << " is " << sum << "." << endl;
  cout << "The difference of " << x << " & " << "y <<  is " << difference << "." << endl;
  cout << "The product of " << x << " & " << y << " is " << product << "." << endl;
  cout << "The quotient of " << x << " & " << y << " is " << quotient << "." << endl;
  getch();
}
This program has pre-defined values for two integers x and y.
The sum, difference, product and quotient of these two values are calculated and then outputted using the 'cout' comma

C++ Program to output an integer, a floating point number and a character


#include <iostream.h>
#include <conio.h>

void main()
{
  clrscr();
  int x = 10;
  float y = 10.1;
  char z = 'a';
  cout << "x = " << x << endl;
  cout << "y = " << y << endl;
  cout << "z = " << z << endl;
  getch();
}
This program has pre-defined values for an integer x, floating point number y, and a character z.
These three values are outputted using the 'cout' command.

C++ Programming : Program 4-A Program to enter velocity, acceleration and time and print final velocity using the formula : v = u + a * t

Click here for C++ PROGRAM
#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int v,u,a,t;
cout << "Enter the velocity, acceleration, time as integers : " << endl;
cin>>u>>a>>t;
v=u+a*t;
cout << "The final velocity is " << v << "." << endl;
getch();
}

C++ Programming -3 A

Click here for C++ PROGRAM
#include <iostream.h>
#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int x,y,sum;
float average;
cout << "Enter 2 integers : " << endl;
cin>>x>>y;
sum=x+y;
average=sum/2;
cout << "The sum of " << x << " and " << y << " is " << sum << "." << endl;
cout << "The average of " << x <<  " and " << y << " is " << average << "." << endl;
getch();
}
This program takes in two integers x and y as a screen input from the user.
The sum and average of these two integers are calculated and outputted using the 'cout' command.

Sources c++


  Generic Console programs
FileDescriptionSource
 C++ Tutorial SourcesSet of simple example sources to follow the C++ Tutorial included in the documents section of this site.Juan Soulie
 FractionExample program to add and reduce fractions.Abraham Ishaq
 FractionA Fraction class that has the ability to add, subtract, multiply, divide and show various statistics of the fraction.Daniel Pronych
 TriangleSmall program for beginners demonstrating the use of loops and iostream + iomanip libraries.Gary Paduana
 Master StringThis is a collection of functions, and classes that will aid you in very explicit string manipulation.Jared Bruni
 EncryptEncrypts/Decrypts a piece of text using vigenere algorithm.Sam Alexander
 RectangleA simple program demonstrating tokenizing by drawing a rectangle.Nick White
 Binary convertA simple program that converts a string into its binary representation.Matt Fowler

  Windows programs
FileDescriptionSource
 WinnieA small program that shows one of the fundamentals of Windows programming: How to create a window.Tom Lee
 SDI FrameA basic framework for Single-Document-Interface applications.Gary Hall
 BMP LoaderExample on how to load a BMP File.Juan Soulie
 GIF ViewExample on how to load and display animated GIF and BMP Files.Juan Soulie
 CWinTcpSocketC++ Winsock wrapper class.Tom Lee
 Master LibraryHeader file containing a lot of C++ functions. Over 6,000 lines of code with code ranging from DirectX to Winsock. Good resource for windows C++ programming (Visual C++ project files).Jared Bruni
 Win32 ExampleBasic intro Windows API, with multiple windows (Visual C++ project files, using C).Jared Bruni
 Win32 Example (II)Basic intro Windows API, controls, menu (Visual C++ project files).Jared Bruni
 URL DownloaderThis is a simple application that allows you to download a file from a web page (Visual C++ project files).Jared Bruni
 Basic Direct 3D 8Basic intro Direct3D8, sets up the window and draws a few things (Visual C++/DirectX project files).Jared Bruni
 Tank 3.0 BetaBeta version of a direct3d game that the author is programming at the moment.Christian Reimann
 HWPrintExample program to demonstrate printing using Win32 API.Tom Lee

  MS-DOS programs
FileDescriptionSource
 NotepadText editor.Zahid Ashfaq

program on cc +

Structure of a program

Published by Juan Soulie
Last update on Sep 29, 2009 at 3:08pm UTC
Probably the best way to start learning a programming language is by writing a program. Therefore, here is our first program:

1
2
3
4
5
6
7
8
9
10
// my first program in C++

#include <iostream>
using namespace std;

int main ()
{
  cout << "Hello World!";
  return 0;
}
Hello World!


The first panel (in light blue) shows the source code for our first program. The second one (in light gray) shows the result of the program once compiled and executed. To the left, the grey numbers represent the line numbers - these are not part of the program, and are shown here merely for informational purposes.

The way to edit and compile a program depends on the compiler you are using. Depending on whether it has a Development Interface or not and on its version. Consult the compilers section and the manual or help included with your compiler if you have doubts on how to compile a C++ console program.

The previous program is the typical program that programmer apprentices write for the first time, and its result is the printing on screen of the "Hello World!" sentence. It is one of the simplest programs that can be written in C++, but it already contains the fundamental components that every C++ program has. We are going to look line by line at the code we have just written:

// my first program in C++
This is a comment line. All lines beginning with two slash signs (//) are considered comments and do not have any effect on the behavior of the program. The programmer can use them to include short explanations or observations within the source code itself. In this case, the line is a brief description of what our program is. 
#include <iostream>
Lines beginning with a hash sign (#) are directives for the preprocessor. They are not regular code lines with expressions but indications for the compiler's preprocessor. In this case the directive #include <iostream> tells the preprocessor to include the iostream standard file. This specific file (iostream) includes the declarations of the basic standard input-output library in C++, and it is included because its functionality is going to be used later in the program. 
using namespace std;
All the elements of the standard C++ library are declared within what is called a namespace, the namespace with the namestd. So in order to access its functionality we declare with this expression that we will be using these entities. This line is very frequent in C++ programs that use the standard library, and in fact it will be included in most of the source codes included in these tutorials.
int main ()
This line corresponds to the beginning of the definition of the main function. The main function is the point by where all C++ programs start their execution, independently of its location within the source code. It does not matter whether there are other functions with other names defined before or after it - the instructions contained within this function's definition will always be the first ones to be executed in any C++ program. For that same reason, it is essential that all C++ programs have a main function. The word main is followed in the code by a pair of parentheses (()). That is because it is a function declaration: In C++, what differentiates a function declaration from other types of expressions are these parentheses that follow its name. Optionally, these parentheses may enclose a list of parameters within them. Right after these parentheses we can find the body of the main function enclosed in braces ({}). What is contained within these braces is what the function does when it is executed.
cout << "Hello World!";
This line is a C++ statement. A statement is a simple or compound expression that can actually produce some effect. In fact, this statement performs the only action that generates a visible effect in our first program. cout is the name of the standard output stream in C++, and the meaning of the entire statement is to insert a sequence of characters (in this case the Hello World sequence of characters) into the standard output stream (cout, which usually corresponds to the screen). cout is declared in the iostream standard file within the std namespace, so that's why we needed to include that specific file and to declare that we were going to use this specific namespace earlier in our code. Notice that the statement ends with a semicolon character (;). This character is used to mark the end of the statement and in fact it must be included at the end of all expression statements in all C++ programs (one of the most common syntax errors is indeed to forget to include some semicolon after a statement).
return 0;
The return statement causes the main function to finish. return may be followed by a return code (in our example is followed by the return code with a value of zero). A return code of 0 for the main function is generally interpreted as the program worked as expected without any errors during its execution. This is the most usual way to end a C++ console program.


You may have noticed that not all the lines of this program perform actions when the code is executed. There were lines containing only comments (those beginning by //). There were lines with directives for the compiler's preprocessor (those beginning by #). Then there were lines that began the declaration of a function (in this case, the main function) and, finally lines with statements (like the insertion into cout), which were all included within the block delimited by the braces ({}) of the main function.

The program has been structured in different lines in order to be more readable, but in C++, we do not have strict rules on how to separate instructions in different lines. For example, instead of

1
2
3
4
5
int main ()
{
  cout << " Hello World!";
  return 0;
}


We could have written:

 
int main () { cout << "Hello World!"; return 0; }


All in just one line and this would have had exactly the same meaning as the previous code.

In C++, the separation between statements is specified with an ending semicolon (;) at the end of each one, so the separation in different code lines does not matter at all for this purpose. We can write many statements per line or write a single statement that takes many code lines. The division of code in different lines serves only to make it more legible and schematic for the humans that may read it.

Let us add an additional instruction to our first program:

1
2
3
4
5
6
7
8
9
10
11
12
// my second program in C++

#include <iostream>

using namespace std;

int main ()
{
  cout << "Hello World! ";
  cout << "I'm a C++ program";
  return 0;
}
Hello World! I'm a C++ program


In this case, we performed two insertions into cout in two different statements. Once again, the separation in different lines of code has been done just to give greater readability to the program, since main could have been perfectly valid defined this way:

 
int main () { cout << " Hello World! "; cout << " I'm a C++ program "; return 0; } 


We were also free to divide the code into more lines if we considered it more convenient:

1
2
3
4
5
6
7
8
int main ()
{
  cout <<
    "Hello World!";
  cout
    << "I'm a C++ program";
  return 0;
}


And the result would again have been exactly the same as in the previous examples.

Preprocessor directives (those that begin by #) are out of this general rule since they are not statements. They are lines read and processed by the preprocessor and do not produce any code by themselves. Preprocessor directives must be specified in their own line and do not have to end with a semicolon (;).

Comments


Comments are parts of the source code disregarded by the compiler. They simply do nothing. Their purpose is only to allow the programmer to insert notes or descriptions embedded within the source code.

C++ supports two ways to insert comments:

1
2
// line comment
/* block comment */ 


The first of them, known as line comment, discards everything from where the pair of slash signs (//) is found up to the end of that same line. The second one, known as block comment, discards everything between the /* characters and the first appearance of the */ characters, with the possibility of including more than one line.
We are going to add comments to our second program:

1
2
3
4
5
6
7
8
9
10
11
12
/* my second program in C++
   with more comments */

#include <iostream>
using namespace std;

int main ()
{
  cout << "Hello World! ";     // prints Hello World!
  cout << "I'm a C++ program"; // prints I'm a C++ program
  return 0;
}
Hello World! I'm a C++ program


If you include comments within the source code of your programs without using the comment characters combinations ///* or */, the compiler will take them as if they were C++ expressions, most likely causing one or several error messages when you compile it.