Project 2

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

OUTPUT:

 

2

Enter the temperature in Fahrenheit:98.6

Temperature in Celcius = 37

 

Program to enter a sentence and output the number ofuppercase & 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++;

}

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

}

OUTPUT:

These programs are truly AMAZING

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

#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();

}

OUTPUT:

 

43 , 67 , 53 , 21 , 6 , 78 , 92 , 48 , 95 , 8

Array in ascending order is :

6<8<21<43<48<53<78<92<95

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;

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];

}

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

}

OUTPUT:

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

Sum of either of the diagonal elements is : 34

Program to enter three integers and output the smallest integer

 

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

}

Output:

82 78 86

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

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

}

Output:

26000

For a total sale value of  26000, the agent’s commission is 5200

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

}

Output:

1000 5 3

Principal = 1000

Rate = 5%

Time = 3 years

Simple Interest = 150

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);

}}}

Output:

23

23 is a prime number.

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

}

Output:

goldfish

The length of the string goldfish is 8.

 

Program to enter three integers and output the biggest integers

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

}

Output:

63 73 79

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

 

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;

}

 

Output:

12

The first 15 multiples of 12 are :

12×1=12  , 12×2=24 , 12×3=36 , 12×4=48 , 12×5=60 , 12×6=72 ,12×7=84 ,12×8=96 ,12×9=108 ,12×10=120

12×11=132 , 12×12=144 , 12×13=156 ,12×14=168 , 12×15=180

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;

}

Output:

250

You have used 250 units.

Your total bill is 125

 

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

}

Output:

this is a nice program

Words = 5

Characters = 18

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

}

Output:

12000

Salary = $12000

Your income tax @ 20% = $2400

Your net salary = $9600

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;

}

Output:

4 4 -3

The first root = 0.5

The second root = -1.5

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

}

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′

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

}

Output:

3

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.

#include <iostream.h>

#include <conio.h>

int main()

{clrscr();

int A[10][10],m,n,x,y,sum=0;

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];

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];}

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”;}

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;}

Output:

3 3

9 8 7 6 5 4 3 2 1

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

#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];

int a1,a2,quo,rem;

a2=oct/10;

a1=oct-a2*10;

for(int x=0;x<6;x++)

{

A[x]=0;

}

for (x=0;x<3;x++)

{

quo=a1/2;

rem=a1%2;

A[x]=rem;

a1=quo;

}

for(x=3;x<6;x++)

{

quo=a2/2;

rem=a2%2;

A[x]=rem;

a2=quo;

}

for(x=x-1;x>=0;x–)

{

bnum*=10;

bnum+=A[x];

}

cout <<“The binary number for the octal number “<< oct <<” is “<< bnum <<“.”<< endl;

}

output:

13

the binary number for the octal number 13 is 1011.

 

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

}

Output:

789 days =

112 weeks OR2 years.

 

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

}

output:

6

12

The number of days in this year till date = 163

 

 

 

 

 

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

}

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

#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();

}

Output:  #

Your input # is a symbol.

 

 

 

 

 

 

 

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

}

Output:

X

The next 2 letters after x are :

y

z

 

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

}

Output:

a

The ASCII code for a is 97.

Factorial Using Function

 

#include<iostream>

#include<conio.h>

 

using namespace std;

 

//Function

long factorial(int);

 

int main()

{

 

 

int counter, n;

 

 

cout<<“Enter the Number :”;

cin>>n;

 

 

cout<<n<<” Factorial Value Is “<<factorial(n);

 

 

getch();

return 0;

}

 

 

long factorial(int n)

{

int counter;

long fact = 1;

for (int counter = 1; counter <= n; counter++)

{

fact = fact * counter;

}

 

return fact;

}

 

Output

Enter the Number :6

6 Factorial Value Is 720

Program Book Entry Using structure Variable

 

#include<iostream.h>

#include<stdio.h>

 

struct books

{

char name[20],author[20];

}a[50];

int main()

{

 

int i,n;

cout<<“No Of Books[less than 50]:”;

cin>>n;

cout<<“Enter the book details\n”;

cout<<“———————-\n”;

 

for(i=0;i<n;i++)

{

cout<<“Details of Book No “<<i+1<<“\n”;

cout<<“Book Name :”;

cin>>a[i].name;

cout<<“Book Author :”;

cin>>a[i].author;

cout<<“———————-\n”;

}

cout<<“================================================\n”;

cout<<” S.No\t| Book Name\t|author\n”;

cout<<“=====================================================”;

for(i=0;i<n;i++)

{

cout<<“\n  “<<i+1<<“\t|”<<a[i].name<<“\t| “<<a[i].author;

}

cout<<“\n=================================================”;

 

return 0;

}

Output

No Of Books[less than 50]:2

Enter the book details

———————-

Details of Book No 1

Book Name :Programming

Book Author :Dromy

Details of Book No 2

Book Name :C

Book Author :Byron

=======================================================

S.No | Book Name |author

=======================================================

1 |Programming | Dromy

2 |C | Byron

=======================================================

if..else Statement program

 

 

#include<iostream>

#include<conio.husing namespace std;

 

int main()

{     int a;

 

cout<<“Enter the Number :”;

cin>>a;

if(a > 10)

{

cout<<a<<” Is Greater than 10″;

}

else

{

cout<<a<<” Is Less than/Equal 10″;

}

getch();

return 0;

}

 

Output 1:

Enter the Number :20

20 Is Greater than 10

Output 2:

Enter the Number :5

5 Is Less than/Equal 10

 

Simple Class Example Program For Find Prime Number

 

#include<iostream>

#include<conio.h>

 

using namespace std;

class prime

{

int a,k,i;

public:

 

prime(int x)

{

a=x;

}

void calculate()

{

k=1;

{

for(i=2;i<=a/2;i++)

if(a%i==0)

{

k=0;

break;

}

else

{

k=1;

}}}

void show()

{

if(k==1)

cout<<“\n”<<a<<” is Prime Number.”;

else

cout<<“\n”<<a<<” is Not Prime Numbers.”;

}

};

int main()

{

int a;

cout<<“Enter the Number:”;

cin>>a;

prime obj(a);

obj.calculate();

obj.show();

getch();

return 0;

}

 

 

Sample Output:

Enter the Number:10

10 is Not Prime Numbers.

 

Enter the Number:7

7 is Prime Number.

 

While Loop Example Program

 

 

 

#include<iostream>

#include<conio.h>

 

using namespace std;

int main()

{int a;

cout<<“Enter the Number :”;

cin>>a;

 

int counter = 1;

 

while (counter <= a)

{

cout<<“Execute While “<<counter<<” time”<<endl;

counter++;

}

getch();

return 0;

}

 

Output:

Enter the Number :4

Execute While 1 time

Execute While 2 time

Execute While 3 time

Execute While 4 time

 

 

The end

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s