This document covers fundamental concepts in C++ programming, including variable declaration, memory allocation, and data types. It explains how to initialize variables, use the 'cin' function for user input, and perform arithmetic operations with examples. Key data types discussed include int, float, double, and char, along with their memory requirements.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
7 views3 pages
PF Week 2
This document covers fundamental concepts in C++ programming, including variable declaration, memory allocation, and data types. It explains how to initialize variables, use the 'cin' function for user input, and perform arithmetic operations with examples. Key data types discussed include int, float, double, and char, along with their memory requirements.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3
Week 2
1. Variable Declaration and Memory Concepts
Variables: o Variables are like containers in which we store data or values. o In C++, we declare a variable by specifying its type (e.g., int, float) and giving it a name (e.g., age, price). Memory Allocation: o Each variable occupies space in the computer’s memory. o The amount of memory a variable takes depends on its type: int (integer) usually takes 4 bytes. float (floating-point for decimal numbers) takes 4 bytes. char (character) usually takes 1 byte. Example: cpp Copy code int age = 25; // integer variable named age, with a value of 25 float price = 9.99; // floating-point variable named price, with a value of 9.99 2. Data Types Basic Data Types: o int: Used for whole numbers (e.g., 5, -3). o float: Used for decimal numbers (e.g., 3.14, -0.5). o double: Similar to float but with more precision (e.g., 3.14159). o char: Used for single characters (e.g., 'a', 'Z'). Example: cpp Copy code int num = 10; char letter = 'A'; 3. Initialization of Variables Initialization: o When you declare a variable, you can also assign it an initial value. o If you don’t initialize a variable, it may hold a garbage value (random value from memory). Example: cpp Copy code int age = 20; // initialized with 20 float temperature = 36.6; // initialized with 36.6 4. Input Using cin Using cin: o cin (character input) allows users to input values during program execution. o It takes data from the user and stores it in a variable. Syntax: cpp Copy code int age; cout << "Enter your age: "; cin >> age; // stores user input in age variable Explanation: o The >> symbol is the extraction operator, which tells cin where to store the input value. 5. Arithmetic Operators Operators: o Arithmetic operators perform basic math calculations. o Operators: +: Addition (e.g., a + b). -: Subtraction (e.g., a - b). *: Multiplication (e.g., a * b). /: Division (e.g., a / b). %: Modulus (remainder) (e.g., a % b gives the remainder of a divided by b). Example: cpp Copy code int a = 10, b = 3; cout << "Sum: " << a + b << endl; cout << "Difference: " << a - b << endl; cout << "Product: " << a * b << endl; cout << "Quotient: " << a / b << endl; cout << "Remainder: " << a % b << endl;