PF Week 5
PF Week 5
This loop runs as long as the condition in the center is true. Note that there is no semi colon after the “for”
statement. If there is only one statement in the “for” loop then the braces may be removed. If we put a
semicolon after the for loop instruction then that loop will not work for any statement.
This loop runs as long as the condition in the parenthesis is true. Note that there is no semicolon after the
“while” statement. If there is only one statement in the “while” loop then the braces may be removed.
This loop runs as long as the condition in the parenthesis is true. Note that there is a semicolon after the
“while” statement. The difference between the “while” and the “do-while” statements is that in the “while”
loop the test condition is evaluated before the loop is executed, while in the “do” loop the test condition is
evaluated after the loop is executed. This implies that statements in a “do” loop are executed at least once.
However, the statements in the “while” loop are not executed if the condition is not satisfied.
#include <iostream>
using namespace std;
int main()
{
return 0;
}
#include <iostream>
using namespace std;
int main()
{
return 0;
}
#include <iostream>
using namespace std;
int main()
{
return 0;
}
#include <iostream>
using namespace std;
int main()
{
return 0;
}
#include <iostream>
using namespace std;
int main() {
int k, num;
cout << "Enter Table Number" ; cin >> num;
for (k = 1 ; k <= 10 ; k++ )
cout << num << " * " << k << " = " << num*k << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int m, k = 0;
for ( ; k <= 10 ; )
{
cout << "Enter Marks" ; cin >> m;
cout << "Marks = " << m;
k++;
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
const int N = 5;
int i,j;
for (i=1; i<=N; i++)
{
for (j=1; j<=i; j++)
cout << "* ";
cout << endl;
}
return 0;
}
#include <iostream>
#include <cmath>
#include<conio.h>
using namespace std;
int main() {
char ch = ‘y’;
double number, answer;
while (ch == ‘y’)
{
cout << "Enter a number: ";
cin >> number;
answer = sqrt(number);
cout << "Square root is "<< answer << endl;
cout << "Do you want the square root of another number?" << endl;
cout << "Press y for yes... " << endl << endl;
ch = getche();
}
return 0;
}
#include <iostream>
#include <cmath>
#include<conio.h>
using namespace std;
int main() {
char ch;
double number, answer;
do
{
cout << "Enter a number: ";
cin >> number;
answer = sqrt(number);
cout << "Square root is "<< answer << endl;
cout << "Do you want the square root of another number?" << endl;
cout << "Press y for yes... " << endl << endl;
ch = getche();
}
while (ch == ‘y’);
return 0;
}
Exercise 1:
Write a program that prints the values by using for loop
1, 7, 49, 343, 2401, 16807, 117649, 823543, 5764801, 40353607
Exercise 2:
Write a program to calculate the sum of digits of a given number. Ask the user to enter a number.
Enter a number: 12345
The sum of digits of 12345 is 15.
Use a while loop to extract digits from number until number becomes 0. In loop use number % 10 to
get and save the last digit of the number (For example 12345 % 10, which equals 5). Then, we remove
the last digit from the number by dividing num by 10 (For example by performing integer division
12345 / 10 which equals 1234). Now, we repeat the process until number becomes 0, extracting each
digit one by one.
Exercise 3:
Write a program to find the factorial of a number.
The factorial function (symbol: !) says to multiply all whole numbers from our chosen number down
to 1.
Examples:
4! = 4 × 3 × 2 × 1 = 24
7! = 7 × 6 × 5 × 4 × 3 × 2 × 1 = 5040
1! = 1
Exercise 4:
Write a program which takes a 3 numbers from users
1. Table number
2. Starting values of table
3. Ending value of table
Then it displays the table of the given number from starting value till ending value. For Example:
Enter Table Number: 7
Enter Starting Value: 6
Enter Ending Value: 12
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
7 x 11 = 77
7 x 12 = 84