0% found this document useful (0 votes)
12 views6 pages

Loops in C++

The document explains three types of loops in programming: for loops, while loops, and do-while loops, detailing their syntax and providing examples. It also includes a series of programming assignments that cover various concepts, such as calculating factorials, reversing numbers, checking for perfect numbers, and identifying prime numbers. Additionally, it presents tasks involving pattern printing and number properties, such as lucky and nearly lucky numbers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views6 pages

Loops in C++

The document explains three types of loops in programming: for loops, while loops, and do-while loops, detailing their syntax and providing examples. It also includes a series of programming assignments that cover various concepts, such as calculating factorials, reversing numbers, checking for perfect numbers, and identifying prime numbers. Additionally, it presents tasks involving pattern printing and number properties, such as lucky and nearly lucky numbers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

For Loop - It is used when you know the number of itera ons in

advance. It consists of an ini aliza on, a condi on, and an


increment/decrement statement.
Syntax –
for (ini aliza on; condi on; increment/decrement) {
// code to be executed
}

Example –
for (int i = 1; i <= 5; i++) {
cout << "Itera on: " << i << endl;
}

While Loop - It is used when the number of itera ons is not


known beforehand and depends on a condi on. It executes the
code block as long as the condi on is true.
Syntax –
while (condi on) {
// code to be executed
}
Example –
int count = 0;
while (count < 3) {
cout << "Count: " << count << endl;
count++;
}

Do While Loop - It is similar to the while loop, but the condi on


is checked at the end of the loop. This guarantees that the code
block is executed at least once.
Syntax –
do {
// code to be executed
} while (condi on);

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)
* *
** **
*** ***
**** ****
***** *****
************
************
***** *****
**** ****
*** ***
** **
* *

You might also like