Saturday, November 29, 2014

C++ Roots, salary details, character pro



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

#include 
#include 
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();
}
 
Output
 
The integer you typed is 987.
The reversed integer is 789.

Program to find the roots of a quadratic equation

#include 
#include 
#include 
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;
}
 
Output
 
The first root = 0.5
The second root = -1.5

Program to enter salary and output income tax and net salary

#include 
#include 
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();
}
 
Output
 
Salary = $12000
Your income tax @ 20% = $2400
Your net salary = $9600

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

#include 
#include 
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();
}
 
Output
 
Words = 5
Characters = 18

No comments:

Post a Comment