You are on page 1of 22

Manual # 3

Topic:
Iteration structures
&
Recursion

Subject:
Introduction to Computing

Author:
Sunawar Khan Ahsan
MS(Computer Vision)
Author2:
Mehwish Shabbir
For Loop
------------------------------------------------------------------------------------------------------------
Note: Please don’t read this manual just for reading purpose, read these manuals
until it make some sense.
------------------------------------------------------------------------------------------------------------

#include<iostream.h>
int main()
{
float g;
g=3/2;
cout<<g;
cout<<"\n\n";
return 0;
}

Output of this program would be 1 not 0.5, this is because of the reason that you declared
g as float and you are doing the division in integers like (3/2) so for right calculation one
of the digit should be float like 3.0/2 to make them float.

But, if you will use a float variable with integer variable and stores its information in
float variable, then it will work alright as in calculation one of the variable i.e. f is float.

#include<iostream.h>
int main()
{
int i;
float f,j;
i=7;
f=2;
j=f/i;
cout<<j;
cout<<"\n\n";
return 0;
}

Similarly, you can see the behavior of program by changing the data types of variable
like see the output using this configuration int i,f; float j, & also int i,j & float f.
------------------------------------------------------------------------------------------------------------
This program will display the numbers from 1 to 10 using for loop.

#include<iostream.h>
int main()
{
int i;
for(i=0;i<=10;i++)
{
cout<<i<<endl;
}
cout<<"\n\n";
return 0;
}
------------------------------------------------------------------------------------------------------------
This program will display the numbers from 10 to 1 using for loop.

#include<iostream.h>
int main()
{
int i;
for(i=10;i>0;i--)
{
cout<<i<<endl;
}
cout<<"\n\n";
return 0;
}
------------------------------------------------------------------------------------------------------------
Write a program which will take the number from the user and then display the sequence
of number from 0 to the number entered by the user.

#include<iostream.h>
int main()
{
int i,j;
cout<<"Enter the Number: ";
cin>>j;
for(i=0;i<=j;i++)
{
cout<<i<<endl;
}
cout<<"\n\n";
return 0;
}
------------------------------------------------------------------------------------------------------------
Extra Command: I am now introducing you with the command that you’ll not find in any
of your text book, it is basically used in professional programming, but I think that this
command is very helpful in understanding of these loops and other programs so it is
better if you get familiar with this command.

You have to include the header files: #include<stdlib.h>


And command is: system(“pause”);

Now it is clear from the name of the command it is used for the pause purpose. So, you
can pause your program at any time.
See the behavior of the following code with the use of this command.

#include<iostream.h>
#include<stdlib.h> //you have to include this header file for pause command.

int main()
{
int i;
for(i=0;i<=10;i++)
{
cout<<i<<endl;
system(“pause”); // At this stage I want to pause my program
}

cout<<"\n\n";
return 0;
}
------------------------------------------------------------------------------------------------------------
#include<iostream.h>
#include<stdlib.h> //you have to include this header file for pause command.

int main()
{
int i=0;
for(;++i<10;)
{
cout<<i<<endl;
system("pause"); // At this stage I want to pause my program
}
cout<<"\n\n";
return 0;
}
------------------------------------------------------------------------------------------------------------
Write a program which will find the factorial of any Non-Negative Integer.
Formula of Factorial:
Factorial of ‘n’ be: n!= n x (n-1) x (n-2) x (n-3) x …….. x (n-(n-1))
= n x (n-1) x (n-2) x (n-3) x …….. x 1
Factorial of ‘7’ be: 7!= 7 x (7-1) x (7-2) x (7-3) x (7-4) x (7-5) x (7-6)
= 7x6x5x4x3x2x1
= 5040

Program:

