Loops in C++
Loops in C++
Example –
for (int i = 1; i <= 5; i++) {
cout << "Itera on: " << i << endl;
}
Example –
int num;
do {
cout << "Enter a posi ve number: ";
cin >> num;
} while (num <= 0);
Assignment
1. Write a Program to find a factorial of a number. (n! =1*2*3…….*n)
2. Write a program to print the reverse of a number.
E.g. - Input: 1234 Output: 4321
3. Write a Program to find the sum of all digits of a number.
E.g. - Input: 1532 Output: 1+5+3+2 = 11
4. Write a program to check whether a number is a Armstrong or Not.
Armstrong Numbers are those numbers which are equal to the sum
of cube of their digits. E.g. - 153 = 1^3 + 5^3 + 3^3
5. Chef will have N guests in his house today. He wants to serve at least
one dish to each of the N guests. Chef can make two types of dishes. He
needs one fruit and one vegetable to make the first type of dish and one
vegetable and one fish to make the second type of dish. Now Chef
has A fruits, B vegetables, and C fishes in his house. Can he prepare at
least N dishes in total?
E.g.: 1. Input: N=4, A=2, B=6, C=3 Output: YES
2. Input: N=3, A=1, B=3, C=1 Output: NO
6. Write a C++ program to check whether a given number is a 'Perfect'
number or not. (A number is called perfect if the number is equal to
sum of its divisors)
E.g. – Input: 6 Output: Perfect Number (6 = 1+2+3)
7. Write a program in C++ to find the prime numbers within a range of
Numbers.
E.g. – Input: S = 1, E = 10 Output: 2 3 5 7
8. Write a C program to find the HCF (Highest Common Factor) of two
Numbers.
E.g. - Input: 24 28 Output: 4
9. Petya loves lucky numbers. We all know that lucky numbers are the
posi ve integers whose decimal representa ons contain only the lucky
digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467
are not. Unfortunately, not all numbers are lucky. Petya calls a number
nearly lucky if the number of lucky digits in it is a lucky number. He
wonders whether number n is a nearly lucky number. You are a given
a number, you have to print Yes if it is nearly lucky, else No.
E.g. – Input: 40047 Output: No
Input: 7747774 Output: Yes
10. You are given two posi ve integers a and b. In one move you can
increase a by 1 (replace a with a+1). Your task is to find the minimum
number of moves you need to do in order to make a divisible by b. It is
possible, that you have to make 0 moves, as a is already divisible by b.
E.g. – Input: 10 4 Output: 2
Input: 15 16 Output: 1
11. Write programs to print the following pa erns:
(i) 1 (ii) A
12 AB
123 ABC
1234 ABCD
12345 ABCDE
(iii) ***** (iv) *
**** ***
*** ******
** ********
* **********
(v) 2 3 4 5 (vi) 1 0 1 0
3456 0101
4567 1010
5678 0101
(vii)
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
(viii)
* *
** **
*** ***
**** ****
***** *****
************
************
***** *****
**** ****
*** ***
** **
* *