Lab 7 (Loops) Updated
Lab 7 (Loops) Updated
Lab 07
Topic Loop Statement
Objective Learning how to use repetition statements.
Repetition:
In repetition, the program repeats particular statements a certain number of times based on some
conditions. Normally three types repetition statement are used:
Counter control:
In this kind of repetition, a set of statement repeats specific number of times according to the
requirement. You know exactly how many times certain statements need to be executed.
Sentinel control:
You do not always know how many pieces of data (or entries) need to be read, but you may know that
the last entry is a special value, called a sentinel.
Flag control:
A flag variable is a bool variable that indicates whether a condition is true or false.
for:
Syntax:
The initial statement usually initializes a variable (called the for loop control, or for indexed, variable). In
C++, for is a reserved word.
while:
Syntax:
The expression provides an entry condition to the loop. If it initially evaluates to true, the statement
executes. The loop condition—the expression—is then reevaluated. If it again evaluates to true, the
statement executes again. The statement (body of the loop) continues to execute until the expression is
no longer true. A loop that continues to execute endlessly is called an infinite loop. To avoid an infinite
loop, make sure that the loop’s body contains statement(s) that assure that the entry condition—the
expression in the while statement—will eventually be false. In C++, while is a reserve word.
Task 1
Write a program in which the user enters a number X and your program prints all the numbers from X to
zero and then back up again. (using for loop)
Expected Output
Enter a number: 5
54321012345
#include <iostream>
using namespace std;
int main(){
int num;
cout << "enter a number =";
cin >> num;
for (int i = num; i >= 0; --i){
cout << i;
}
for (int j = 1; j <= num; j++){
cout << j;
}
system("pause");
}
Task 2
Write a program in which the user enters two numbers X and Y and your program prints all the numbers
that are divisible by 3 or 5 between X and Y.(using for loop)
Expected Output
Example 1
Enter 1st number: 3
Enter 2nd number: 14
Number divisible by 3 or 5 are: 3
Number divisible by 3 or 5 are: 5
Number divisible by 3 or 5 are: 6
Number divisible by 3 or 5 are: 9
Number divisible by 3 or 5 are: 10
Number divisible by 3 or 5 are: 12
Example 2
Enter 1st number:11
Enter 2nd number:2
Number divisible by 3 or 5 are: 3
Number divisible by 3 or 5 are: 5
Number divisible by 3 or 5 are: 6
Number divisible by 3 or 5 are: 9
Number divisible by 3 or 5 are: 10
#include <iostream>
using namespace std;
int main(){
int a, b;
cout << "enter a number =";
cin >> a;
cout << "enter a number =";
cin >> b;
for (int i = a; i <= b; i++){
if ((i % 3 == 0) || (i % 5 == 0)){
cout << "number divisible by 3 or 5 are = " << i << endl;
}
}
system("pause");
}
Task 3
Write a program in which the user enters a positive number and if user enters negative number ask user
to enter a positive number again unless positive number is entered (use do while) and your program
displays its digit in reverse order. (use while loop)
Expected Output:
1731
Task 4
You have to show every digit of the number separately (use while loop)
Example: number is 4235
Output should be
Number is 4235
Digits of the number are 5, 3, 2 and 4
[Can you find a way to show the digits in the following order?]
Number is 4235
Digits of the number are 4, 2, 3 and 5
#include <iostream>
using namespace std;
int main(){
int num; cout << "enter a number = ";
cin >> num;
int rev = 0;
while (num > 0){
rev = num % 10;
cout << rev;
if (num / 10 == 0){
cout << ",";
}
num = num / 10;
cout << ",";
}
system("pause");
}
Task 5
You have to find sum of the digits of the number (use while loop)
Example: number is 4235
Output should be
Number is 4235
Digits of the number are 5, 3, 2 and 4
Digit sum for the number is 14
include <iostream>
using namespace std;
int main(){
int num;
int sum = 0;
cout << "enter a number = ";
cin >> num;
int rev = 0;
while (num > 0){
rev = num % 10;
cout << rev;
sum = sum + rev;
num = num / 10;
cout << ",";
}
cout << "sum of numbers are " << sum << endl;
system("pause");
}
Task 6
You have to find sum of the square of every digits of the number (use while loop)
Example: number is 4235
Output should be
Number is 4235
Digit is 5 its square is 25
Digit is 3 its square is 9
Digit is 2 its square is 4
Digit is 4 its square is 16
Square Digit sum for the number is 54
#include <iostream>
using namespace std;
int main(){
int num;
int sum = 0;
int x = 0;
cout << "enter a number = ";
cin >> num;
int rev = 0;
while (num > 0){
rev = num % 10;
x = rev*rev;
cout << "digit is " << rev << "and its square is = " << x << endl;
sum = sum + x;
Task 7
You have to find the reverse number of the number (use while loop)
Example: number is 4235
Output should be
Number is 4235 its reverse number is 5324
[Here you have to find a good way to construct the new number from the given digits, start that work
on paper, get one digit at a time and see what you have to do when a new digit comes in to add]
#include <iostream>
using namespace std;
int main(){
int num;
int rev = 0;
cout << "enter a number ";
cin >> num;
cout << "number is " << num;
while (num > 0){
rev = (rev * 10) + (num % 10);
Task 8
You have to find every number factorial N! = 1 * 2 * 3 * 5 * 6 * 7 * 8 *. . * N (use for loop)
Example: number is 8
Output should be
Factorial for number is [8 * 7 * 6 * 5 * 4 * 3 * 2 * 1] is 40320
[Keep in mind that you cannot calculate factorial for very large numbers easily]
#include <iostream>
using namespace std;
int main(){
int num;
int fact = 1;
cout << "enter a number ";
cin >> num;
for (int i = 1; num >= i; num--){
fact = fact*num;
if (num >= 1){
cout << num << " * ";
}
}
cout << "] is "<< fact;
system("pause");
}
Task 9
You have to find which numbers are prime(use for loop)
A number is prime if it is only divisible by one and itself and it has only two unique divisors
Example: number is 17
Output should be
17 is the prime number
[Keep in mind that prime numbers start from 2]
#include <iostream>
using namespace std;
int main(){
int num;
int count = 0;
cout << "enter a number ";
cin >> num;
for (int i = 1; i <= num; i++){
if (num%i == 0){
count++;
}
}
if (count == 2){
cout << "number is prime ";
}
else
cout << "number is not prime ";
system("pause");
}
Task 10
You have to find which number is Armstrong number
Armstrong number is n digit number where the sum of nth power of its each digit is equal to the same
n digit number.
[Keep in mind the number of digits in a number]
Thus an Armstrong number of three digit is that number in which the sum of the cubes of its digits is
equal to the number itself.
Example: 371 a 3 digit number, where sum of 3^3 + 7^3+1^3 = 27 + 343 + 1 = 371
Thus an Armstrong number of four digit is that number in which the sum of the fourth power of its digits
is equal to the number itself.
Lab Task 1
You have to find all the positive divisors of every number and count the number of divisors of that
number.
A positive divisor of a number, is a number that divides the number completely and remainder is zero
Example: number is 120
Output should be
Divisors of 120 are 1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 20, 24, 30, 40, 60 and 120
Total number of divisors of 120 are 16
Lab Task 2
You have to find which numbers are Strong numbers
Strong numbers are the numbers whose sum of factorial of digits is equal to the original number
Example: number is 145, so 1! + 4! + 5! = 1 + 24 + 120 = 145
Output should be
Number is 145
Digit is 5 its factorial is 120
Digit is 4 its factorial is 24
Digit is 1 its factorial is 1
Digits Factorial sum is 145, so 145 is a strong number
Lab Task 3
You have to find binary equivalent for every number
Example: number is 145
Output should be
Number is 145 and its binary equivalent is 10010001
[You have to use different already learnt concepts here and form the new number in binary form. You
may use one long long int variable to store the new binary number]