Notes Jan29
Notes Jan29
Prof. E. Natarajan,
Department of Mathematics,
IIST, Thiruvananthapuram, Kerala
▶
Prof. E. Natarajan MA-211 -Comp. Prog. Appl. 9 / 60
▶
▶
Prof. E. Natarajan MA-211 -Comp. Prog. Appl. 20 / 60
▶ # include < iostream >
using namespace std ;
int main ()
{
int res ,a ,b , c ;
cout < < " Enter 3 nos \ n " ;
cin > >a > >b > > c ;
res =a >= b && a >= c ? a :b >= a && b >= c ? b : c ;
cout < < " Greatest number is " << res < < endl ;
return 0;
}
▶ You could also write the same condition differently by changing the
way in which the conditional operators are nested. For example, the
following statement will also store the greatest of values a, b and c in
variable res: res = a>=b ? a>=c ? a :c : b>=c? b : c;
▶ You could also write the same condition differently by changing the
way in which the conditional operators are nested. For example, the
following statement will also store the greatest of values a, b and c in
variable res: res = a>=b ? a>=c ? a :c : b>=c? b : c;
▶ Whilst the statement does the same job, the only difference is that,
this statement internally translates to nested if-else blocks instead of
an else if ladder.
Prof. E. Natarajan MA-211 -Comp. Prog. Appl. 21 / 60
▶ Loops’ also give programmers a privilege to write the operation once
and execute it multiple times thereby saving on the programmer’s
time as well as reducing lines of code.
▶
▶ The for loop first performs initialization of the variable(s) specified in
initialization part of its syntax.
▶
▶ The for loop first performs initialization of the variable(s) specified in
initialization part of its syntax.
▶ After initialization is completed, the loop checks for the result of
condition specified. Only if the condition is evaluated as ‘true’,
operations within the loop will be executed.
▶
▶ The for loop first performs initialization of the variable(s) specified in
initialization part of its syntax.
▶ After initialization is completed, the loop checks for the result of
condition specified. Only if the condition is evaluated as ‘true’,
operations within the loop will be executed.
▶ After the specified operations are fully executed, the loop performs
“increment/decrement”
Prof. E. Natarajan MA-211 -Comp. Prog. Appl. 33 / 60
▶ Program to compute 12 + 22 + 32 + 42 + · · · x 2 using for loop
▶
▶ For example, if the user enters number of rows as three the output
triangle should contain only three rows as shown below:
▶
▶ For example, if the user enters number of rows as three the output
triangle should contain only three rows as shown below:
▶
▶ For example, if the user enters number of rows as three the output
triangle should contain only three rows as shown below:
▶
▶ As seen, row number 0 (which is the first row) contains 1 star, row
number 1 (which is the second row) contains 2 starts and so on. The
number of stars required in each row is documented in the table.
▶
▶ As seen, row number 0(which is the first row) contains four stars, row
number 1(which is the second row) contains three starts and so on.
The number of starts required in each row is documented in the
following table.
▶
▶ As seen, row number 0(which is the first row) contains four stars, row
number 1(which is the second row) contains three starts and so on.
The number of starts required in each row is documented in the
following table.
▶ From the table, it is clear that in any row number i there are r − i
stars to be printed.
▶
▶ As seen, row number 0(which is the first row) contains four stars, row
number 1(which is the second row) contains three starts and so on.
The number of starts required in each row is documented in the
following table.
▶ From the table, it is clear that in any row number i there are r − i
stars to be printed.
▶
Prof. E. Natarajan MA-211 -Comp. Prog. Appl. 40 / 60
▶
▶
Prof. E. Natarajan MA-211 -Comp. Prog. Appl. 45 / 60
▶