0% found this document useful (0 votes)
5 views27 pages

Lab 3

The document contains programming assignments and examples in C++ for calculating temperature conversions, employee net salary, and implementing a simple calculator using switch statements. It also covers loops, including for, while, and do-while, with examples for calculating sums and factorials. Additionally, it includes exercises for rewriting code and determining number categories based on user input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views27 pages

Lab 3

The document contains programming assignments and examples in C++ for calculating temperature conversions, employee net salary, and implementing a simple calculator using switch statements. It also covers loops, including for, while, and do-while, with examples for calculating sums and factorials. Additionally, it includes exercises for rewriting code and determining number categories based on user input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Lab 2

Prepared by:
DR. Mennatallah Hesham Ibrahim
Presented by: Haidy Hamed
Department of Computer Science
Assignment
 Draw a flowchart and write a program that
calculate Kelvin and Fahrenheit from a Celsius
degree entered by the user.
k = c + 273
f = c * 5/9 + 32
Program Code
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
float c,k,f;
cout << "Please Enter a Celsius Degree: ";
cin >> c;
k = c + 273;
f = c * 5/9 + 32;
cout << "Kelven: " << k << '\n';
cout << "Fehrenhit: " << f << '\n';
system("PAUSE");
return EXIT_SUCCESS;
}
Exercise 1
 Write a C++ program that accepts an employee salary and a
tax percentage then computes the monthly net salary (ns)
and determine his income level according to the following
table:
Code
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int basic_salary,tax_percent,net_salary,tax_value;
cout << "Please enter your net basic salary: ";
cin >> basic_salary;
cout << "Please enter your tax percentage: ";
cin >> tax_percent;
tax_value=basic_salary*tax_percent/100;
net_salary=basic_salary-tax_value;
cout << "Your net salary: " << net_salary<<endl;
Code
if(net_salary<3000)
cout << "Low"<<endl;
else if(net_salary<8000)
cout << "Moderate"<<endl;
else
cout << "Excellent"<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
The Switch Statement
 Is called multiple selection structure
 Selects the action to be performed from many different
actions
 It is not necessary to enclose a multi-statement case in
braces
The Switch Statement
Advanced Calculator
 Write a program that simulate a simple calculator that
accepts two operands (use floating point) and the desired
operation (+, - , %, * , /) then displays the result. It prevents
division by 0. When it finishes the calculation the program
should ask the user whether he wants to do another
calculation or not
Code
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
double num1,num2,result;
char Op;
cout << "Enter the first number : ";
cin >> num1;
cout << "Enter your operation : ";
cin >> Op;
cout << "Enter the second number : ";
cin >> num2;
Code (Cont.)
switch (Op)
{
case '+' :
result=num1+num2;
break;
case '-' :
result=num1-num2;
break;
case '*' :
result=num1*num2;
break;
case '%' :
result=(int)num1%(int)num2;
break;
Code (Cont.)
case '/' :
if (num2 == 0)
cout << "You cannot divide by zero";
else
result=num1/num2;
break;
default :
cout << "Sorry your operation is wrong "<<endl;
}
cout << "Result= " << result;
system("PAUSE");
return EXIT_SUCCESS;
}
Exercise 2: Rewrite the following code using
nested if/else. What does it print out?
int x=7;
int y=3;
char op='%';
switch(op)
{
case '-':
cout <<x-y;
break;
case ‘+':
cout <<x+y;
break;
case '*':
cout <<x*y;
break;
}
Answer
int x=7;
int y=3;
char op='%';
if(op=='-')
cout <<x-y;
else if(op==‘+')
cout <<x+y;
else
cout <<x*y;
Repeated Processing
1. for Loop
2. while Loop
3. do … while Loop
Loops
 A control structure that cause a statement or a
group of statements to repeat
for Loop
while Loop
 Has 2 parts:
1. Condition that is tested for a true or false value
2. A statement or block that is repeated as long as the expression
is true (loop body)
 Format
while(expression)
{
Statement;
Statement;
}
 Pretest loop
do … while Loop
 Has 2 parts:
1. Condition that is tested for a true or false value
2. A statement or block that is repeated as long as the expression
is true (loop body)
 Format
do
{
Statement;
Statement;
} while(expression);
 Postest loop
 Guaranteed to loop at least one time
Program 1
 Write a program that calculate the following
S= 1+4+7+10+….n
Code
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int sum=0,n;
cout << "Please Enter value for n: ";
cin>>n;
for(int x=1;x<=n;x+=3)
{
sum+=x;
}
cout << "Sum = " << sum << '\n';
system("PAUSE");
return EXIT_SUCCESS;
}
Program 2
 Writ a program that accepts a number and display it’s
factorial (factorial(x)=x*(x-1)*(x-2)*(x-3)….*3*2*1)
Code
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int x,fact=1;
cout <<"Enter a number: "<<endl;
cin>>x;
if(x==0)
cout<< "Factorial of 0 is " << fact <<endl;
else if(x<0)
cout<< " No Factorial "<<endl;
else
{
for(int i=x;i>=1;i--)
fact=fact*i;
}
cout <<"Factorial of "<<x<<" is "<<fact<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Program 3
 Write a program that reads a number between 1 and 10 from
the user and print “small” if the number is 1,2 or 3 ,print
“medium” if the number is 4,5 or 6, otherwise print large.
Code
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int x;
do
{
cout << "Please enter a number";
cin >> x;
}while (x>10 || x<1);
if(x <= 3)
cout << "small" << endl;
else if(x<=6)
cout << "medium" << endl;
else
cout << "large" << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
My Contact
 Haidy.hamed.mustafa@gmail.com
☺ THANK YOU ☺

You might also like