Introduction & Execution of 1st code
The language that the operating system understands is different from any human language or programming language. This is called “machine language”. Writing programming codes to communicate with the machine using machine language is a tedious task that require number of programming instructions.
Writing, Executing and Saving a C++ Code:
Every student must familiar with typing, execution and saving of a C++ code. The software which the student will be use for developing C++ code is Microsoft Visual Studio 2013.
Visual C++ Editor:
This creates a C++ file in which you will have to enter your C++ code.
Visual C++ Compiler:
Compiler is used to convert high level language into low level language.
Any statement written inside /* */ this or outside this // called comments C++ compiler did not translate these comments.
The first line written in C++ workspace is #include<iostream> this is called pre-processing directive. These directives cause the source program to be modified before compiling process begins.
void main( )/ int main( ):
This statement indicates the declaration of a main method or main function header. Any thing written inside these parenthesis is part main method.
The cout statement prints Hello World on the output screen.
Q 1:Display your name,registration number and session on the output screen
using namespace std;
cout << “Ahmed Mujtaba” << endl;
cout << “0015” << endl;
cout << “Fall 2016” << endl;
Q 2:Display addition ,subtraction and division of two numbers on the screen
Q 3:Calculate the following
- 5*(13+89) – (34 – 12)
- (12+7) / 9 – (45 + 11) / 6
C++ data types and Arithmetic Operations
C++ Data Types:
The most common data types that will be used in C++ Programming are int,double,float,char,bool,etc. Data types define the nature of a declared variable.
Data Type Use
1)double or float decimal values
2)char character variables
In C++ arithmetic operations are used to perform mathematical calculations such as +, -, *, /.
This command is used to take input from user.
Q 1: Take two integers from the user and implement addition,subtraction and division
Q 2: Determine the value of y=a^3+b^3+c^3+3abc(a+b+c) .Take the variables from user
Q 3:Implement a code to convert x kg into y pounds. (1 kg=2.204 pounds)
Control and Conditional statements
The most common conditional statement is If statement. It allows the program to make a decision on true and false condition of the statement.
If else statement:
The if-else selection statement allows the programmer to specify the different actions are to be performed if the condition is true than when condition is false.
Nested if-else refers to implementing an if-else block within another if or else block.
Q 1: Implement a program which decides if a number enter from user is divisible by 2.
Q 2: Determine the roots of a quadratic equation.
Q 3: Determine number is even or odd.
While and Do-While Loop
The while repetition allows the user to execute a particular set of instructions repeatedly while some conditions remain true. The while loop is very similar to for loop. The syntax of a general while loop is as follows:
Every while loop can be converted into a do-while but not vice versa. The reason is that the do-while loop always executes at least once. The basic syntax for the do-while is.
Q 1:Determine the number enter by user is a palindrome or not.
Q 2:Count how many 5 are present in series.
The for repetition statement handles all the details of counter-controlled repetition. The for loop consists of three parts initialization,condition and update.
for(initialization ; condition ; update)
Q 1:Implement a program that calculates the sum of all integers from 0 to 10.
Q 2:Implement a program using for loop that prints multiple of 5 from 0 to 100
Q 3:Implement a code that calculates the mathematical constant ‘e’ for power 1 and n terms taken by user.
Q 4:Determine the factorial of a number entered by user.