#include <iostream.h>
int main()
{
int x,i,f;
f=1;
cout<<"Enter the Non-Negative No to find its Factorial: ";
cin>>x;
for(i=1;i<=x;i++)
{
f=f*i;
}
cout<<"The factorial of No: "<<x<<" = "<<f;
cout<<"\n\n";
return 0;
}
------------------------------------------------------------------------------------------------------------
#include <iostream.h>
int main()
{
int count,total;
for (count=0, total=0; count<10; count++) {
total = total + count;
cout<<”Count=”<<count<<”Total=”<<total;
}
return 0;
}
Output of this program is
Count= 0 Total=0
Count= 1 Total=1
Count= 2 Total=3
Count= 3 Total=6
Count= 4 Total=10
Count= 5 Total=15
Count= 6 Total=21
Count= 7 Total=28
Count= 8 Total=36
Count= 9 Total=45
Nested For Loops + Patterns Programming
-----------------------------------------------------------------------------------------------------------
Note the behavior of the program.
Nested For Loop: No Effect of major for loop’s variable ‘i’ on secondary for loop. (i.e.
for(j=0;j<=10;j++ ))

#include<iostream.h>

int main()
{
int i,j;

for(i=0;i<=10;i++)
{
for(j=0;j<=10;j++)
{
cout<<j;
}

cout<<"\n";
}

cout<<"\n\n";

return 0;
}
------------------------------------------------------------------------------------------------------------
Nested For Loop: Effect of major for loop’s variable ‘i’ on initialization of secondary for
loop. (i.e. for(j=i+1;j<=10;j++ ))

#include<iostream.h>

int main()
{
int i,j;

for(i=0;i<=10;i++)
{
cout<<i;

for(j=i+1;j<=10;j++)
{
cout<<j;
}

cout<<"\n";
}

cout<<"\n\n";

return 0;
}

The above program can also be written as:

#include<iostream.h>

int main()
{
int i,j;

for(i=0;i<=10;i++)
{
for(j=i;j<=10;j++)
{
cout<<j;
}

cout<<"\n";
}

cout<<"\n\n";

return 0;
}
------------------------------------------------------------------------------------------------------------
Nested For Loop: Effect of major for loop’s variable ‘i’ on condition of secondary for
loop. (i.e. for(j=0;j<=i;j++ ))

#include<iostream.h>

int main()
{
int i,j;

for(i=0;i<=10;i++)
{
for(j=0;j<=i;j++)
{
cout<<j;
}

cout<<"\n";
}

cout<<"\n\n";

return 0;
}
------------------------------------------------------------------------------------------------------------
Nested For Loop: Effect of major for loop’s variable ‘i’ on initialization & condition of
secondary for loop. (i.e. for(j=i;j<=i;j++ ))

#include<iostream.h>

int main()
{
int i,j;

for(i=0;i<=10;i++)
{
for(j=i;j<=i;j++)
{
cout<<j;
}

cout<<"\n";
}

cout<<"\n\n";

return 0;
}
------------------------------------------------------------------------------------------------------------
Nested For Loop: Effect of major secondary loop’s variable ‘j’ on initialization of
secondary for loop. (i.e. for(i=j;j<=10;i++ ))

#include<iostream.h>

int main()
{
int i,j;

for(i=j;i<=10;i++)
{
for(j=i;j<=i;j++)
{
cout<<j;
}

cout<<"\n";
}

cout<<"\n\n";

return 0;
}

Note: You will see the garbage on the output. It is because of the reason that you did not
assign any value to j and you make i=j, which means that you are assigning the value j to
I, but you did not assign any value to j, so that’s why this kind of programming will not
work.
------------------------------------------------------------------------------------------------------------
Write a program which will create the output as show below:

#include <iostream.h>
#include<math.h>

int main()
{
int i,j;

for(i=0;i<8;i++)
{
for(j=0;j<=i;j++)
{
cout<<"*";
}
cout<<"\n";
}

cout<<"\n\n";

return 0;
}
While Loop, Do-While Loop& Nested loops
------------------------------------------------------------------------------------------------------------
In a For Loop, you can initialize multiple variables and multiple conditions like:

