Welcome

C++ PROGRAMS

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

Program:-
#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.

Input:-
No inputs from the user for this program.

Output:-
x = 10
y = 10.1
z = a


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

Program:-
#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.

Input:-
No inputs from the user for this program.

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.


Program to enter two integers and find their sum and average

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.

Input:-
8 6

Output:-
The sum of 8 and 6 is 14.
The average of 8 and 6 is 7.


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

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();
}
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.

Input:-
20 10 5

Output:-
The final velocity is 70.


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

Program:-
#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.

Input:-
15

Output:-
Your present age is 15 years.
You are not of the right age for joining grade 10 !


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

Program:-
#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.

Input:-
74

Output:-
74 is less than 100.


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

Program:-
#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.

Input:-
26000

Output:-
For a total sale value of $26000, the agent's commission is $5200


Program to switch between different cases

Program:-
#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.

Input:-
3

Output:-
You chose to play...playing too much everyday is bad.


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

Program:-
#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.

Input:-
1000 5 3

Output:-
Principal = $1000
Rate = 5%
Time = 3 years
Simple Interest = $150


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

Program:-
#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.

Input:-
23

Output:-
23 is a prime number.


Program to enter a string and find its length

Program:-
#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.

Input:-
goldfish

Output:-
The length of the string goldfish is 8.


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

Program:-
#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.

Input:-
8

Output:-
The cube of 8 is:  512


Program to enter an integer and print out its successor

Program:-
#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.

Input:-
49

Output:-
The successor of 49 is 50.


Program to draw 2 rectangles and fill 1 of them

Program:-
#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.

Input:-

Output:-


Program to draw circles

Program:-
#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.

Input:-

Output:-


Program to change the background colors on the screen

Program:-
#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.

Input:-

Output:-


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

Program:-
#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.

Input:-

Output:-


Program to write in different fonts on the screen

Program:-
#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.

Input:-

Output:-


Program to construct a 3-dimensional bar

Program:-
#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.

Input:-

Output:-


Program to plot pixels

Program:-
#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.

Input:-

Output:-


Program to enter two integers and print the quotient and remainder

Program:-
#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.

Input:-
23 4

Output:-
Quotient of 23 & 4 = 5
Remainder = 3


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

Program:-
#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.

Input:-
86

Output:-
The number 86 is even.


Program to enter three integers and output the biggest integer

Program:-
#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.

Input:-
63 73 79

Output:-
The biggest integer out of the 3 integers you typed 64, 73 & 79 is :
79


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

Program:-
#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.

Input:-
32 47 44

Output:-
The biggest integer out of the 3 integers you typed 32, 47 & 44 is :
47


Program to enter an integer and output its 15 multiples

Program:-
#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.

Input:-
12

Output:-
The first 15 multiples of 12 are :
12x1=12
12x2=24
12x3=36
12x4=48
12x5=60
12x6=72
12x7=84
12x8=96
12x9=108
12x10=120
12x11=132
12x12=144
12x13=156
12x14=168
12x15=180


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

Program:-
#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.

Input:-
250

Output:-
You have used 250 units.
Your total bill is $125


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

Program:-
#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.

Input:-
this is a nice program

Output:-
Words = 5
Characters = 18


Program to enter salary and output income tax and net salary

Program:-
#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.

Input:-
12000

Output:-
Salary = $12000
Your income tax @ 20% = $2400
Your net salary = $9600


Program to find the roots of a quadratic equation

Program:-
#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.

Input:-
4 4 -3

Output:-
The first root = 0.5
The second root = -1.5


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

Program:-
#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.


Input:-
987

Output:-
The integer you typed is 987.
The reversed integer is 789.


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

Program:-
#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.

Input:-
3

Output:-
Total = 0.14112


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.

Program:-
#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.

Input:-
3 3
9 8 7 6 5 4 3 2 1

Output:-
Matrix A, Row Sum(Last Column) and Column Sum(Last Row) :
9     8     7     24
6     5     4     15
3     2     1     6
18     15     12
Sum of diagonal elements is : 15


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

Program:-
#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.

Input:-
13

Output:-
The binary number for the octal number 13 is 1011.


Program to convert days into years and weeks

Program:-
#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.

Input:-
789

Output:-
789 days =
112 weeks OR
2 years.


Program to find the total days in the year till date

Program:-
#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.

Input:-
6
12

Output:-
The number of days in this year till date = 163


Program to compute the Fibonacci series

Program:-
#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.

Input:-
12

Output:-
0
1
1
2
3
5
8
13
21
34
55
89


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

Program:-
#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.

Input:-
#

Output:-
Your input # is a symbol.


Program to enter a letter and output the next 2 letters

Program:-
#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.

Input:-
x

Output:-
The next 2 letters after x are :
y
z


Program to enter a character and output its ASCII code

Program:-
#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.

Input:-
a

Output:-
The ASCII code for a is 97.


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

Program:-
#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.

Input:-
No input from the user for this program.

Output:-
1  10  45  120  210  252  210  120  45  10  1


Program to convert temperatures from Celsius to Fahrenheit and vice versa

Program:-
#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.

Input:-
2
98.6

Output:-
Temperature in Celcius = 37


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

Program:-
#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.

Input:-
These programs are truly AMAZING

Output:-
Uppercase Consonants = 5.
Lowercase Consonants = 13.
Uppercase Vowels = 3.
Lowercase Vowels = 7.
Number of vowels = 10


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

Program:-
#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.

Input:-
43
67
53
21
6
78
92
48
95
8

Output:-
Array in ascending order is :
6
8
21
43
48
53
67
78
92
95


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

Program:-
#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.

Input:-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

Output:-
Sum of either of the diagonal elements is : 34


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


Program:-
#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.

Input:-
82 78 86


Output:-
The smallest integer out of the 3 integers you typed 82, 78 & 86 is :
78

No comments:

Post a Comment

Leave Comments