marți, 11 decembrie 2018

c++ programming part seven



638. The 4 numbers are read from the keyboard. How many of them are
odd numbers?
639. #include  
640. int main (){int a, b, c, d, n;
641. // read the input data: a, b, c and d
642. cout << "a ="; cin >> a;
643. cout << "b =" cin >> b;
644. cout << "C"; cin >> C;
645. cout << "d =" cin >> d;
646. // initialize a count of 0 with which we will increase by 1
647. every time we find a number
648. n = 0;
649. if (a % 2!= 0)
650. no ++;
651. if (b 0% 2!=)
652. n ++;
653. if (c 0% 2!=)
654. n ++;
655. if (d % 2!= 0)
656. n ++;
657. cout << "read" << << n << "count seems";
658. return 0;
659. }

660. Note that in C++ the sign != means not equal.
661. For example:
662. if (d % 2!= 3)
663. means d is not equal with the number 3.
664. Nested IF
665. If we want to check a multi-branch condition we can
666. use if nested statements, which are nothing but
667. some statements.
668. Example:
669. if (a> b) a = 1;
670. else if (b> a) b = 1;
671. else if (a == b) cout << "are equal";

help an old man
672. REPETITIVE CYCLES (LOOP STATEMENT)
673. When are they used?
674. They are used when we have to perform an operation (an statement) several
times.
675. There are two types of repetitive cycles: with a known number of steps (we know
from the beginning how many times a certain one will be executed the statement), or with
an unforeseen number of steps (we will execute an statement until a condition is

met).
676. Loop with known steps: CYCLE "FOR"
677. General form:
678. FOR (variable = initial value, variable = final value;
679. variable update)
680. statement;
681. In the example below:
682. - the loop variable is i
683. - the initial value is 1
684. - the final value is 5
685. - The update is i = i + 1
go to the church every week
686. Example:
687. #include  
688. # includes  
689. int main ()
690. {
691. int i;
692. - I declare an integer "i" variable
693. float x, y;
694. - I declare two x and y real-number variables with decimal places
695. cout << "x" << '\ t' << "y" << endl;
696. - read the two values ??in the cout stream
697. - we start a loop for
698. for (i = 0; i <6 1="" i="i" nbsp="" p="">699. - the loop variable is i, gets the initial value 1 after each execution of statemens
will increase by 1; the loop run up to i = 5, because the final value is i <6 nbsp="" p="">do not eat anything a day a week
700. {
701. x = 1 + 2 * i;
702. - at the first execution, I is equal to 1, 1 + 2 * i is equal to 3, so x
703. gets the value of 3
704. y = 2 * x + 3 + x;
705. - calculate the expression in the right leg, ie 2 * 1 + 3 + 1, so y
706. gets the value of 6
you go from time to time to a show

707. cout << x << y << endl;
708. -
709. the current values ??of x and y are displayed, ie 3 and 6
710. -
711. i grow by 1, that is, it becomes 2
712. -
713. test the condition: i <6 nbsp="" p="">714. -
715. 2 is less than 6, so it remains in the loop
716. -
717. the statements are repeated, up to i = 5
718. }
719. return 0;
720. - the main function returns an int result
721. }


Niciun comentariu:

Trimiteți un comentariu

Rețineți: Numai membrii acestui blog pot posta comentarii.