Assignment C++ Programming
Assignment C++ Programming
QUESTIONS
1. Who is Written C++
C++ programming is one of the most popular and widely used object-oriented programming
language which was developed by Bjarne Stroustrup in 1979. C++ is derived from C
programming. So, almost all code that run on C runs correctly on C++. If you have good
understanding of basic features of C programming, you will have a head start learning C++.
b. While
Output
Enter a positive integer: 4
Factorial of 4 = 24
In this program, user is asked to enter a positive integer which is stored in variable number
(supposed user entered 4). Here is the working of while loop in this program:
1.
2.
3.
4.
5.
Output
Enter a number: 4
Enter a number: 3.4
Enter a number: 6.7
Enter a number: -4.5
Enter a number: 0
Sum = 9.6
d. While True
I am writting a program that gives us the whole and the decimal part of the number given by the user.
The program then asks the user if he wants to restart the program, this is a that of the code and if
someone could please explain me what does the while(true) loop do.
1 while (true)
2
{
3
4
if (started != -1)
5
{
6
cout << "Do you want to restart the program? (1 - yes, 0 7 no)" << endl;
8
while (started < 0 || started > 1)
9
cin >> started;
10
if (started == 0)
11
12
break;
}
e. Do/While
Output
Enter a positive integer: 4
Factorial of 4 = 24
In this program, user is asked to enter a positive integer which is stored in variable number
(supposed user entered 4). Here is the working of while loop in this program:
1.
2.
3.
4.
5.
f. Jump / Loop
Outer loop executing. i = 0
Inner loop executing. j = 0
Inner loop executing. j = 1
Outer loop executing. i = 1
Inner loop executing. j = 0
Inner loop executing. j = 1
Outer loop executing. i = 2
Inner loop executing. j = 0
Inner loop executing. j = 1
Outer loop executing. i = 3
Inner loop executing. j = 0
Jumped to stop. i = 3
g. If/ else
C++ if Statement
The if statement checks whether the test condition is true or not. If the test condition is true,
it executes the code/s inside the body of if statement. But it the test condition is false, it
skips the code/s inside the body of if statement.
Working of if Statement
The if keyword is followed by test condition inside parenthesis ( ). If the test condition is
true, the codes inside curly bracket is executed but if test condition is false, the codes
inside curly bracket { } is skipped and control of program goes just below the body of if as
shown in figure above.
Flowchart of if
Output 1
Enter an integer: 5
You entered a positive number: 5
This statement is always executed because it's outside if statement.
Output 2
Enter a number: -5
This statement is always executed because it's outside if statement.
C++ if...else
The if...else executes body of if when the test expression is true and executes the body
of else if test expression is false.
The if statement checks whether the test expression is true or not. If the test condition is
true, it executes the code/s inside the body of if statement. But it the test condition is false,
it executes the code/s inside the body of else .
Flowchart of if...else
if ( number >= 0) {
cout << "You entered a positive integer: "<<number<<endl;
}
else {
cout<<"You entered a negative integer: "<<number<<endl;
}
cout<<"This statement is always executed because it's outside if...else
statement.";
return 0;
}