Programming With C++: (TASK 4)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Ministry of Higher Education

Higher Technological Institute

10th of Ramadan City

Biomedical Engineering Department

PROGRAMMING
with C++
[TASK 4]
Student Name: Ahmed Fouad Mohamed
ID: 20190118

Course Code: EDE 125


Group: (81)

Supervisor
Dr. Nour S. Bakr
Eng . Manar Fathy

1
3.1
#include <iostream>
using namespace std;

int main()
{
int ID;
cout << "Name:Ahmed Fouad Mohamed Waghe " << endl;
cout << "________________________________" << endl;
cout << "Enter ID : "; cin >> ID;
cout << "________________________________" << endl;
cout << " prints too many if variable count exceeds 100 " << endl;
cout << "____________________________________________________" << endl;
int number;

cout << "Enter number : ";


cin >> number;
if (number <= 100)
cout << "number is : " << number << "\n";
else if (number >100)
cout << "too many \n";
else

cout << "invalid \n";


}

2
3.3

What is wrong with this code:


cout << "Enter n: ";
cin >> n;
if (n < 0)
cout << "That is negative. Try again." << endl;
cin >> n;

ans
int n;

cout << "Enter n: ";


cin >> n;
if (n < 0)
cout << "That is negative. Try again." << endl;
cin >> n;

3.9

What is wrong with this code:


if (x = 0) cout << x << " = 0\n";
else cout << x << " != 0\n";

ans
int x;

cin >> x;
if (x == 0) cout << x << " = 0\n";
else cout << x << " != 0\n";

3.14
What is the difference between the following two statements:
if (n > 2) { if (n < 6) cout << "OK"; } else cout << "NG";
if (n > 2) { if (n < 6) cout << "OK"; else cout << "NG"; }

ans

the first one : the code end after the } and it prints ok if n<6
second one : the code run if n<6 and print ok if it don’t this condition it prints NG

3
3.1
Modify the program in Example 3.1 on page 36 so that it prints a response
only if n is divisible by d.

This program tests if one positive integer is not divisible by another:


int main()
{
int n,d;
cout << "Enter two positive integers: ";
cin >> n >> d;
if (n%d) cout << n << " is not divisible by " << d << endl;
}

Ans

#include <iostream>
using namespace std;

int main()
{
int n, d;
cout << "Enter two positive integers: ";
cin >> n >> d;
if (n / d) cout << n << " is divisible by " << d << endl;
}
3.5
Write and run a program that reads the user’s age and then prints “You are a child.” if the
age < 18, “You are an adult.” if 18  age < 65, and “You are a senior citizen.” if age  65.
Ans

#include <iostream>
using namespace std;

int main()
{
int age;
cout << "Enter age : ";
cin >> age;
if (age != 0 &age < 18)
cout << "You are a child \n";

else if (age >=18 & age<= 65)


cout << "You are a adult \n";
else if (age > 65)
cout << "You are a senior citizen \n";
else
cout << "Error\n";

4
3.9

Write and run a program that plays the game of “Rock, paper, scissors.” In this game, two
players simultaneously say (or display a hand symbol representing) either “rock,” “paper,” or
“NER.” The winner is the one whose choice dominates the other. The rules are: paper
dominates (wraps) rock, rock dominates (breaks) scissors, and scissors dominate (cut) paper.
Use enumerated types for the choices and for the results.
Ans
#include <iostream>
using namespace std;

int main()
{
int A, B;
cout << "____________________\n"
" 1- if you want rock enter 1 \n"
" 2- if you want paper enter 2 \n"
"3- if you want scissors enter 3 \n"
"___________________\n";
cout << "PLAYER 1 ENTER THE VALUE : "; cin >> A;
cout << "PLAYER 2 ENTER THE VALUE : "; cin >> B;
if (A == 1 & (B == 2 || B == 3))
cout << "PLAYER 1 IS WINNER" << endl;
else if (A == 3 & B == 2)
cout << "PLAYER 1 IS WINNER" << endl;
else if (A == 2 & (B == 1 || B == 3))
cout << "PLAYER 2 IS WINNER" << endl;
else
cout << " Invalid input \n";

5
The difference between flowcharts and algorithm and pseudocode
Algorithm:
➢ An algorithm is a sequence of instructions used to solve a particular problem
➢ Flowchart and pseudo code are tools to document and represent the algorithm.in other
words
➢ Algorithm is used to solve a particular problem
➢ An algorithm can be represented using a flow chart or pseudo code
Flow Chart:
➢ Flow Chart is a Graphical representation of an algorithm
pseudo-Code:
➢ pseudo code is readable
➢ Formally styled English like language representation of algorithm
Some important points about flow chart and Algorithm:
➢ Both pseudo code and algorithm structured and constructs of a programming language for
a representation
➢ The user does not require the knowledge of a programming language to write understand
a flow chart or a pseudo code

You might also like