#include<iostream.h>

int main()
{

for(int i=0, int j=5; i>=0 && j<=8;i++,j++)


{
cout<<i<<" "<<j<<"\n";
}
cout<<"\n\n";

return 0;
}

Note the output of the following program:

#include<iostream.h>

int main()
{

for(int i=0, int j=5; i>=0 && j<=8;i++)


{
cout<<i<<" "<<j<<"\n";
}
cout<<"\n\n";

return 0;
}
------------------------------------------------------------------------------------------------------------

Write a program which will create the sequence of numbers from 1 to 26.
Note the output of the following program:

Without Int & Inc With Int & Without Inc With Int & Inc
#include<iostream.h> #include<iostream.h> #include<iostream.h>
int main() int main() int main()
{ { {
int i; int i; int i;
while(i<=26) i=0; i=0;
{ while(i<=26) while(i<=26)
cout<<i<<" "; { {
} cout<<i<<" "; cout<<i<<" ";
cout<<"\n\n"; } i++;
return 0; cout<<"\n\n"; }
} return 0; cout<<"\n\n";
} return 0;
}

Program can also be initialized as:

#include<iostream.h>
int main()
{
int i=0;
while(i<=26)
{
cout<<i<<" ";
i++;
}
cout<<"\n\n";
return 0;
}
------------------------------------------------------------------------------------------------------------
Write a program which will display the English language letters in both capital & small
letters.

#include<iostream.h>
int main()
{
int i;
char a;
a='A';
i=0;
while(i<26)
{
cout<<a<<" ";
a++;
i++;
}
cout<<"\n";
a='a';
i=0;
while(i<26)
{
cout<<a<<" ";
a++;
i++;
}
cout<<"\n\n";
return 0;
}
------------------------------------------------------------------------------------------------------------
Write a program which will fill the entire screen with a smiling face. The smiling face
has an ASCII value 1.

#include<iostream.h>
int main()
{
char a;
a=1;
while(1)
{
cout<<a;
}

return 0;
}
------------------------------------------------------------------------------------------------------------
Programming Exercise
------------------------------------------------------------------------------------------------------------
Q1: Write a program that estimates the value of the mathematical constant e by using the
formula:
e = 1 + 1 + 1 + 1 + 1 + ……..
1! 2! 3! 4!
Prompt the user for the desired accuracy of e (i.e. the number of terms in the summation).
------------------------------------------------------------------------------------------------------------
x
Q1: Write a program that computes the value of e (i.e. e^x) by using the formula:
x
e = 1 + x + x^2 + x^3 + ……..
1! 2! 3!
Prompt the user for the desired accuracy of e (i.e. the number of terms in the summation).
------------------------------------------------------------------------------------------------------------
Q2: Write a program to find out the arithmetic mean on the following basis:
Display the Message “How many Number for A.M” and then user enter numbers
Enter the list of real numbers
Compute the arithmetic mean and then print result on the screen
------------------------------------------------------------------------------------------------------------
Q3: Create the Following Output. Revised Guessing Game
Type in a Character from a to e:
B
Sorry, b Is Incorrect.
Try Again
C
That’s Gooood
Play Again. (Enter ‘n’ for No and ‘y’ for Yes): n
Thank You for Playing
Byeeeeeeeeee!
------------------------------------------------------------------------------------------------------------
Q4: Write a complete C program using while loop to compute total course fees for
students.
1 Prompt the user to enter their name (string), id no (string) and duration of study
(integer).
2 Suppose that the course fee is starting from RS10k in the first year and increases
5% the following years.
3 Calculate the annual fee and total course fees for the whole duration of study.
4 Display all information as shown below.

