Professional Documents
Culture Documents
Final Revision
Final Revision
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;
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>
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;
Example 5
// C++program to check whether a number is positive, negative or zero entered from 5 different
users.
#include <iostream>
int main() {
int 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>
int main() {
int n;
int sum = 0;
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:
int main()
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