C++ Programming :
I. Program 1-A
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.
SAMPLE INPUT
No inputs from the user for this program.
SAMPLE OUTPUT
x = 10
y = 10.1
z = a
II. Program 2-A
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' command.
SAMPLE INPUT
No inputs from the user for this program.
SAMPLE OUTPUT
The sum of 10 & 2 is 12.
The difference of 10 & 2 is 8.
The product of 10 & 2 is 20.
The quotient of 10 & 2 is 5.
III. Program 3-A
C++ Program to enter two integers and find their sum and average
#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.
SAMPLE INPUT
8 6
SAMPLE OUTPUT
The sum of 8 and 6 is 14.
The average of 8 and 6 is 7.
IV. Program 4-A
Program to enter velocity, acceleration and time and print final velocity using the formula : v = u + a * t
#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();
}
This program takes in the velocity, acceleration and the time as a screen input from the user.
The final velocity is calculated using the formula v = u + a * t, and then outputted using the 'cout' command.
SAMPLE INPUT
20 10 5
SAMPLE OUTPUT
The final velocity is 70.
V. Program 5-A
C++ 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.
SAMPLE INPUT
15
SAMPLE OUTPUT
Your present age is 15 years.
You are not of the right age for joining grade 10 !
VI. Program 6-A
C++ 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.
SAMPLE INPUT
74
SAMPLE OUTPUT
74 is less than 100.
VII. Program 7-A
C++ 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.
SAMPLE INPUT
26000
SAMPLE OUTPUT
For a total sale value of $26000, the agent's commission is $5200
VIII. Program 8-A
C++ 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.
SAMPLE INPUT
3
SAMPLE OUTPUT
You chose to play...playing too much everyday is bad.
IX. Program 9-A
C++ 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.
SAMPLE INPUT
1000 5 3
SAMPLE OUTPUT
Principal = $1000
Rate = 5%
Time = 3 years
Simple Interest = $150
X. Program 10-A
C++ 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 appropriate message by writing to the 'cout' stream.
SAMPLE INPUT
23
SAMPLE OUTPUT
23 is a prime number.
XI. Program 12-A
C++ 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.
SAMPLE INPUT
8
SAMPLE OUTPUT
The cube of 8 is : 512
XII. Program 13-A
C++ 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.
SAMPLE INPUT
49
SAMPLE OUTPUT
The successor of 49 is 50.
XIII. Program 14-A
C++ 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.
XIV. Program 16-A
C++ 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.
XV. Program 17-A
C++ 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.
XVI. Program 18-A
C++ 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.
XVII. Program 21-A
C++ 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.
SAMPLE INPUT
23 4
SAMPLE OUTPUT
Quotient of 23 & 4 = 5
Remainder = 3
XVIII. Program 27-A
C++ 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.
SAMPLE INPUT
this is a nice program
SAMPLE OUTPUT
Words = 5
Characters = 18
XIX. Program 28-A
C++ 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.
SAMPLE INPUT
12000
SAMPLE OUTPUT
Salary = $12000
Your income tax @ 20% = $2400
Your net salary = $9600
--
by sugu :-)
No comments:
Post a Comment