------------------------------------------------------------------------------------------------------------
Q5: Write programs to find the sum of the following series:
i) 1+1/3+1/5…………1/99
ii) 1+1/2+1/4+…….1/100
iii) 1+1/4+1/8+…….1/100
iv) 1/2+2/3+3/4+…….99/100
v) 1/1!+2/2!+3/3!………….n
vi) 1/3!+5/4!+9/5!………….n
vii) 12 + 22 + 32+…………………..n2
viii) 1/x!+1/x^2+1/x^3………….n
ix) 1+3+5+7+…………………………….10
x) 2+4+6+8+10
xi) x+x1+x2+x3+……..xN
------------------------------------------------------------------------------------------------------------
Q6: Write a program to print the following sequences:-
i) 64 32 16 8 4 2
ii) 1 3 9 27 81………n
iii) 8 12 17 24 28 33 …………n
iv) 1 -4 7 -10……………………..40
v) 1 4 7 10 ………………………..40
-----------------------------------------------------------------------------------------------------------
Q7: Write a program to find out the factorial of an integer.
Hint: - Factorial of 5 is 5*4*3*2*1
------------------------------------------------------------------------------------------------------------
Q8: Write a program to create following sequence:-
1 1 1 1
2 16 2 8
3 81 3 27
4 256 4 64
5 625 5 125
6 1296 6 216
7 2401 7 343
8 4096 8 512
9 6561 9 729
10 1000

------------------------------------------------------------------------------------------------------------
Q9: Write a program for following output:-
Enter First Number, operator, second number: 10/3
Answer = 3.3333333
Do You Want To Try It Again(y/n)? y
Enter First Number, operator, second number: 12/3
Answer = 4
Do You Want To Try It Again(y/n)? n
------------------------------------------------------------------------------------------------------------
Q10:Write a program for following series. Number of steps inputs ask form user.
SUM =12-32+52-72+.....
------------------------------------------------------------------------------------------------------------
Q11: One large chemical company pays its salesperson on commissions basis. The
salespeople each receive $200 per week plus 9% of their gross sales for that week. For
example, a salesperson who sells $5000 worth of chemical in a week receive $200 plus
9% of his salary, or a total of $650.
Write a program that use any iteration statement to input each salespersons gross sales for
last week and calculates and displays the salesperson earning.
Notes:- Program will quit when user enter -1 as input.
------------------------------------------------------------------------------------------------------------
Q12: Write a program to generate the following series:-
1, 5, 25, 125, 625…………n
------------------------------------------------------------------------------------------------------------
Nested While-Loops:
I want to display the sequence of numbers from 0 to 10 ten times:
Note the output of the programme:
#include<iostream.h>
int main()
{
int i,j;
i=0;
j=0;
while(i<=10)
{
while(j<=10)
{
cout<<j<<" ";
j++;
}
i++;
cout<<"\n";
}
cout<<"\n\n";
return 0;
}
Error occurs in initialization.
Note the following program:
#include<iostream.h>

int main()
{
int i,j;
i=0;
while(i<=10)
{
j=0;
while(j<=10)
{
cout<<j<<" ";
j++;
}
i++;
cout<<"\n";
} cout<<"\n\n";
return 0;
}
------------------------------------------------------------------------------------------------------------
Q13: Write a program on the following basis
Create a selection menu as shown below.
Ask user to enter first and last alphabet as well as their sort selection.
Refer to following conditions:
If user select ‘A’: do ascending sort
If user selects‘B’: do descending sort
If user selects others: display “Sorry. Invalid selection”.
Display all information as follows.
------------------------------------------------------------------------------------------------------------
Q14: Write a program in which the user gives the four values of ages. The program
should be schemed in such a way that it will take the three ranges, first range b/w first
and second value, second range b/w second and third value, third range b/w third and
fourth value.
Now consider the table below according to which the program takes the first
range and then tells you that the first range has this much sequence of ages in the
following category. And then pick the second range and so on.

Range of Age Category


