Basic programming course: Lesson #2 - Variables and Types of Data

in devjr-s20w2 •  yesterday  (edited)

Assalam-o-Alaikum!!!

Greetings to my all STEEMIT members. hopefully you al are fine and I am also fine ALHAMDULILLAH. And Today I try my best to complete all homework tasks

image.png

Q:1:What are variables and what are they used for?

Variables are symbolic names or identifiers used to store data or values that can be manipulated during program execution. Variables allow you to refer to data without knowing its specific value at compile time, and their contents can change as the program runs.

  • Uses of Variables:

  • Storing Information: Variables store data that can be reused or modified in programs. For example, storing the result of a calculation or user input.

  • Code Readability: Variables allow you to assign meaningful names to data, making code easier to read and maintain.

  • Flexibility: You can easily change a variable's value and have the rest of the program adjust accordingly.

  • Data Manipulation: Variables allow programmers to perform operations on data, such as mathematical computations, string manipulation, etc.

  • Control Flow: Variables often control the flow of a program by storing states or conditions. For example, in loops or conditional statements
Assign a type of data to the following variables and explain why: email, phone, working_hours, price_steem, and age

1. email

Data Type: String (Text)
Explanation: Emails consist of alphanumeric characters, symbols (like @ and .), and typically do not involve numerical calculations. Thus, they are best represented as a string (text) data type.

2. Phone

Data Type: String (Text)
Explanation: Even though phone numbers contain digits, they are not used in mathematical operations. They also may include symbols like +, -, or spaces, so they are better represented as strings rather than integers or floats.

3. working_hours

Data Type: Integer or Float (Number)
Explanation: Working hours typically represent a count of hours worked or available. This value can be an integer if it's a whole number (e.g., 8 hours), or a float if it includes fractions of an hour (e.g., 7.5 hours).

4. price_steem

Data Type: Float (Number)
Explanation: Prices are often represented with decimal points to account for cents or small units of currency. A float data type is appropriate for monetary values like the price of Steem (a cryptocurrency).

5. age

Data Type: Integer (Number)
Explanation: Age is typically expressed as a whole number representing years. Since there are no fractions of years commonly used in this context, an integer data type is appropriate.

Summary:
String for email and phone because they contain characters, not just numbers.
Integer or Float for working_hours depending on whether fractions are needed.
Float for price_steem due to the need for precision with decimals.
Integer for age as it represents a whole number.

Explain how the following code works:

Algorithm names
Define name, last_name as Character;
Print "Enter your name:";
Read name;
Print "Enter your last name:";
Read last_name;
Print "Hello " name " " last_name ", welcome";
EndAlgorithm

Ans:

Declare two char variables name and last_name to store single characters.
Display a message asking for the user's name: "Enter your Name:"
Accept a single character input and store it in name.
Display a message asking for the user's last name: "Enter your Last Name:"
Accept a single character input and store it in last_name.
Print a greeting message: "Hello [name] [last_name] welcome".

  • Algorithm names:

This marks the start of the algorithm, named "names." It's essentially labeling this block of instructions for reference or identification.

  • Define name, last_name as Character;

This line declares two variables: name and last_name. These variables are of type Character, which means they are intended to store strings of text (e.g., a person's name).

  • Print "Enter your name:"

The program outputs the text "Enter your name:" to the user. This is a prompt asking the user to provide their first name.

  • Print "Enter your last name:"

The program then prompts the user with "Enter your last name:", asking for their last name.

  • Print "Hello " name " " last_name ", welcome";

This line prints a personalized greeting to the user. The program combines the string "Hello " with the contents of the name and last_name variables, and finishes with ", welcome". The result is a full sentence is in this example.

image.png

OUTPUT:

image.png

Develop a pseudo-code to calculate the value in USD of X STEEM. For this you can rely on this algorithm:

Ans:

Declare three variables: price_steem, amount_steem, and total.
Input price_steem from the user (ask for the current price of STEEM in USD).
Input amount_steem from the user (ask for the amount of STEEM the user has)
Calculate the total value:
total = amount_steem * price_steem
Output the value of total (the value of STEEM in USD).

image.png

OUTPUT

image.png

25% to @null

Special thanks to @alejos7ven

Invite Friends:
@abdullahw2
@yancar
@memamun

Regards:@arinaz08

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Upvoted. Thank You for sending some of your rewards to @null. It will make Steem stronger.