Programming Fundamentals Lab

Posted on at


 

 

 


EXPERIMENT PART:
1) Read the following C++ program for understanding. Add some suitable declarations to complete the program and run it.
Source code:
#include<iostream>
#include<conio.h>
void main()
{

float units;
float price ;
float cost,tax;
double total;
int idnumber = 12583;

cout<<"enter number of units"<<endl;
cin>>units;
cout<<"enter price for the unit"<<endl;
cin>>price;

cost = price*units;
cout <<"idnumber = " <<idnumber<< endl << "units = " << units <<endl
<< "price = " << price << endl << "cost = " << cost << endl;

tax = cost*0.06;
total = cost + tax;
cout << "tax = " << tax << endl
<< "total = " << total << endl;
Getch();
}

 

 

 


2) Run the following program as it is, check the errors, correct them and determine the output:

 

SOURCE CODE:

#include<iostream>
#include<conio.h>
void main( )
{
int i;
cout << "Please enter an integer value: "<<”\n”;
cin >> i;
cout << "The value you entered is " << i;
cout << " and its double is " << i * 2 << endl;
getch();
}

 

 

 

EXERCIE PART:

1) Write and run a program that performs the following steps:
EXERCISES
a. Asks user to enter the value of radius r.
b. Calculates the circumference using the formula: C = 2 X Pi X r.
c. Displays the circumference.

“SOURCE CODE”

#include <iostream>
#include<conio.h>
void main()
{
float r;
float c;
const float Pi=3.14159;
cout<<"enter radius\n";
cin>>r;
c=2*Pi*r;
cout<<"circumfernce is"<<c<<endl;
getch();
}

 

 

 

 

 

 

 

 

 

 

2) Write and run a program that gets two numbers from user and displays their
a. Addition
b. Su btraction
c. Multiplication
d. Division
e. Remainder

 


SOURCE CODE:
#include <iostream>
#include<conio.h>
void main()
{
int a;
int b;
float ans;
cout<<"enter ist number\n";
cin>>a;
cout<<"enter second number\n";
cin>>b;
ans=a+b;
cout<< "addition of the numbers is = "<<ans<<endl;
ans=a-b;
cout<<"subtraction of the numbers is ="<<ans<<endl;
ans=a*b;
cout<<"multiplication of the numbers is ="<<ans<<endl;
ans=a%b;
cout<<"remainder of the numbers is ="<<ans<<endl;
ans=a/b;
cout<<"division of numbers is ="<<ans<<endl;
getch();
}


3) Write a program to get three sides of a triangle and calculates its area using the formula:
area = sqrt[s(s-a)(s-b)(s-c)] where s = (a + b + c) / 2. Use math library to find the square
root of values.

Source code:
{
float a,b,c,s,A;
cout<<"enter sides of triangle\n";
cin>>a;
cin>>b;
cin>>c;
s=(a+b+c)/2;
A=sqrt(s*(s-a)*(s-b)*(s-c));
cout<<"area of triangle is="<<A;
}



 


11. Write a program that prompts the user for their quarterly water bill for the last four quarters.
The program should find and output their average monthly water bill. If the average bill exceeds
$75, the output should include a message indicating that too much water is being used. If the
average bill is at least $25 but no more than $75, the output should indicate that a typical amount
of water is being used. Finally, if the average bill is less than $25, the output should contain a
message praising the user for conserving water. Use the sample run below as a model for your
output.
Sample Run 1:
Please input your water bill for quarter 1:
300
Please input your water bill for quarter 2:
200 Muhammad Usman Programming Fundamentals Spring 2011
Please input your water bill for quarter 3:
225
Please input your water bill for quarter 4:
275
Your average monthly bill is $83.33. You are using excessive amounts of water


Source code:
#include<iostream>
#include<conio.h>
void main()
{
float A,B,C,D;
float average;
cout<<"Please input your water bill for quarter 1: ";
cin>>A;
cout<<"Please input your water bill for quarter 2: ";
cin>>B;
cout<<"Please input your water bill for quarter 3:";
cin>>C;
cout<<"Please input your water bill for quarter 4: ";
cin>>D;
average =(A + B + C + D) / 12;
cout<<"average is"<<average<<endl;
if(average>75)
{
cout<<"too much water is used";

}
else if(average<25)
{
cout<<"thats good you are using water in a good way\n";
}
getch();
}

 

 

 



About the author

Saif-Filmannex

I am doing Bs Electronics Engineering from International Islamic University

Subscribe 0
160