0-15 Teenager
16-32 Young
33-55 Mid-Age
56 to onwards Old-Age
------------------------------------------------------------------------------------------------------------
Q15: Write a program to generate following series:-
A C E G I…… character
----------------------------------------------------------------------------------------------------------
Q1: Write a program in which a user can enter the range of values. Now, the scheme of
the program should be like that first the program will display all the prime numbers
present in the sequence and after that sequence the program should display separate even
and odd numbers in that sequence.
------------------------------------------------------------------------------------------------------------
Q16: According to a study, the approximate level of intelligence of a person can be
calculated using the following formula:
i = 2 + (y + 0.5x)
Write a program, which will produce a table of values of i, y and x, where y varies from 1
to 6, and for each value of y, x varies from 5.5 to 12.5 in steps of 0.5 .
------------------------------------------------------------------------------------------------------------
Q17: Write a Program to find H.C.F or G.C.D.
Hint: The largest integer which is perfectly divisible to two or more numbers is
known as H.C.F or G.C.D of those two numbers.
------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------
Q18: Write a Program to find L.C.M.
Hint: L.C.M of two integer a and b is the lowest positive integer that is divisible
by both a and b
------------------------------------------------------------------------------------------------------------
Q19: Write a program that take an integer from the user and find its reverse.
------------------------------------------------------------------------------------------------------------
Q20:Write C program to Check Armstrong Number.
For example:
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.
12 is not equal to 1*1*1+2*2*2 // 12 is not an Armstrong number.
Sample Output
Enter a positive integer: 371
371 is an Armstrong number.
------------------------------------------------------------------------------------------------------------
Q21: Write a program to find power given number. Both parameters ask form from the
user.
------------------------------------------------------------------------------------------------------------
Q22: Write a program to calculate sum and product of Natural Numbers.
------------------------------------------------------------------------------------------------------------
Q23: Write a program to print all even and odd number with upper and lower limit.
------------------------------------------------------------------------------------------------------------
Q24: Write a program to print all ASCII Values with Characters.
------------------------------------------------------------------------------------------------------------
Q25: Write code to print a table of a given number within the upper and lower limit with
asterisk.
Output:
Enter Table Number 2
Enter Where You Want to Start 3
Enter Ending Value 5
2 * 3 = 6 ******
2 * 4 = 8 ********
2 * 5 = 10 **********
------------------------------------------------------------------------------------------------------------
Q26: Write a program to input five values and then compute average. Display result on
screen.
------------------------------------------------------------------------------------------------------------
Q27: Write a program that input the sums of integers assuming that first integer read
specifies the number of values remaining to be entered. The program should read one
value per input statement. A typical input sequence might be 5,100,200,150,300,and 500.
Integer 5 indicates that five values are to be summed.
------------------------------------------------------------------------------------------------------------
Q28: Write a program to break an integral value and find its product and sum. For
example user enters 5689. Output will be calculated as 5+6+8+9, 5*6*8*9
------------------------------------------------------------------------------------------------------------
Q29: Write a program to find out Weighted Average.
------------------------------------------------------------------------------------------------------------
Q30: Write a program to count the values in integer.
------------------------------------------------------------------------------------------------------------
Q31: Write a program that input a number. It then displays the sum of all odd numbers
and the sum of all even numbers from 1 to the number entered by the user.
------------------------------------------------------------------------------------------------------------
Q32: A person invests 10000 in a saving account yielding 5% interest. Assuming all
interest is left deposit in account, calculate and print the amount of money in the accounts
at eh end of each year for ten year.
Hint: - a=p (1 +r) n
p= Original Amount Invested
r= Rate of Interest
n= Number of Years
a= Amount on deposit at the end of nth Year
------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------
Q33: Write a program that could find whether the number entered through keyboard is
odd or even and should also tell that whether the number is prime or not. The program
should keep on taking the value till the user ends and before termination should find the
total number of odd, evens and prime entered.
------------------------------------------------------------------------------------------------------------
Q34: Write a program that input starting and ending numbers and display all prime
number ending with digit 7 between the given ranges in descending order.
------------------------------------------------------------------------------------------------------------
Q35: Write a program that displays all prime number between 100 and 500 that are also
palindrome.
------------------------------------------------------------------------------------------------------------
Q36: Write Program to Check Whether a Number is Palindrome or Not.
Sample Output
Enter an integer: 12321
12321 is a palindrome.
------------------------------------------------------------------------------------------------------------
Q37: Write a program to generate following:-
13 4
69 2
347 3
------------------------------------------------------------------------------------------------------------
Q38: Write a program to generate all possible combinations of 1, 2, 3 and 4.
------------------------------------------------------------------------------------------------------------
Q39: Write a program that inputs number until user enter negative number. The program
calculates the average, maximum and minimum of all positive numbers.
------------------------------------------------------------------------------------------------------------
Q40: Write a program that inputs a sequence from the user and count the character and
words in the sentence.
------------------------------------------------------------------------------------------------------------
Q41: Write a program that input a number from the user and find whether it is Fibonacci
number or not.
------------------------------------------------------------------------------------------------------------
Q42: Write a program that input 2 number and display highest and lowest number.
------------------------------------------------------------------------------------------------------------
Q43: Write a program that inputs number from the user. It the number is greater than 0, it
is displayed and the next number is input. The program exits the loop if the number is
Zero or negative.
------------------------------------------------------------------------------------------------------------
Q44: Write a program to display alphabets from A to Z using for loop.
------------------------------------------------------------------------------------------------------------
Q45: Write a program that will ask the user a question with four possible answers. The
question should be asked 20 times. After all the inputs is gathered the program should
output the number of times each answer was selected.
------------------------------------------------------------------------------------------------------------
Q46: Write a program that input a number from the user and displays all Armstrong
number up to that number.
------------------------------------------------------------------------------------------------------------
Q47: Write a program that input a number from the user and displays all Perfect number
up to entered number.
------------------------------------------------------------------------------------------------------------
Q48: Write a program that input number of student in class. It then input marks of these
students and print First Highest, Second Highest and Third Highest marks.
------------------------------------------------------------------------------------------------------------
Q49: Write a program that input n Number and print how many numbers are +ve and –
Ve.
------------------------------------------------------------------------------------------------------------
Q50: Write a program to generate Fibonacci series until given number.
1, 1, 2, 3, 5, 8, 13, 21…….
------------------------------------------------------------------------------------------------------------
Q51: Write a program to display series 2,4,16,256... using while loop.
------------------------------------------------------------------------------------------------------------
Patterns

