Programming For Engineers Lecture 05
Programming For Engineers Lecture 05
if
if-else
1
Train your mind……….!!!
2
for Loop
For loop
Output
0123456789
Table for 2
2x1=2
2x2=4
2x3=6
:
:
2 x 10 = 20
Example - Calculate Table for 2
#include <iostream.h>
main ( )
{
int counter ;
for ( counter = 1 ; counter <= 10 ; counter = counter + 1 )
{
cout << "2 x " << counter << " = " << 2* counter << "\n“ ;
}
}
Output
2 x1 = 2
2x2=4
2x3=6
:
:
2 x 10 = 20
Flow chart for the ‘Table’ example
Start
counter=1
While
No Exit
counter <=10?
yes
Print 2*counter
Counter =
counter + 1
Stop
#include <iostream>
using namespace std;
int main() {
// The counter variable can be declared in the init-expression.
#include <iostream.h>
main ( )
{
int number ;
int maxMultiplier ;
int counter ;
maxMultiplier = 10 ;
cout << " Please enter the number for which you wish to construct the table “ ;
cin >> number ;
for ( counter = 1 ; counter <= maxMultiplier ; counter = counter + 1 )
{
cout << number <<" x " << counter<< " = " << number * counter << "\n“ ;
}
}
#include <iostream>
using namespace std;
int main(){
for (int i = 10; i > 0; i--) {
cout << i << ' ';
}
// Output: 10 9 8 7 6 5 4 3 2 1
for (int i = 10; i < 20; i = i+2) {
cout << i << ' ';
}
// Output: 10 12 14 16 18
12
• Always think re-use
• Don’t use explicit constants
Pre-increment vs Post-increment
• Pre-increment and pre-decrement operators increments or
decrements the value of the object and returns a reference to the
result.
14
Example
• #include <iostream>
•
• int main()
•{
• int n1 = 1;
• int n2 = ++n1;
• int n3 = ++ ++n1;
• int n4 = n1++;
• // int n5 = n1++ ++; // error
• // int n6 = n1 + ++n1; // undefined behavior
• std::cout << "n1 = " << n1 << '\n'
• << "n2 = " << n2 << '\n'
• << "n3 = " << n3 << '\n'
• << "n4 = " << n4 << '\n';
•}
15
Increment operator
++
• counter ++ ;
same as
• counter = counter + 1;
Decrement operator
--
• counter -- ;
same as
• counter = counter - 1
+=
• counter += 3 ;
same as
• counter = counter + 3 ;
-=
• counter -= 5 ;
same as
• counter = counter – 5 ;
*=
x*=2
x=x*2
/=
x /= 2
x=x/2
Compound Assignment Operators
operator=
%=
• x %= 2 ;
same as
• x=x%2;
Comments
• Write comment at the top program to show
what it does
• Write comments that mean some thing
continue ;
continue
while trynum <= 5 ;
{
….
….
continue ;
}
continue in ‘for’ loop
28
Syntax
• The syntax for a nested for loop statement in C++ is as follows −
29
Prime Number Logic
• A positive integer which is only divisible by 1 and itself is known as
prime number.
30
Example
The following program uses a nested for loop to find the prime numbers from 2 to 100 −
• #include <iostream>
• using namespace std;
•
• int main () {
• int i, j;
•
• for(i = 2; i<100; i++) {
• for(j = 2; j <= (i/j); j++)
• if(!(i%j)) break; // if factor found, not prime
• if(j > (i/j)) cout << i << " is prime\n";
• }
•
• return 0;
•}
31
C++ Program To display the half pyramid
of * using nested-for loop
• This program is like the number triangle.
• In number triangle we were displaying numbers in this program we
are displaying character “*”.
• Here we are asking the user to enter the number of rows for
displaying the star pattern.
• You can replace character “*” with the character you like for eg : $ , %
, etc.
32
Sample Output
• Enter number of rows: 4
*
**
***
****
*/
33
Code
• #include<iostream>
• using namespace std;
• int main(){
• ///clrscr();
• int rows,i,j;
• cout<<"Enter number of rows:";
• cin>>rows;
• for(i=1;i<=rows;i++){
• for(j=1;j<=i;j++){
• cout<<"* ";
•}
• cout<<endl;
•}
• return 0;
•}
34
C++ program to find the sum of 2
matrices.
• Nested for loop is used to calculate the sum of two 2-dimensional
matrices.
• The program consists of three for nested loops where the outer loop
runs equal to size of row and inner loop runs equal to size of column.
• The first and second are used for entering the values of elements for
Matrix A and B, while the third is used for displaying the sum of the
elements of the two matrices.
• Matrix A and B are stored in 2-dimensional arrays a and b respectively.
• In the final nested loop, each element of a and b is traversed and the
sum is printed.
35
Output
• Enter size of row:2
• Enter size of column:3
• Enter elements of matrix A
• 270
• 3 -1 7
• Enter elements of matrix B
• 492
• 0 1 -8
• Sum of A and B
• 6 16 2
• 3 0 -1
36
Code
• #include <iostream> • for(i=0;i<row;i++)
• { • {