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<<"nn";
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<<"nn";
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<<"nn";
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<<"nn";
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<<"nn";
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<<"nn";
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<<"nn";
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)
= 7 x 6 x 5 x 4 x 3 x 2 x 1
= 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<<"nn";
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<<"nn";
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<<"nn";
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<<"nn";
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<<"nn";
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<<"nn";
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<<"nn";
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<<"nn";
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<<"nn";
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<<"nn";
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>
int main()
{
int i;
while(i<=26)
{
cout<<i<<" ";
}
cout<<"nn";
return 0;
}
#include<iostream.h>
int main()
{
int i;
i=0;
while(i<=26)
{
cout<<i<<" ";
}
cout<<"nn";
return 0;
}
#include<iostream.h>
int main()
{
int i;
i=0;
while(i<=26)
{
cout<<i<<" ";
i++;
}
cout<<"nn";
return 0;
}
Program can also be initialized as:
#include<iostream.h>
int main()
{
int i=0;
while(i<=26)
{
cout<<i<<" ";
i++;
}
cout<<"nn";
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<<"nn";
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).
------------------------------------------------------------------------------------------------------------
Q1: Write a program that computes the value of e x
(i.e. e^x) by using the formula:
e x
= 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
2 16
3 81
4 256
5 625
6 1296
7 2401
8 4096
9 6561
1 1
2 8
3 27
4 64
5 125
6 216
7 343
8 512
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<<"nn";
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<<"nn";
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.
------------------------------------------------------------------------------------------------------------
P a t t e r n s
Write Program to create Following Patterns
52
Enter Number:- 5
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
53
Enter Number of Rows:- 5
*
**
***
****
*****
54
Enter Number of Rows:- 5
*****
****
***
**
*
55
Enter Number of Rows:- 5
*
**
***
****
*****
56
Enter Number of Rows:- 5
*****
****
***
**
*
57
Enter Number of Rows:- 5
*
**
* * *
* * * *
* * * * *
58
Enter Number of Rows:- 5
* * * * *
* * * *
* * *
* *
*
59
Enter Number of Rows:- 5
*
**
***
****
*****
****
***
**
*
60
Enter Number of Rows:- 5
*
**
***
****
*****
****
***
**
*
61
Enter Number of Rows:- 5
*
***
*****
*******
*********
*******
*****
***
*
62
Enter Rows:- 10
* * * * * * * *
* *
* *
* *
* *
* *
* *
* *
* *
* * * * * * * *
63
Enter Rows:- 10
********************
********* *********
******** ********
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
64
Enter Number of Rows:- 5
*
* *
* *
* *
* *
* *
* *
* *
*
65
66 67
70
* * * * * * * * * *
* * *
* * * * *
* * * * * * *
* * * * * * * * * *
71
Enter A Number:- 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
72
Enter A Number:- 5
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
73
Enter A Number:- 5
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
74
Enter A Number:- 5
5
4 5
3 4 5
2 3 4 5
1 2 3 4 5
75
Enter Number of Rows:- 5
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
76
Enter The Number of Rows:5
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5
77
Enter The Number of Rows:5
1
1 2 1
1 2 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 2 1
1 2 1
1
78 79
* * * * * # * * * * *
* * * * # * # * * * *
* * * # * * * # * * *
* * # * * * * * # * *
* # * * * * * * * # *
# * * * * * * * * * #
80
Enter Upper Class Character:- F
A
B B
C C C
D D D
E E E E
F F F F F
81
Enter The Number of Rows: 6
A A A A A A
B B B B B
C C C C
D D D
E E
F
82
Enter Number of Rows:- 6
A B C D E F
A B C D E
A B C D
A B C
A B
A
83
Enter Number of Rows:- 8
8 6 4 2
8 6 4
8 6 4
8 6
8
84
Enter Number of Rows:- 10
*
*C*
*C*C*
*C*C*C*
*C*C*C*C*C*
*C*C*C*C*C*C*
*C*C*C*C*C**C*C*
*C*C*C*C*C*C*C*C*
*C*C*C*C*C* C*C*C*C*
*C*C*C*C*C* C*C*C*C*C*
85
ABCDEFGFEDCBA
ABCDEF FEDCBA
ABCDE EDCBA
ABCD DCBA
ABC CBA
AB BA
A A
86
Enter Number of Rows:- 8
1
2 3
3 4 5
4 5 6 7
5 6 7 8 9
6 7 8 9 10 11
7 8 9 10 11 12 13
8 9 10 11 12 13 14 15
87
Enter Number of Rows:- 8
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1
0 1 0 1 0 1
1 0 1 0 1 0 1
0 1 0 1 0 1 0 1
88
1
1 2
2 3 4
4 5 6 7
7 8 9 10 11
89
1 0 0 0 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 0 0 0 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 0 0 0
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
92
Enter Number of Rows:- 5
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
93
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
94
*
* *
* *
* *
* *
* *
* *
* *
* *
*******************
95
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
96
Enter Number of Rows:- 9
1
2 2
3 3 3
4 4 4 4
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
97
* *
* *
* *
* *
*
98
# # # # # #
# # # # #
# # # #
# # #
# #
#
# #
# # #
# # # #
# # # # #
# # # # # #
99
*
* *
* *
* *
* *
100
Su
Sun
Suna
Sunaw
Sunawa
Sunawar
101
1 1
12 21
12321
102
*******
***S***
**SSS**
*SSSSS*
103
*****6
****656
***65456
**653456
*654323456
65432123456
104
10001
01010
00100
01010
10001
105
ZXYWVWXYZ
ZYXWXYZ
ZYXYZ
ZYZ
106
........9
.......898
......78987
.....6789876
....567898765
...45678987654
..3456789876543
.234567898765432
12345678987654321
107
COMPUTER
O E
M T
P U
U P
T M
E O
RETUPMOC
108
4 3 2 1 2 3 4
3 3
2 2
1 1
2 2
3 3
4 3 2 1 2 3 4
109
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
110
11111
10001
10001
11111
111
************************
**** ***
************************
**** ***
*** **** ***
*** **** ***
************************
*********************
************************
*********************
*** **** ***
*** **** ***
************************
**** ***
************************
**** ***
112
1 1
12 21
123 321
1234 4321
123454321
1234 4321
123 321
12 21
1 1
113
* * * *
*
* * * *
*
* * * *
114
sssssssss
sssssssss
ssssossss
sssssssss
sssssssss
115
......*
...* A *
.* A B C *
...* A *
......*
116
1RR2RR3R 4R5
16RRRRRRRR6
15RRRRRRRR7
14RRRRRRRR8
13R12R11R10 9
117
1
2 7
3 8 12
4 9 13 16
5 10 14 17 19
6 11 15 18 20 21
118
0*********
*1********
**2*******
***3******
****4*****
*****5****
119
###1###
##2#2##
#3###3#
4#####4
120
######1######
#####121#####
####12321####
###1234321###
##123454321##
121
program
rogra
ogr
g
ogr
rogra
program
122
5 5
25 7
125 8
625 13
3125 11
15625 19
123
12345 - 12345
_1234 - 1234
__123 - 123
___12 - 12
____1 – 1
124
12345678910
13579
14710
159
16
17
18
19
110
1
124
1
333
55555
7777777
999999999
7777777
55555
333
1
125
7777777 7777777
666666 666666
55555 55555
4444 4444
333 333
22 22
1 1
126
C
O O
M M M
P P P P
U U U U U
T T T T T T
E E E E E E E
R R R R R R R R
127
----*
---*-*
--*---*
-*-----*
--*---*
---*-*
----*
128
1234554321
1234---4321
123------321
12---------21
1------------1
12---------21
123------321
1234---4321
1234554321
129
*___________*
___*_______*_
_____*___*_
_______*____
_____*___*__
___*_______*
*____________*
130
1
212
32123
4321234
543212345
131
*^*^*^*^*^*^
*^*^*^*^*^
*^*^*^*^
*^*^*^
*^*^
132
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
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
133
5432*
543*1
54*21
5*321
*4321
134
4444444
4333334
4322234
4321234
4322234
4333334
4444444
135
* - ***********
*** - *********
***** - *******
******* - *****
********* - ***
*********** - *
136
* *
** **
*** ***
*******
*** ***
** **
* *
137
***___***
***___***
*********
*********
***___***
***___***
138
*
**
***
****
*****
******
*****
****
***
**
*
139
* * * * * * * * * *
* *
* * * * * * * * * *
* * * *
* * * *
* * * * * * * * * * * * * * *
* * * * * *
* * * *
* * * * * *
* * * * * * * * * * * * * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * * * * * * * *
*
* * * * * * * * * *
140& 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.
-----------------------------------------------------------------------------------------------------------
S una w a r K ha n Ahs a n
For solution contact me at 03334892200
Mail: sk_ahsan38@yahoo.com
-----------------------------------------------------------------------------------------------------------
S.K. Ahasn