Write Program to create Following Patterns


Enter Number:- 5 Enter Number of Rows:- 5
***** *
***** **
52 ***** 53 ***
***** ****
***** *****
Enter Number of Rows:- 5 Enter Number of Rows:- 5
***** *
**** **
54 *** 55 ***
** ****
* *****
Enter Number of Rows:- 5 Enter Number of Rows:- 5
***** *
**** **
56 *** 57
***
** ****
* *****
Enter Number of Rows:- 5 Enter Number of Rows:- 5
*
**
* * * * * ***
**** ****
58 *** 59 *****
** ****
* ***
**
*
Enter Number of Rows:- 5 Enter Number of Rows:- 5
* *
** ***
*** *****
**** *******
60 ***** 61 *********
**** *******
*** *****
** ***
* *
Enter Rows:- 10 Enter Rows:- 10
* * * * * * * * ********************
* * ********* *********
* * ******** ********
* * ******* *******
62 * * 63 ****** ******
* * ***** *****
* * **** ****
* * *** ***
* * ** **
* * * * * * * * * *
Enter Number of Rows:- 5
*
* *
* *
* *
64 * * 65
* *
* *
* *
*

66 67

Enter A Number:- 5
********* * 1
* * * 1 2
70 * *** * 71 1 2 3
* ***** * 1 2 3 4
********* * 1 2 3 4 5

Enter A Number:- 5 Enter A Number:- 5


