0% found this document useful (0 votes)
32 views

Programming Fund Lab 01

This document discusses programming fundamentals and data types in C++. It defines variables and their syntax, lists valid C++ data types like int, float, double, char, string, and bool. Examples are given of declaring variables of each data type and assigning values. The document also provides a programming task to define and initialize two float variables, calculate their sum and difference, and display the results.

Uploaded by

iltamasjaat
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Programming Fund Lab 01

This document discusses programming fundamentals and data types in C++. It defines variables and their syntax, lists valid C++ data types like int, float, double, char, string, and bool. Examples are given of declaring variables of each data type and assigning values. The document also provides a programming task to define and initialize two float variables, calculate their sum and difference, and display the results.

Uploaded by

iltamasjaat
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

PROGRAMMING FUNDAMENTALS LAB

- DATA TYPES AND VARIABLES :-


1. Variables:

�Data such as numbers, characters, or even complete records are stored in


variables to
enable their processing by a program. Variables are also referred to as
objects,
particularly if they belong to a class.�

Defining Variables:

�A variable must be defined before you can use it in a program. When you
define a variable,
the type is specified and an appropriate amount of memory reserved. This
memory space is
addressed by reference to the name of the variable. �

A simple definition has the following syntax:-

SYNTAX: type variable list;

Valid Declaration:-
1. Int i, a, j;
2. Char c, ch;
3. Float f, salary
4. Double d;

Rules for naming a variable:-

1. A variable name can only have alphabets, numbers, and the underscore _.
2. A variable name cannot begin with a number.
3. It is a preferred practice to begin variable names with a lowercase
character. For example, name is preferable to Name.
4. A variable name cannot be a keyword. For example, int is a keyword that
is used to denote integers.
5. A variable name can start with an underscore. However, it's not
considered a good practice.

C++ Data Types:-

There are different type of variables:


1. int: stores integers (whole number), without decimals, such as 123 or -
123.
2. Float: are used to store floating-point numbers (decimals and
exponentials).
3. Double: stores floating point number, with decimals, such as 19.99 or -
19.99.
4. Char: stores single characters such as �a� or �b�. Char values are
surrounded by single quotes.
5. String: stores text, such as �hello world�. String values are surrounded
by double quotes.
Variable Example:-

1. Int myNum= 5;
2. Float myfloat = 3.1234567;
3. Double myFloatNumber= 5.99999999999;
4. Char myChar = �d�;
5. String myText = �hello�;
6. Bool myBoolean= true;

Task 1:

Write a C++ program that two defines variables for floating-point numbers and
initializes them with the values 123.456 and 76.543. Then display the sum
and the difference of these two numbers on screen.

Solution:

#include <iostream>

using namespace std;

int main()
{
float num1 = 123.456;
float num2 = 76.543;
float num3 = num1 + num2;
float num4 = num1 - num2;
cout<<"sum of two number:"<<num3<<endl;
cout<<"diff of two number:"<<num4;
}

You might also like