Loops

  • 1.
    Manual # 3 Topic: Iterationstructures & Recursion Subject: Introduction to Computing Author: Sunawar Khan Ahsan MS(Computer Vision) Author2: Mehwish Shabbir
  • 2.
    For Loop ------------------------------------------------------------------------------------------------------------ Note: Pleasedon’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<<"nn"; 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<<"nn"; 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<<"nn"; 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;
  • 3.
    } cout<<"nn"; return 0; } ------------------------------------------------------------------------------------------------------------ Write aprogram 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<<"nn"; 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<<"nn"; 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<<"nn";
  • 4.
    return 0; } ------------------------------------------------------------------------------------------------------------ Write aprogram 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) = 7 x 6 x 5 x 4 x 3 x 2 x 1 = 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<<"nn"; 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
  • 5.
    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<<"nn"; 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<<"nn"; return 0; } The above program can also be written as: #include<iostream.h> int main() { int i,j; for(i=0;i<=10;i++)
  • 6.
    { for(j=i;j<=10;j++) { cout<<j; } cout<<"n"; } cout<<"nn"; return 0; } ------------------------------------------------------------------------------------------------------------ Nested ForLoop: 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<<"nn"; 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<<"nn"; return 0; } ------------------------------------------------------------------------------------------------------------
  • 7.
    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<<"nn"; 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<<"nn"; return 0; }
  • 8.
    While Loop, Do-WhileLoop& 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<<"nn"; 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<<"nn"; 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> int main() { int i; while(i<=26) { cout<<i<<" "; } cout<<"nn"; return 0; } #include<iostream.h> int main() { int i; i=0; while(i<=26) { cout<<i<<" "; } cout<<"nn"; return 0; } #include<iostream.h> int main() { int i; i=0; while(i<=26) { cout<<i<<" "; i++; } cout<<"nn"; return 0; } Program can also be initialized as: #include<iostream.h> int main() { int i=0; while(i<=26) { cout<<i<<" ";
  • 9.
    i++; } cout<<"nn"; return 0; } ------------------------------------------------------------------------------------------------------------ Write aprogram 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<<"nn"; 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; } ------------------------------------------------------------------------------------------------------------
  • 10.
    Programming Exercise ------------------------------------------------------------------------------------------------------------ Q1: Writea 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). ------------------------------------------------------------------------------------------------------------ Q1: Write a program that computes the value of e x (i.e. e^x) by using the formula: e x = 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 -----------------------------------------------------------------------------------------------------------
  • 11.
    Q7: Write aprogram 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 2 16 3 81 4 256 5 625 6 1296 7 2401 8 4096 9 6561 1 1 2 8 3 27 4 64 5 125 6 216 7 343 8 512 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<<"nn"; return 0; } Error occurs in initialization.
  • 12.
    Note the followingprogram: #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<<"nn"; 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. ------------------------------------------------------------------------------------------------------------
  • 13.
    ------------------------------------------------------------------------------------------------------------ Q18: Write aProgram 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 ------------------------------------------------------------------------------------------------------------
  • 14.
    ------------------------------------------------------------------------------------------------------------ Q33: Write aprogram 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. ------------------------------------------------------------------------------------------------------------
  • 15.
    P a tt e r n s Write Program to create Following Patterns 52 Enter Number:- 5 * * * * * * * * * * * * * * * * * * * * * * * * * 53 Enter Number of Rows:- 5 * ** *** **** ***** 54 Enter Number of Rows:- 5 ***** **** *** ** * 55 Enter Number of Rows:- 5 * ** *** **** ***** 56 Enter Number of Rows:- 5 ***** **** *** ** * 57 Enter Number of Rows:- 5 * ** * * * * * * * * * * * * 58 Enter Number of Rows:- 5 * * * * * * * * * * * * * * * 59 Enter Number of Rows:- 5 * ** *** **** ***** **** *** ** * 60 Enter Number of Rows:- 5 * ** *** **** ***** **** *** ** * 61 Enter Number of Rows:- 5 * *** ***** ******* ********* ******* ***** *** * 62 Enter Rows:- 10 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 63 Enter Rows:- 10 ******************** ********* ********* ******** ******** ******* ******* ****** ****** ***** ***** **** **** *** *** ** ** * *
  • 16.
    64 Enter Number ofRows:- 5 * * * * * * * * * * * * * * * * 65 66 67 70 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 71 Enter A Number:- 5 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 72 Enter A Number:- 5 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1 73 Enter A Number:- 5 1 2 3 4 5 2 3 4 5 3 4 5 4 5 5 74 Enter A Number:- 5 5 4 5 3 4 5 2 3 4 5 1 2 3 4 5 75 Enter Number of Rows:- 5 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1 76 Enter The Number of Rows:5 1 2 3 2 3 4 5 4 3 4 5 6 7 6 5 4 5 6 7 8 9 8 7 6 5 77 Enter The Number of Rows:5 1 1 2 1 1 2 3 2 1 1 2 3 4 5 4 3 2 1 1 2 3 2 1 1 2 1 1
  • 17.
    78 79 * ** * * # * * * * * * * * * # * # * * * * * * * # * * * # * * * * * # * * * * * # * * * # * * * * * * * # * # * * * * * * * * * # 80 Enter Upper Class Character:- F A B B C C C D D D E E E E F F F F F 81 Enter The Number of Rows: 6 A A A A A A B B B B B C C C C D D D E E F 82 Enter Number of Rows:- 6 A B C D E F A B C D E A B C D A B C A B A 83 Enter Number of Rows:- 8 8 6 4 2 8 6 4 8 6 4 8 6 8 84 Enter Number of Rows:- 10 * *C* *C*C* *C*C*C* *C*C*C*C*C* *C*C*C*C*C*C* *C*C*C*C*C**C*C* *C*C*C*C*C*C*C*C* *C*C*C*C*C* C*C*C*C* *C*C*C*C*C* C*C*C*C*C* 85 ABCDEFGFEDCBA ABCDEF FEDCBA ABCDE EDCBA ABCD DCBA ABC CBA AB BA A A 86 Enter Number of Rows:- 8 1 2 3 3 4 5 4 5 6 7 5 6 7 8 9 6 7 8 9 10 11 7 8 9 10 11 12 13 8 9 10 11 12 13 14 15 87 Enter Number of Rows:- 8 1 0 1 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 88 1 1 2 2 3 4 4 5 6 7 7 8 9 10 11 89 1 0 0 0 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 0 0 0 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 0 0 0 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 92 Enter Number of Rows:- 5 1 2 1 3 2 1 4 3 2 1 5 4 3 2 1 93 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
  • 18.
    94 * * * * * ** * * * * * * * * * * ******************* 95 1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 5 10 15 20 25 96 Enter Number of Rows:- 9 1 2 2 3 3 3 4 4 4 4 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 97 * * * * * * * * * 98 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 99 * * * * * * * * * 100 Su Sun Suna Sunaw Sunawa Sunawar 101 1 1 12 21 12321 102 ******* ***S*** **SSS** *SSSSS* 103 *****6 ****656 ***65456 **653456 *654323456 65432123456 104 10001 01010 00100 01010 10001 105 ZXYWVWXYZ ZYXWXYZ ZYXYZ ZYZ 106 ........9 .......898 ......78987 .....6789876 ....567898765 ...45678987654 ..3456789876543 .234567898765432 12345678987654321 107 COMPUTER O E M T P U U P T M E O RETUPMOC
  • 19.
    108 4 3 21 2 3 4 3 3 2 2 1 1 2 2 3 3 4 3 2 1 2 3 4 109 1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9 110 11111 10001 10001 11111 111 ************************ **** *** ************************ **** *** *** **** *** *** **** *** ************************ ********************* ************************ ********************* *** **** *** *** **** *** ************************ **** *** ************************ **** *** 112 1 1 12 21 123 321 1234 4321 123454321 1234 4321 123 321 12 21 1 1 113 * * * * * * * * * * * * * * 114 sssssssss sssssssss ssssossss sssssssss sssssssss 115 ......* ...* A * .* A B C * ...* A * ......* 116 1RR2RR3R 4R5 16RRRRRRRR6 15RRRRRRRR7 14RRRRRRRR8 13R12R11R10 9 117 1 2 7 3 8 12 4 9 13 16 5 10 14 17 19 6 11 15 18 20 21 118 0********* *1******** **2******* ***3****** ****4***** *****5**** 119 ###1### ##2#2## #3###3# 4#####4
  • 20.
    120 ######1###### #####121##### ####12321#### ###1234321### ##123454321## 121 program rogra ogr g ogr rogra program 122 5 5 25 7 1258 625 13 3125 11 15625 19 123 12345 - 12345 _1234 - 1234 __123 - 123 ___12 - 12 ____1 – 1 124 12345678910 13579 14710 159 16 17 18 19 110 1 124 1 333 55555 7777777 999999999 7777777 55555 333 1 125 7777777 7777777 666666 666666 55555 55555 4444 4444 333 333 22 22 1 1 126 C O O M M M P P P P U U U U U T T T T T T E E E E E E E R R R R R R R R 127 ----* ---*-* --*---* -*-----* --*---* ---*-* ----* 128 1234554321 1234---4321 123------321 12---------21 1------------1 12---------21 123------321 1234---4321 1234554321 129 *___________* ___*_______*_ _____*___*_ _______*____ _____*___*__ ___*_______* *____________* 130 1 212 32123 4321234 543212345
  • 21.
    131 *^*^*^*^*^*^ *^*^*^*^*^ *^*^*^*^ *^*^*^ *^*^ 132 1 2 34 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 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 133 5432* 543*1 54*21 5*321 *4321 134 4444444 4333334 4322234 4321234 4322234 4333334 4444444 135 * - *********** *** - ********* ***** - ******* ******* - ***** ********* - *** *********** - * 136 * * ** ** *** *** ******* *** *** ** ** * * 137 ***___*** ***___*** ********* ********* ***___*** ***___*** 138 * ** *** **** ***** ****** ***** **** *** ** * 139 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 140& 141
  • 22.
    Case Study:- Suppose yougive 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. ----------------------------------------------------------------------------------------------------------- S una w a r K ha n Ahs a n For solution contact me at 03334892200 Mail: sk_ahsan38@yahoo.com ----------------------------------------------------------------------------------------------------------- S.K. Ahasn