12345 12345
1234 2345
72 123 73 345
12 45
1 5
Enter A Number:- 5 Enter Number of Rows:- 5
5 12345
45 1234
74 345 75 123
2345 12
12345 1
Enter The Number of Rows:5 Enter The Number of Rows:5
1
1 1 2 1
2 3 2 1 2 3 2 1
76 3 4 5 4 3 77 1 2 3 4 5 4 3 2 1
4 5 6 7 6 5 4 1 2 3 2 1
5 6 7 8 9 8 7 6 5 1 2 1
1
*****#*****
****#*#****
***#***#***
78 79 **#*****#**
*#*******#*
#*********#

Enter Upper Class Character:- F Enter The Number of Rows: 6


A A A A A AA
B B B B B B B
80 C C C 81 C C C C
DD D D D D
E E E E E E
F F F F F F
Enter Number of Rows:- 6 Enter Number of Rows:- 8
A B C D E F
A B C D E 8 6 4 2
82 A B C D 83 8 6 4
A B C 8 6 4
A B 8 6
A 8
Enter Number of Rows:- 10
*
*C* ABCDEFGFEDCBA
*C*C* ABCDEF FEDCBA
*C*C*C* ABCDE EDCBA
84 *C*C*C*C*C* 85 ABCD DCBA
*C*C*C*C*C*C* ABC CBA
*C*C*C*C*C**C*C* AB BA
*C*C*C*C*C*C*C*C* A A
*C*C*C*C*C* C*C*C*C*
*C*C*C*C*C* C*C*C*C*C*
Enter Number of Rows:- 8 Enter Number of Rows:- 8
1 1
2 3 0 1
3 4 5 1 0 1
86 4 5 6 7 87 0 1 0 1
5 6 7 8 9 1 0 1 0 1
6 7 8 9 10 11 0 1 0 1 0 1
7 8 9 10 11 12 13 1 0 1 0 1 0 1
8 9 10 11 12 13 14 15 0 1 0 1 0 1 01
1 0 0 0 0 0 0 0 0 0
1 0 1 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
1 2 0 0 0 1 0 0 0 0 0 0
2 3 4 0 0 0 0 1 0 0 0 0 0
88 89 0 0 0 0 0 1 0 0 0 0
4 5 6 7
0 0 0 0 0 0 1 0 0 0
7 8 9 10 11 0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 1
Enter Number of Rows:- 5 1
1 1 2
2 1 1 2 3
92 3 2 1 93 1 2 3 4
4 3 2 1 1 2 3 4 5
5 4 3 2 1
*
* *
* *
* * 1 2 3 5 4
* * 2 4 6 8 10
94 * * 95 3 6 9 12 15
* * 4 8 12 16 20
* *
* * 5 10 15 20 25
*******************
Enter Number of Rows:- 9
1
2 2
3 3 3 * *
96 4 4 4 4 97 * *
5 5 5 5 5 * *
6 6 6 6 6 6 * *
7 7 7 7 7 7 7 *
8 8 8 8 8 8 8
######
#####
#### *
### * *
## * *
98 # 99 * *
## * *
###
####
#####
######
Su
Sun
Suna 1 1
100 Sunaw 101 12 21
Sunawa 12321
Sunawar

*****6
******* ****656
***S*** ***65456
102 **SSS** 103 **653456
*SSSSS* *654323456
65432123456

10001
ZXYWVWXYZ
01010
ZYXWXYZ
104 00100 105 ZYXYZ
01010 ZYZ
10001

........9 COMPUTER
.......898 OE
......78987 MT
.....6789876 PU
106 ....567898765 107 UP
...45678987654 TM
..3456789876543 EO
.234567898765432 RETUPMOC
12345678987654321
4321234
33 12345
22 16 17 18 19 6
108 11 109 15 24 25 20 7
22 14 23 22 21 8
33 13 12 11 10 9
4321234

************************
**** ***
************************
**** ***
*** **** ***
11111 *** **** ***
10001 ************************
110 10001 111 *********************
11111 ************************
*********************
*** **** ***
*** **** ***
************************
**** ***
************************
**** ***

