0% found this document useful (0 votes)
6 views

c++ Program

Uploaded by

deju
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

c++ Program

Uploaded by

deju
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

1

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

#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.
2

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

#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.
3

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

#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 << ", ";
4

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.

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

You might also like