Final Revision

You might also like

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

FINAL REVISION

Computer programming 1

PREPARED BY
Dr: Nourhan Mohsen
Dr.Reham Nasser
Final revision
Example 1
// C++program that calculate the area of spheres by which radius will be entered by user.
- If area is less than 10 print “the area of sphere is small”
- If area is less than 30 print “the area of sphere is regular”
- If area is greater than 50 print “the area of sphere is big ”
#include <iostream>
using namespace std;

int main() {
float radius, area;
cout << "Enter the radius of the sphere: ";
cin >> radius;
area = 4 * 3.14 * radius * radius;
cout << "The area of the sphere is: " << area << endl;
if (area < 10) {
cout << "The area of sphere is small." << endl;
} else if (area < 30) {
cout << "The area of sphere is regular." << endl;
} else if (area > 50) {
cout << "The area of sphere is big." << endl;
}
return 0;
}
Explanation:
First, we prompt the user to enter the radius of the sphere. Then, we calculate the area using the
formula `4 * pi * r^2`, where `r` is the radius. After that, we use if-else statements to check
which range the area falls in and print the corresponding message. Finally, we return 0 to end the
program.

Example 2
// C++program that takes from customers the amount of his receipt then find their gift if:
- Receipt more than 500 the gift will be small table
- Receipt greater than 300 the gift will be a vase
- Receipt less than 200 the gift will be a watch
#include <iostream>
using namespace std;

int main() {
float receipt_amt;
cout << "Enter the amount of your receipt: ";
cin >> receipt_amt;

if(receipt_amt>500) {
cout << "Congratulations, your gift is a small table!" << endl;
}
else if(receipt_amt>300) {
cout << "Congratulations, your gift is a vase!" << endl;
}
else if(receipt_amt<200) {
cout << "Congratulations, your gift is a watch!" << endl;
}
else {
cout << "Sorry, you are not eligible for any gift." << endl;
}

return 0;
}
Explanation
the program first prompts the user to enter the amount of his receipt. It then checks the receipt
amount against different conditions and prints the appropriate gift message based on the
condition satisfied. If none of the conditions are met, it prints a message stating that the user is
not eligible for any gift.

Example 3
// c++ program find largest of three numbers where numbers entered by user using if statement

#include <iostream>
using namespace std;

int main() {
int num1, num2, num3, largest;
cout << "Enter three numbers: ";
cin >> num1 >> num2 >> num3;

largest = num1;

if (num2 > largest) {


largest = num2;
}

if (num3 > largest) {


largest = num3;
}

cout << "The largest number is: " << largest << endl;

return 0;
}

Example 4
// C++ program to Convert temperature in Fahrenheit to Celsius.
- If temperature less than 15 °C “its cold please stay warm”
- If temperature is greater than 25 °C “its sunny day you can go out”
- If temperature greater than 30 °C “its hot please stay hydrated”
NB: cel=((frh * 5.0)-(5.0 * 32))/9

#include <iostream>

using namespace std;

int main() {
float frh, cel;
cout << "Enter temperature in Fahrenheit: ";
cin >> frh;
cel = ((frh * 5.0) - (5.0 * 32)) / 9;
cout << "Temperature in Celsius is: " << cel << "°C" << endl;

if (cel < 15) {


cout << "It's cold, please stay warm." << endl;
} else if (cel > 25) {
if (cel > 30) {
cout << "It's hot, please stay hydrated." << endl;
} else {
cout << "It's a sunny day, you can go out." << endl;
}
}
return 0;
}
Explanation
In this program, the user inputs the temperature in Fahrenheit, which is then converted to Celsius
using the formula provided. After the conversion, the program checks the temperature in Celsius
and displays a message based on the conditions mentioned in the problem statement.

Example 5
// C++program to check whether a number is positive, negative or zero entered from 5 different
users.
#include <iostream>

using namespace std;

int main() {
int num;

for(int i=1; i<=5; i++) {


cout << "Enter a number: ";
cin >> num;

if(num > 0)
cout << num << " is positive." << endl;
else if(num < 0)
cout << num << " is negative." << endl;
else
cout << "The number is zero." << endl;
}

return 0;
}
Explanation
In the above code, we have used a loop to check the number for five different inputs. Inside the
loop, we take input from the user and check whether the number is positive, negative, or zero
using an if-else statement. Finally, we output the result to the console.

Example 6
//C++ program to find the sum of first n natural numbers, positive integers such as 1,2,3,...n are
known as natural numbers
#include <iostream>

using namespace std;

int main() {
int n;
int sum = 0;

cout << "Enter the value of n: ";


cin >> n;

for (int i = 1; i <= n; i++) {


sum += i;
}

cout << "The sum of first " << n << " natural numbers = " << sum << endl;

return 0;
}
Explanation
in this program, we first ask the user to enter the value of n, which is the number of natural
numbers we want to sum. We then use a for loop to iterate from 1 to n and add each number to a
variable called sum. Finally, we print the sum of the first n natural numbers.
Example 7
// C++ program to find the factorial of number
#include <iostream> output:

using namespace std;

int main()

int x, num, factorial = 1;

cout << "Type positive number: ";

cin >> num;

for (x = 1; x <= num; ++x) {

factorial *= x; // factorial = factorial * x;

cout << "Factorial of " << num << " = " << factorial;

return 0;

Explanation

We first declare two variables `num` and `factorial` to store the input number and the calculated
factorial respectively.
- We prompt the user to enter a number using `cout` and `cin`.
- We then calculate the factorial using a `for` loop, starting from 1 to `num`, and multiplying
each number iteratively with the previous product.
- Finally, we display the result using `cout`.

For instance, if the user enters `5` as the input value, the program will output:
```
Enter a number: 5
Factorial of 5 = 120

You might also like