11
12 21
123 321 ****
1234 4321 *
112 123454321 113 ****
1234 4321 *
123 321 ****
12 21
11

sssssssss ......*
sssssssss ...* A *
114 ssssossss 115 .* A B C *
sssssssss ...* A *
sssssssss ......*

1
1RR2RR3R 4R5 27
16RRRRRRRR6 3 8 12
116 15RRRRRRRR7 117 4 9 13 16
14RRRRRRRR8 5 10 14 17 19
13R12R11R10 9 6 11 15 18 20 21

0*********
*1******** ###1###
**2******* ##2#2##
118 ***3****** 119 #3###3#
****4***** 4#####4
*****5****
program
######1###### rogra
#####121##### ogr
120 ####12321#### 121 g
###1234321### ogr
##123454321## rogra
program

55
12345 - 12345
25 7
_1234 - 1234
122 125 8 123 __123 - 123
625 13
___12 - 12
3125 11
____1 – 1
15625 19

12345678910 1
13579 333
14710 55555
159 7777777
124 16 124 999999999
17 7777777
18 55555
19 333
110 1
1

C
7777777 7777777 OO
666666 666666 MMM
55555 55555 PPPP
125 4444 4444 126 UUUUU
333 333 TTTTTT
22 22 EEEEEEE
11 RRRRRRRR

1234554321
----* 1234---4321
---*-* 123------321
--*---* 12---------21
127 -*-----* 128 1------------1
--*---* 12---------21
---*-* 123------321
----* 1234---4321
1234554321

*___________*
___*_______*_ 1
_____*___*_ 212
129 _______*____ 130 32123
_____*___*__ 4321234
___*_______* 543212345
*____________*
1 2 3 4 5 6 7 8 9 10
36 37 38 39 40 41 42 43 44 11
35 64 65 66 67 68 69 70 45 12
*^*^*^*^*^*^ 34 63 84 85 86 87 88 71 46 13
*^*^*^*^*^ 33 62 83 96 97 98 89 72 47 14
131 *^*^*^*^ 132 32 61 82 95 100 99 90 73 48 15
*^*^*^ 31 60 81 94 93 92 91 74 49 16
*^*^ 30 59 80 79 78 77 76 75 50 17
29 58 57 56 55 54 53 52 51 18
28 27 26 25 24 23 22 21 20 19

4444444
5432* 4333334
543*1 4322234
133 54*21 134 4321234
5*321 4322234
*4321 4333334
4444444

* - *********** * *
*** - ********* ** **
***** - ******* *** ***
135 ******* - ***** 136 *******
********* - *** *** ***
*********** - * ** **
* *

*
**
***___*** ***
***___*** ****
********* *****
137 ********* 138 ******
*****
***___*** ****
***___*** ***
**
*

* *
* * * * * * * *
* *
* * * * * * * * * *
* * * *
* * * *
* *** ** *********
* **** *
* * * *
140
* **** *
139 * *** ** ********* &
* * * * 141
* * * *
* * * *
* * * *
* * * *
* * * * * * * * * *
*
* * * * * * * * * *
Case Study:-
Suppose you give a dinner party for six guests, but your table seats
only four. In how many can four of the six guests arrange themselves at the
table? Any of the six guests can sit in the first chair, and any of the
remaining three can sit in the fourth chair. (The last two will have to stand).
So the number of possible arrangements of sic guests in four chairs is
6*5*4*3, which is 360.
Write a program that calculates the number of possible arrangements
for any number of guests and any number of chairs.
Assume there will never be fever guests than chairs. Don’t let this get
too complicated. A simple for loop should do it.

-----------------------------------------------------------------------------------------------------------
Sunawar Khan Ahsan
For solution contact me at 03334892200
Mail: sk_ahsan38@yahoo.com
-----------------------------------------------------------------------------------------------------------

S.K. Ahasn

You might also like